unique
PrototypeUnique is an overloaded name; there are actually two unique functions. template <class ForwardIterator> ForwardIterator unique(ForwardIterator first, ForwardIterator last); template <class ForwardIterator, class BinaryPredicate> ForwardIterator unique(ForwardIterator first, ForwardIterator last, BinaryPredicate binary_pred); DescriptionEvery time a consecutive group of duplicate elements appears in the range[first, last), the algorithm unique removes all but the first element. That is, unique returns an iterator new_last such that the range [first, new_last) contains no two consecutive elements that are duplicates. [1] The iterators in the range [new_last, last) are all still dereferenceable, but the elements that they point to are unspecified. Unique is stable, meaning that the relative order of elements that are not removed is unchanged.
The reason there are two different versions of DefinitionDefined in the standard header algorithm, and in the nonstandard backward-compatibility header algo.h.Requirements on typesFor the first version:
Preconditions
ComplexityLinear. Exactly(last - first) - 1 applications of operator== (in the case of the first version of unique) or of binary_pred (in the case of the second version). ExampleRemove duplicates from consecutive groups of equalints. Vector<int> V; V.push_back(1); V.push_back(3); V.push_back(3); V.push_back(3); V.push_back(2); V.push_back(2); V.push_back(1); Vector<int>::iterator new_end = unique(V.begin(), V.end()); copy(V.begin(), new_end, ostream_iterator<int>(cout, " ")); // The output it "1 3 2 1".
Remove all duplicates from a vector of inline bool eq_nocase(char c1, char c2) { return tolower(c1) == tolower(c2); } inline bool lt_nocase(char c1, char c2) { return tolower(c1) < tolower(c2); } int main() { const char init[] = "The Standard Template Library"; Vector<char> V(init, init + sizeof(init)); sort(V.begin(), V.end(), lt_nocase); copy(V.begin(), V.end(), ostream_iterator<char>(cout)); cout << endl; Vector<char>::iterator new_end = unique(V.begin(), V.end(), eq_nocase); copy(V.begin(), new_end, ostream_iterator<char>(cout)); cout << endl; } // The output is: // aaaabddeeehiLlmnprrrStTtTy // abdehiLmnprSty Notes[1] Note that the meaning of "removal" is somewhat subtle.Unique, like remove, does not destroy any iterators and does not change the distance between first and last. (There's no way that it could do anything of the sort.) So, for example, if V is a Vector, remove(V.begin(), V.end(), 0) does not change V.size(): V will contain just as many elements as it did before. Unique returns an iterator that points to the end of the resulting range after elements have been removed from it; it follows that the elements after that iterator are of no interest. If you are operating on a Sequence, you may wish to use the Sequence's erase member function to discard those elements entirely.
[2] Strictly speaking, the first version of
[3] See alsoBinaryPredicate, remove, remove_if, unique_copy, adjacent_find, | |||||||

