-
Notifications
You must be signed in to change notification settings - Fork 3k
/
ex10_18_19.cpp
76 lines (60 loc) · 1.62 KB
/
ex10_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
//
// @author @Yue Wang @pezy
// @date 12.10.2014
//
// 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>
// from ex 10.9
void elimdups(std::vector<std::string> &vs)
{
std::sort(vs.begin(), vs.end());
auto new_end = std::unique(vs.begin(), vs.end());
vs.erase(new_end, vs.end());
}
// ex10.18
void biggies_partition(std::vector<std::string> &vs, std::size_t sz)
{
elimdups(vs);
auto pivot = partition(vs.begin(), vs.end(), [sz](const std::string &s){
return s.size() >= sz;}
);
for(auto it = vs.cbegin(); it != pivot; ++it)
std::cout << *it << " ";
}
// ex10.19
void biggies_stable_partition(std::vector<std::string> &vs, std::size_t sz)
{
elimdups(vs);
auto pivot = stable_partition(vs.begin(), vs.end(), [sz](const std::string& s){
return s.size() >= sz;
});
for(auto it = vs.cbegin(); it != pivot; ++it)
std::cout << *it << " ";
}
int main()
{
// ex10.18
std::vector<std::string> v{
"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"
};
std::cout << "ex10.18: ";
std::vector<std::string> v1(v);
biggies_partition(v1, 4);
std::cout << std::endl;
// ex10.19
std::cout << "ex10.19: ";
std::vector<std::string> v2(v);
biggies_stable_partition(v2, 4);
std::cout << std::endl;
return 0;
}