-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy path10.25.cpp
58 lines (52 loc) · 1.57 KB
/
10.25.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
/*
* Exercise 10.25: In the exercises for § 10.3.2 (p. 392) you wrote a version
* of biggies that uses partition. Rewrite that function to use check_size and
* bind.
*
* by Faisal Saadatmand
*/
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
void elimDups(std::vector<std::string> &words)
{
sort(words.begin(), words.end());
auto end_uniqe = unique(words.begin(), words.end());
words.erase(end_uniqe, words.end());
}
std::string make_plural(size_t ctr, const std::string &word,
const std::string &ending)
{
return (ctr > 1) ? word + ending : word;
}
bool check_size(const std::string &s, std::string::size_type size)
{
return s.size() >= size;
}
void biggies(std::vector<std::string> &words,
const std::vector<std::string>::size_type sz)
{
using std::placeholders::_1;
elimDups(words);
stable_sort(words.begin(), words.end(),
[] (const std::string &s1, const std::string &s2)
{ return s1.size() < s2.size(); });
auto wc = partition(words.begin(), words.end(),
bind(check_size, _1, sz));
auto count = wc - words.begin();
std::cout << count << ' ' << make_plural(count, "word", "s")
<< " of length "<< sz << " or longer" << '\n';
for_each(words.begin(), wc,
[] (const std::string &s) { std::cout << s << ' '; });
}
int main()
{
std::vector<std::string> words{"the", "quick", "red", "fox", "jumps",
"over", "the", "slow", "red", "turtle"};
constexpr std::string::size_type size = 5;
biggies(words, size);
std::cout << std::endl;
return 0;
}