-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunique2.cc
More file actions
40 lines (36 loc) · 1019 Bytes
/
unique2.cc
File metadata and controls
40 lines (36 loc) · 1019 Bytes
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
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
#include <time.h>
using namespace std;
template<typename ForwardIterator>
ForwardIterator
myUnique(ForwardIterator begin, ForwardIterator end)
{
auto it1 = begin;
auto it2 = begin;
while (++it1 != end)
{
if (!(*it1 == *it2)) {
*(++it2) = *it1;
}
}
return ++it2;
}
int
main (int argc, char *argv[])
{
int arr[20];
srand(time(NULL));
for_each(begin(arr), end(arr), [] (int &x) { x = rand() % 100; });
cout << "Input data" << endl;
for_each(begin(arr), end(arr), [] (const int &x) { cout << x << " "; });
sort(begin(arr), end(arr), std::greater<int>());
cout << endl << "Sorted data" << endl;
for_each(begin(arr), end(arr), [] (const int &x) { cout << x << " "; });
auto itr = myUnique(begin(arr), end(arr));
cout << endl << "Sorted Unique data" << endl;
for_each(begin(arr), itr, [] (const int &x) { cout << x << " "; });
cout << endl;
}