forked from Mooophy/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex10_14_15_16_18_19.cpp
154 lines (121 loc) · 4.24 KB
/
ex10_14_15_16_18_19.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//! @Alan
//!
//! Exercise 10.14:
//! Write a lambda that takes two ints and returns their sum.
//!
//! Exercise 10.15:
//! Write a lambda that captures an int from its enclosing function
//! and takes an int parameter. The lambda should return the sum of
//! the captured int and the int parameter.
//!
//! Exercise 10.16:
//! Write your own version of the biggies function using lambdas.
//!
//! Exercise 10.18:
//! Rewrite biggies to use partition instead of find_if.
//!
//! Exercise 10.19:
//! Rewrite the previous exercise to use stable_partition, which like
//! stable_sort maintains the original element order in the paritioned
//! sequence.
//!
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
//! Exercise 10.14
auto sum = [](const int i1, const int i2 )
{ return i1 + i2; };
//! ^
//! @note There is a ";" here, just like that used after a body of a class.
//! Exercise 10.15
void f()
{
int i = 0;
auto sum = [i](const int j)
{
return i + j;
};
}
//! Exercise 10.16
void
wy_elimdups(std::vector<std::string> &vs);
void
wy_biggies(std::vector<std::string> &vs,const std::vector<std::string>::size_type sz);
//! Exercise 10.18
void
wy_biggies_partition(std::vector<std::string> &vs, const std::vector<std::string>::size_type sz);
//! Exercise 10.19
void
wy_biggies_STpartition(std::vector<std::string> &vs, const std::vector<std::string>::size_type sz);
int main()
{
return 0;
}
void wy_elimdups(std::vector<std::string> &vs)
{
for (auto element : vs)
std::cout << element
<<" ";
std::cout <<"\n";
//! sort alphabetically
std::sort(vs.begin(), vs.end());
for (auto element : vs)
std::cout << element
<<" ";
std::cout <<"\n";
//! put all duplicates at the end of the vector
//! and get the iterator pointing to the one past
//! the last unique element.
auto unique_iterator = std::unique(vs.begin(),vs.end());
for (auto element : vs)
std::cout << element
<<" ";
std::cout <<"\n";
vs.erase(unique_iterator, vs.end());
for (auto element : vs)
std::cout << element
<<" ";
std::cout <<"\n";
}
//! Exercise 10.16
void
wy_biggies(std::vector<std::string> &vs, const std::vector<std::string>::size_type sz)
{
wy_elimdups(vs);
// sort words by size, but maintain alphabetical order for words of the same size
std::stable_sort(vs.begin(), vs.end(),
[](const std::string &s1, const std::string &s2){return s1.size() < s2.size();});
// get an iterator to the first element whose size() is >= sz
auto wc = std::find_if(vs.begin(), vs.end(),
[sz](const std::string &s)
{return s.size() > sz;});
std::for_each(wc, vs.end(), [](const std::string &s)
{std::cout << s;});
}
//! Exercise 10.18
void wy_biggies_partition(std::vector<std::string> &vs, const std::vector<std::string>::size_type sz)
{
wy_elimdups(vs);
// sort words by size, but maintain alphabetical order for words of the same size
std::stable_sort(vs.begin(), vs.end(),
[](const std::string &s1, const std::string &s2){return s1.size() < s2.size();});
auto wc = std::partition(vs.begin(), vs.end(),
[sz](const std::string &s)
{return s.size() > sz;});
std::for_each(wc, vs.end(), [](const std::string &s)
{std::cout << s;});
}
//! Exercise 10.19
void wy_biggies_STpartition(std::vector<std::string> &vs, const std::vector<std::string>::size_type sz)
{
wy_elimdups(vs);
// sort words by size, but maintain alphabetical order for words of the same size
std::stable_sort(vs.begin(), vs.end(),
[](const std::string &s1, const std::string &s2){return s1.size() < s2.size();});
auto wc = std::stable_partition(vs.begin(), vs.end(),
[sz](const std::string &s)
{return s.size() > sz;});
std::for_each(wc, vs.end(), [](const std::string &s)
{std::cout << s;});
}