-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
ex10_16.cpp
55 lines (46 loc) · 1.33 KB
/
ex10_16.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
//!
//! @author @Yue Wang @pezy
//! @date 02.12.2014
//!
//! Exercise 10.16:
//! Write your own version of the biggies function using lambdas.
//!
#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());
}
void biggies(std::vector<std::string>& vs, std::size_t sz)
{
using std::string;
elimdups(vs);
//! sort by size, but maintain alphabetical order for same size.
std::stable_sort(vs.begin(), vs.end(),
[](string const& lhs, string const& rhs) {
return lhs.size() < rhs.size();
});
//! get an iterator to the first one whose size() is >= sz
auto wc = std::find_if(vs.begin(), vs.end(),
[sz](string const& s) { return s.size() >= sz; });
//! print the biggies
std::for_each(wc, vs.end(), [](const string& s) { std::cout << s << " "; });
}
int main()
{
//! ex10.16
std::vector<std::string> v{"1234", "1234", "1234", "hi~",
"alan", "alan", "cp"};
std::cout << "ex10.16: ";
biggies(v, 3);
std::cout << std::endl;
return 0;
}
//! output :
//!
// ex10.16: hi~ 1234 alan