forked from chronolaw/boost_guide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforeach.cpp
More file actions
106 lines (84 loc) · 1.99 KB
/
Copy pathforeach.cpp
File metadata and controls
106 lines (84 loc) · 1.99 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright (c) 2015
// Author: Chrono Law
#include <std.hpp>
using namespace std;
#include <boost/foreach.hpp>
#include <boost/assign.hpp>
//////////////////////////////////////////
void case1()
{
using namespace boost::assign;
vector<int> v = (list_of(1),2,3,4,5);
BOOST_FOREACH(auto x, v)
{
cout << x << ",";
}
cout << endl;
string str("boost foreach");
BOOST_FOREACH(auto& c, str)
{
cout << c << "-";
}
set<int> s = list_of(10)(20)(30);
int x;
BOOST_FOREACH (x, s)
{
if (++x % 7 == 0)
{
cout << x << endl;
break;
}
}
}
//////////////////////////////////////////
#define foreach BOOST_FOREACH
#define reverse_foreach BOOST_REVERSE_FOREACH
void case2()
{
int ar[] = {1,2,3,4,5};
foreach(auto& x, ar)
cout << x << " ";
cout << endl;
map<int, string> m = {{1, "111"},{2, "222"},{3, "333"}};
foreach(auto& x, m)
cout << x.first << x.second << endl;
vector< vector<int> > v = {{1,2},{3,4}};
foreach(auto& row, v)
{
reverse_foreach(auto& z, row)
cout << z << ",";
cout << endl;
}
}
//////////////////////////////////////////
#include <boost/array.hpp>
#include <boost/circular_buffer.hpp>
#include <boost/unordered_set.hpp>
void case3()
{
using namespace boost::assign;
boost::array<int, 5> ar = (list_of(1), 2, 3, 4, 5);
foreach(auto x, ar)
cout << x << " ";
cout << endl;
pair<decltype(ar.begin()), decltype(ar.end())>
rng(ar.begin(), ar.end() -2);
foreach(auto x, rng)
cout << x << " ";
cout << endl;
boost::circular_buffer<int> cb = list_of(1)(2)(3);
foreach(auto x, cb)
cout << x << " ";
cout << endl;
boost::unordered_set<double> us = list_of(3.14)(2.717)(0.618);
foreach(auto x, us)
cout << x << " ";
cout << endl;
}
//////////////////////////////////////////
int main()
{
case1();
case2();
case3();
}