public
Last active

C++11 range based for with containers

  • Download Gist
foo.cpp
C++
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
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <list>
#include <map>
using namespace std;
 
int main()
{
int arr[] { 10, 20, 30 };
string str { "string" };
vector<double> vd { 1.4142, 2.7183, 3.1415 };
set<string> ss { "foo", "bar", "baz" };
list<char> lc { 'x', 'y', 'z' };
map<int,string> mis { { 100, "hundred" }, { 20, "twenty" }, { 50, "fifty" } };
for(auto &i : arr)
cout << i << endl;
for(auto &i : str)
cout << i << endl;
for(auto &i : vd)
cout << i << endl;
for(auto &i : ss)
cout << i << endl;
for(auto &i : lc)
cout << i << endl;
for(auto &i : mis)
cout << i.first << ": " << i.second << endl;
}
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
10
20
30
s
t
r
i
n
g
1.4142
2.7183
3.1415
bar
baz
foo
x
y
z
20: twenty
50: fifty
100: hundred

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.