Skip to content

Instantly share code, notes, and snippets.

@Rhomboid
Created October 10, 2012 21:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rhomboid/3868698 to your computer and use it in GitHub Desktop.
Save Rhomboid/3868698 to your computer and use it in GitHub Desktop.
C++11 range based for with containers
#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;
}
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment