-
Notifications
You must be signed in to change notification settings - Fork 7
/
03_foreach_assoc.cpp
47 lines (33 loc) · 1.06 KB
/
03_foreach_assoc.cpp
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
38
39
40
41
42
43
44
45
46
47
/* output
set: 1 2 3 4 5 6
map: one:1 three:3 two:2
unordered_map: three:3 one:1 two:2
unordered_set: 6 5 4 3 2 1
*/
#include <range/v3/algorithm/for_each.hpp> //specific includes
namespace rng = ranges::v3;
#include <iostream>
#include <string>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
using namespace std;
auto print = [] (int i) { cout << i << " "; };
//must take a pair for map types
auto printm = [] (pair<string, int> p) { cout << p.first << ":" << p.second << " "; };
int main() {
cout << "set: ";
set<int> si { 1, 2, 3, 4, 5, 6 };
rng::for_each( si, print );
cout << endl << "map: ";
map<string, int> msi { { "one", 1 }, { "two", 2 }, { "three", 3 } };
rng::for_each( msi, printm );
cout << endl << "unordered_map: ";
unordered_map<string, int> umsi { { "one", 1 }, { "two", 2 }, { "three", 3 } };
rng::for_each( umsi, printm );
cout << endl << "unordered_set: ";
unordered_set<int> usi { 1, 2, 3, 4, 5, 6 };
rng::for_each( usi, print );
cout << endl;
}