forked from Drishty06/STL-Functions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunique.cpp
29 lines (24 loc) · 800 Bytes
/
unique.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
// Used for: to remove duplicates of any element present consecutively in a range[first, last)
// syntax: iterator = unique(vec.begin(), vec.end())
// code snippet:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> v = { 1, 1, 3, 3, 3, 10, 1, 3, 3, 7, 7, 8 }, i;
vector<int>::iterator ip;
// Using std::unique
ip = std::unique(v.begin(), v.begin() + 12);
// Now v becomes {1 3 10 1 3 7 8 * * * * *}
// * means undefined
// Resizing the vector so as to remove the undefined terms
v.resize(std::distance(v.begin(), ip));
// Displaying the vector after applying std::unique
for (ip = v.begin(); ip != v.end(); ++ip) {
cout << *ip << " ";
}
return 0;
}
//Output: 1 3 10 1 3 7 8