Created
October 10, 2012 21:50
-
-
Save Rhomboid/3868698 to your computer and use it in GitHub Desktop.
C++11 range based for with containers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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