1. 程式人生 > >c++11 遍歷容器的方法

c++11 遍歷容器的方法

以輸出vector中的每個元素為例,四種遍歷容器的方法。

vector<string> vec = {"c++","is","the","best","program","language"};

  1. 普通for迴圈
    for(auto beg = vec.begin(); beg!=vec.end();++beg){
    cout<<*beg<<" ";
    }
  2. 範圍for迴圈
    for(auto temp : vec){
       cout<<*temp<<" ";
    }
  3. lambda 表示式結合for_each演算法
    for_each(vec.begin(),vec.end(),[](string s){cout<<s<<" ";});

  4. 建立一個遍歷類,過載函式呼叫運算子
    class print_string{
    private:
      ostream os;
      string sep;
    public:
      print_string(ostream &o = cout, string s = " "):os(o),sep(s){}
      void operator()(string s){
         os<<s<<sep;
      }
    };
    
    for_each(vec.begin(),vec.end(),print_string());
    

PS:我只是為了測試csdn的部落格編輯器好不好用,以上程式碼都是直接用編輯器的程式碼片段編寫的,並沒有測試過,如果有錯,你來打我噻~

相關推薦

c++11 容器方法

以輸出vector中的每個元素為例,四種遍歷容器的方法。 vector<string> vec = {"c++","is","the","best","program","language"}; 普通for迴圈 for(auto beg = vec.begin(

C# winForm 容器控件內所有Label,修改其背景色

tab () PE windows clas out ont code tostring //容器控件為tableLayoutPanel1        foreach (Control label in tableLayoutPanel1.Con

C++11::tuple中的元素

轉自:https://blog.csdn.net/lanchunhui/article/details/49868077 在設計tuple的遍歷程式之前,我們不妨先進行std::pair的列印工作: // #include <utility> cout << make

C++11中對容器的各種循環的效率比較

ets normal pre unsigned int qdebug tex contain string 1 #include "CycleTimeTst.h" 2 #include <string> 3 #include <vector&

#C++11新特性 for迴圈容器#

#include<iostream> #include<string> using namespace std; string s = "hello"; for (auto &i : s ) i = toupper(i); //改

jsp c標簽不方法

style clas class jsp 方法 bsp item nbsp data 生產中遇到過不需要遍歷的事情。 一般遍歷必須要 <c:forEach items="${resultObj.xxx}" var="data" varStatus="status"&

C#中ArraryList的三種方法

using System; using System.Collections; using System.Linq; using System.Text; namespace ArrayListDemo { class Program { static void

C#】各類資料集合的方法

    1、列舉型別 //遍歷列舉型別Sample的各個列舉名稱 foreach (string sp in Enum.GetNames(typeof(Sample)))

c語言字串陣列的方法

在這裡我們重點介紹遍歷字串的三種方法。   首先我們來看一道程式設計題目:   輸入一個字串,且都是數字,也可以是負數,轉化成相應的整型數並輸出,若輸入字母則停止。   我們知道,在C語言裡有一個函式是“atoi”,它可以把字串轉換成整型數,包含在標頭檔案stdlib

C#中Hashtable的4種方法

直接上程式碼,程式碼中使用四種方法遍歷Hashtable。 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

C++ 中使用for迴圈和迭代器容器

之前一直看的是第四版的《C++ Primer》,裡面貌似只介紹了用迭代器遍歷容器(可能是受當時版本所限),這裡記錄下如何用for迴圈來遍歷容器(也可遍歷普通陣列) class Solution{ // C++ 中預設的成員函式型別是private,

jQuery - eq() 方法

集合 text nbsp 指示 clas src css doc 如果 <!DOCTYPE html> <html> <head> <style> div { width:60px; height:60px; mar

窗體內元素-通用方法(DevExpress 中BarManager的

ger 需求 lds == eve type() arp exp .get 需求:WinForm窗體中對於各個元素進行遍歷設置。 本身因為程序絕大部分功能已經開發完畢,但是權限控制沒有追加,在後續開發中要求對窗體內各個資源的使用權限進行控制,最初各個資源並非動態添加的,所

iOS中數組方法及比較

結果 keyword div standard c語言 tracking dsm track 代碼 數組遍歷是編碼中很常見的一種需求,我們來扒一拔iOS裏面都有什麽樣的方法來實現,有什麽特點。 因為ios是兼容C語言的,所以c語言裏面的最最常見的for循環遍

通過Java對象來成員方法,成員變量,構造函數

return pri typename static spa tde log 繼承 red 1 package classTest; 2 3 import java.lang.reflect.Constructor; 4 import java.lang.ref

C++ Primer-小技巧

spa res pri cores code 一個 使用 個數 primer 使用一下方法遍歷較簡單,且輸出是占一行: for (auto c : scores) { cout << c << " "; } 比起下

C++ Vector的幾種方式及效能對比

幾種容器遍歷方法 1.迭代器 for (std::vector<int>::iterator it = vecTest.begin(); it != vecTest.end(); ++it) { tempNum = *it; } 2.C++11 新增關鍵字auto f

hdfs檔案方法

import org.apache.commons.lang.StringUtils; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*;   import jav

C++:vector,拷貝,插入

vector遍歷: 類C遍歷,按插入順序 vector<int> a = {1,2,3,4,5}; for(int i=0;i<a.size();i++) printf("%d%s",a[i],i==a.size()-1?"\n":" "); 迭代器遍歷

c forEach 集合中的元素屬性詳解,判斷是否是最後一個元素,

分享一下我老師大神的人工智慧教程吧。零基礎,通俗易懂!風趣幽默!http://www.captainbed.net/ 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!