-
Notifications
You must be signed in to change notification settings - Fork 67
/
partition_uniques_duplicates.cpp
68 lines (52 loc) · 1.34 KB
/
partition_uniques_duplicates.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
#include "../prep/random.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
template <typename iterator>
iterator partition_uniques_duplicates(iterator begin, iterator end) {
if(begin == end) return end;
sort(begin, end);
auto write = begin;
for(auto read = begin + 1; read != end; ++read) {
if(*read != *write) {
swap(*read, *++write);
}
}
return ++write;
}
int main(int argc, char **argv) {
cout << "Seed: " << seed_random(argc > 2 ? atoi(argv[2]) : 0) << endl;
vector<int> v(argc > 1 ? atoi(argv[1]) : 10);
size_t dups = v.size() / 3;
cout << "Generating at least " << dups << " duplicates" << endl;
cout << "Input values:" << endl;
for(size_t i = dups; i < v.size(); ++i) {
v[i] = rand() % 100;
cout << ' ' << v[i];
}
for(size_t i = 0; i < dups; ++i) {
v[i] = v[i + dups];
cout << ' ' << v[i];
}
cout << endl;
auto duplicates = partition_uniques_duplicates(begin(v), end(v));
cout << "Uniques:" << endl;
for(auto i = begin(v); i != duplicates; ++i) {
cout << ' ' << *i;
}
cout << endl;
cout << "Duplicates:" << endl;
for(auto i = duplicates; i != end(v); ++i) {
cout << ' ' << *i;
}
cout << endl;
sort(begin(v), end(v));
cout << "Sorted values:" << endl;
for(auto i = begin(v); i != end(v); ++i) {
cout << ' ' << *i;
}
cout << endl;
return 0;
}