forked from Mooophy/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex10_11.cpp
52 lines (42 loc) · 1.06 KB
/
ex10_11.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
// @Alan
// Exercise 10.11:
// Write a program that uses stable_sort and isShorter to sort a vector passed
// to your version of elimDups.
// Print the vector to verify that your program is correct.
//
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <list>
// print a container like vector, deque, list, etc.
template<typename Sequence>
inline std::ostream& println(Sequence const& seq)
{
for(auto const& elem : seq) std::cout << elem << " ";
std::cout << std::endl;
return std::cout;
}
inline bool
is_shorter(std::string const& lhs, std::string const& rhs)
{
return lhs.size() < rhs.size();
}
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());
}
int main()
{
std::vector<std::string> v{
"1234", "1234", "1234", "Hi", "alan", "wang"
};
elimdups(v);
std::stable_sort(v.begin(), v.end(), is_shorter);
std::cout << "ex10.11 :\n";
println(v);
return 0;
}