forked from jaege/Cpp-Primer-5th-Exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
14.40.cpp
61 lines (51 loc) · 1.63 KB
/
14.40.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
// based on ex10.16.cpp
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
std::string make_plural(size_t ctr, const std::string &word,
const std::string &ending = "s") {
return (ctr > 1) ? word + ending : word;
}
void elimDups(std::vector<std::string> &words) {
std::sort(words.begin(), words.end());
auto end_unique = std::unique(words.begin(), words.end());
words.erase(end_unique, words.end());
}
struct Shorter {
bool operator()(const std::string &s1, const std::string &s2) const {
return s1.size() < s2.size();
}
};
struct Longer {
Longer(size_t size) : sz(size) { }
bool operator()(const std::string &s) const { return s.size() >= sz; }
private:
size_t sz;
};
struct PrintString {
PrintString(std::ostream &o = std::cout, char d = ' ')
: os(o), delimiter(d) { }
void operator()(const std::string &s) const { os << s << delimiter; }
private:
std::ostream &os;
char delimiter;
};
void biggies(std::vector<std::string> words, // use value instead of reference
std::vector<std::string>::size_type sz) {
elimDups(words);
std::stable_sort(words.begin(), words.end(), Shorter());
auto iter = std::find_if(words.begin(), words.end(), Longer(sz));
auto count = words.end() - iter;
std::cout << count << " " << make_plural(count, "word") << " of length "
<< sz << " or longer." << std::endl;
std::for_each(iter, words.end(), PrintString());
}
int main() {
std::vector<std::string> words;
for (std::string s; std::cin >> s; words.push_back(s)) { }
biggies(words, 3);
std::cout << std::endl;
biggies(words, 5);
return 0;
}