function template
std::unique
<algorithm>
template <class ForwardIterator>
ForwardIterator unique ( ForwardIterator first, ForwardIterator last );
template <class ForwardIterator, class BinaryPredicate>
ForwardIterator unique ( ForwardIterator first, ForwardIterator last,
BinaryPredicate pred );
Remove consecutive duplicates in range
Removes the duplicate consecutive elements from the range
[first,last). This is done by removing from the resulting range all the elements that compare equal to the element right preceding them (only the first element in each group of consecutive equal elements is kept).
The resulting range consists of the elements between
first and the iterator returned by the function, which points to the new end of range. The elements past the new end of range are still valid, although with unspecified values.
The comparison between elements is performed by either applying the
== comparison operator, or the template parameter
comp (for the second version) between them.
The behavior of this function template is equivalent to:
1 2 3 4 5 6 7 8 9 10 11
|
template <class ForwardIterator>
ForwardIterator unique ( ForwardIterator first, ForwardIterator last )
{
ForwardIterator result=first;
while (++first != last)
{
if (!(*result == *first)) // or: if (!pred(*result,*first)) for the pred version
*(++result)=*first;
}
return ++result;
}
|
Notice that in this example implementation the elements past the new end are not altered, but alternative implementations may change their values.
Parameters
- first, last
- Forward iterators to the initial and final positions of the sequence. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
- pred
- Binary predicate taking two elements as argument and returning the result of the comparison between them, with true (non-zero) meaning that they are to be considered equal, and false (zero) for not-equal. This can either be a pointer to a function or an object whose class overloads operator().
Return value
A forward iterator pointing to the new end of the sequence, which now does not include the elements that were consecutive duplicates.
Example
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
|
// unique algorithm example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
bool myfunction (int i, int j) {
return (i==j);
}
int main () {
int myints[] = {10,20,20,20,30,30,20,20,10}; // 10 20 20 20 30 30 20 20 10
vector<int> myvector (myints,myints+9);
vector<int>::iterator it;
// using default comparison:
it = unique (myvector.begin(), myvector.end()); // 10 20 30 20 10 ? ? ? ?
// ^
myvector.resize( it - myvector.begin() ); // 10 20 30 20 10
// using predicate comparison:
unique (myvector.begin(), myvector.end(), myfunction); // (no changes)
// print out content:
cout << "myvector contains:";
for (it=myvector.begin(); it!=myvector.end(); ++it)
cout << " " << *it;
cout << endl;
return 0;
}
|
Output:
myvector contains: 10 20 30 20 10
|
Complexity
At most, performs as many assignments and comparisons or applications of
pred as the number of elements in the range
[first,last).
See also
- unique_copy
- Copy range removing duplicates (function template
)
- adjacent_find
- Find equal adjacent elements in range (function template
)
- remove
- Remove value from range (function template
)
- remove_if
- Remove elements from range (function template
)