forked from chronolaw/professional_boost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfor.cpp
102 lines (80 loc) · 1.58 KB
/
for.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
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
// Copyright (c) 2016
// Author: Chrono Law
#include <std.hpp>
using namespace std;
#include <boost/core/ignore_unused.hpp>
///////////////////////////////////////
void case1()
{
int a[] = {2,3,5,7};
for(int i = 0;i < 4; ++i)
{
cout << a[i] << ",";
}
cout << endl;
vector<int> v = {253, 874};
for(auto iter = v.begin(); iter != v.end(); ++iter)
{
cout << *iter << ",";
}
cout << endl;
}
///////////////////////////////////////
void case2()
{
int a[] = {2,3,5,7};
for(auto&& x : a)
{
cout << x << ",";
}
cout << endl;
vector<int> v = {253, 874};
for(const auto& x : v)
{
cout << x << ",";
}
cout << endl;
for(auto& x : v)
{
cout << ++x << ",";
}
cout << endl;
auto&& _range = v;
for(auto _begin = std::begin(_range),
_end = std::end(_range);
_begin!= _end; ++_begin)
{
auto x = *_begin;
boost::ignore_unused(x);
}
}
///////////////////////////////////////
namespace std {
template<typename I>
auto begin(const std::pair<I, I>& p) ->decltype(p.first)
{
return p.first;
}
template<typename I>
auto end(const std::pair<I, I>& p) -> decltype(p.second)
{
return p.second;
}
} //namespace std
void case3()
{
vector<int> v = {1,2,3,4,5};
auto r = std::make_pair(v.begin(), v.begin() + 3);
for(auto x : r)
{
cout << x << ",";
}
}
///////////////////////////////////////
int main()
{
std::cout << "hello C++11/14 for" << std::endl;
case1();
case2();
case3();
}