forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex10_34_35_36_37.cpp
127 lines (107 loc) · 2.95 KB
/
ex10_34_35_36_37.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//! @Alan
//!
//! Exercise 10.34:
//! Use reverse_iterators to print a vector in reverse order.
//!
//! Exercise 10.35:
//! Now print the elements in reverse order using ordinary iterators.
//!
//! Exercise 10.36:
//! Use find to find the last element in a list of ints with value 0.
//!
//! Exercise 10.37:
//! Given a vector that has ten elements, copy the elements from positions
//! 3 through 7 in reverse order to a list.
//!
#include <iostream>
#include <fstream>
#include <algorithm>
#include <iterator>
#include <string>
#include <list>
//! Exercise 10.34
void
r_print(const std::vector<std::string> &v);
//! Exercise 10.35
void
r_withOrdinart_print(const std::vector<std::string> &v);
//! Exercise 10.36
std::list<int>::iterator
find_last_0(std::list<int> &l);
//! Exercise 10.37
void
vec2list_3_7_reverse(const std::vector<int> &v, std::list<int> &l);
int main()
{
std::vector<std::string> v={"aa","bb","cc"};
//! test for 10.34
r_print(v);
std::cout << "\n";
//! test for 10.35
r_withOrdinart_print(v);
std::cout << "\n";
//! test for 10.36
std::list<int> l = {1,2,3,4,0,5,6};
auto it = find_last_0(l);
std::cout << *it << "\n";
//! test for 10.37
std::vector<int> vi = {1,2,3,4,5,6,7,8,9,10};
std::list<int> lst;
vec2list_3_7_reverse(vi,lst);
for(auto e : lst)
std::cout << e <<" ";
return 0;
}
//! Exercise 10.34
inline void
r_print(const std::vector<std::string> &v)
{
std::for_each(v.rbegin(), v.rend(),[](const std::string &s)
{
std::cout << s << " ";
});
}
//! Exercise 10.35
inline void
r_withOrdinart_print(const std::vector<std::string> &v)
{
for (auto it = v.end() - 1; it != v.begin() - 1; --it)
std::cout << *it << " ";
}
//! Exercise 10.36
//! @note reverse iterator can not conver to oridinary directly
//! it may be done via the member base().
inline std::list<int>::iterator
find_last_0(std::list<int> &l)
{
//! assumimg : 1 2 3 4 0 5 6
//! 1 2 3 4 0 5 6
//! ^
//! to which r_it refer.
std::list<int>::reverse_iterator
r_it = std::find(l.rbegin(), l.rend(), 0);
//! 1 2 3 4 0 5 6
//! ^
//! to which it refer.
std::list<int>::iterator it = r_it.base();
//! 1 2 3 4 0 5 6
//! ^
//! to which --it refer.
return --it;
}
//! Exercise 10.37:
//! Given a vector that has ten elements, copy the elements from positions
//! 3 through 7 in reverse order to a list.
inline void
vec2list_3_7_reverse(const std::vector<int> &v, std::list<int> &l)
{
//! 1 2 3 4 5 6 7 8 9 10
//! ^ ^^
//! rend rbegin
std::copy(v.rbegin() + 3, v.rend() - 2, std::back_inserter(l));
//! ^
//! @note: std::copy copies the range [first,last) into result.
//! hence, the arguments here denote:
//! [7 6 5 4 3 2)
//! ^ this one is specified but not included.
}