-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathstl_13_unique_remove_if.cpp
70 lines (56 loc) · 1.27 KB
/
stl_13_unique_remove_if.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
62
63
64
65
66
67
68
69
70
// code_report
// https://youtu.be/Mclo4og_7fE
#include <vector>
#include <algorithm>
#include <iostream>
#include <functional>
using namespace std;
template<typename T>
void print(T t) {
for (auto e : t) cout << e << ' ';
cout << '\n';
}
void example1() {
vector v = { 1, 2, 2, 3, 3, 1, 2, 2 };
v.erase(unique(begin(v), end(v)), end(v));
print(v);
}
void example2() {
vector v = { 1, 2, 2, 3, 3, 1, 2, 2 };
vector<int> u;
unique_copy(begin(v), end(v), back_inserter(u));
print(u);
}
void example3() {
vector v = { 1, 2, 2, 3, 3, 1, 2, 2 };
v.erase(unique(begin(v), end(v), greater<>()), end(v));
print(v);
}
void example4() {
vector v = { 1, 2, 2, 3, 3, 1, 2, 2 };
v.erase(unique(begin(v), end(v), less<>()), end(v));
print(v);
}
template<typename F>
auto my_unique(F f, F l) {
auto comp_with_prev = [prev = *f](auto e) mutable {
auto p = prev; prev = e; return e == p;
};
return remove_if(next(f), l, comp_with_prev);
}
void example5() {
vector v = { 1, 2, 2, 3, 3, 1, 2, 2 };
vector u = v;
v.erase(unique(begin(v), end(v)), end(v));
u.erase(my_unique(begin(u), end(u)), end(u));
print(v);
print(u);
}
int main() {
example1();
example2();
example3();
example4();
example5();
return 0;
}