forked from Mooophy/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex10_09.cpp
44 lines (37 loc) · 1021 Bytes
/
ex10_09.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
//
// @author @Yue Wang @shbling @pezy @zzzkl @Yue Wang
// @date 01.12.2014 Jun 2015
//
// Exercise 10.9:
// Implement your own version of elimDups. Test your program by printing
// the vector after you read the input, after the call to unique, and after
// the call to erase.
//
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
// print containers like vector, deque, list, etc.
template<typename Sequence>
auto println(Sequence const& seq) -> std::ostream&
{
for (auto const& elem : seq)
std::cout << elem << " ";
return std::cout << std::endl;
}
auto eliminate_duplicates(std::vector<std::string> &vs) -> std::vector<std::string>&
{
std::sort(vs.begin(), vs.end());
println(vs);
auto new_end = std::unique(vs.begin(), vs.end());
println(vs);
vs.erase(new_end, vs.end());
return vs;
}
int main()
{
std::vector<std::string> vs{ "a", "v", "a", "s", "v", "a", "a" };
println(vs);
println(eliminate_duplicates(vs));
return 0;
}