forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex10_09.cpp
61 lines (54 loc) · 1.49 KB
/
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//!
//! @author @Yue Wang @shbling @pezy @zzzkl
//! @date 01.12.2014
//!
//! 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 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;
}
//! Exercise 10.9
void elimdups(std::vector<std::string> &vs)
{
println(vs);
//! sort
std::sort(vs.begin(), vs.end());
println(vs);
//! put all duplicates at the end of the vector
auto new_end = std::unique(vs.begin(),vs.end());
println(vs);
//! ^^^^^^^^^^^^
//! @attention Accessing from new_end to end() should be considered as a UB.
//! As a result, it would behave differently on different platform.
//! For detail, see : https://github.com/Mooophy/Cpp-Primer/pull/85#issuecomment-65016500
//! erase duplicates
vs.erase(new_end, vs.end());
println(vs);
}
int main()
{
//! ex 10.9
std::vector<std::string> vs{"a","v","a","s","a","a","a"};
std::cout << "ex 10.9:\n";
elimdups(vs);
return 0;
}
//! output:
//ex 10.9:
//a v a s a a a
//a a a a a s v
//a s v a a a a // VS2013 or clang will have some differences. (a s v a a)
//a s v