Library: Algorithms
Function
An algorithm that removes consecutive duplicates from a range of values and places the resulting unique values into the result
#include <algorithm> namespace std { template<class ForwardIterator> ForwardIterator unique(ForwardIterator start, ForwardIterator finish); template<class ForwardIterator, class BinaryPredicate> ForwardIterator unique(ForwardIterator start, ForwardIterator finish, BinaryPredicate binary_pred); template<class InputIterator, class OutputIterator> OutputIterator unique_copy(InputIterator start, InputIterator finish, OutputIterator result); template <class InputIterator, class OutputIterator, class BinaryPredicate> OutputIterator unique_copy(InputIterator start, InputIterator finish, OutputIterator result, BinaryPredicate binary_pred); }
The unique() algorithm moves through a sequence and eliminates all but the first element from every consecutive group of equal elements. There are two versions of the algorithm -- one that tests for equality and a second that tests adjacent elements against a binary predicate. An element is unique if it does not meet the corresponding condition listed here:
*i == *(i - 1)
or
binary_pred(*i, *(i - 1)) == true.
If an element is unique, it is copied to the front of the sequence, overwriting the existing elements. Once all unique elements have been identified. The remainder of the sequence is left unchanged, and unique() returns the end of the resulting range.
The unique_copy() algorithm copies the first element from every consecutive group of equal elements to an output iterator. The unique_copy() algorithm also has two versions -- one that tests for equality and a second that tests adjacent elements against a binary predicate.
unique_copy() returns the end of the resulting range.
For unique_copy(), exactly (finish - start) - 1 applications of the corresponding predicate are performed.
// // unique.cpp // #include <algorithm> #include <vector> #include <iostream> typedef std::vector<int, std::allocator<int> > vector_t; typedef std::insert_iterator<vector_t > v_iter_t; typedef std::ostream_iterator<int, char, std::char_traits<char> > os_iter_t; int main () { // Initialize two vectors. int a1[20] = {4, 5, 5, 9, -1, -1, -1, 3, 7, 5, 5, 5, 6, 7, 7, 7, 4, 2, 1, 1}; vector_t v(a1+0, a1+20), result; // Create an insert_iterator for results. v_iter_t ins (result, result.begin ()); // Demonstrate includes. std::cout << "The vector: " << std::endl << " "; std::copy (v.begin (), v.end (), os_iter_t (std::cout, " ")); // Find the unique elements. std::unique_copy(v.begin(), v.end(), ins); // Display the results. std::cout << std::endl << std::endl << "Has the following unique elements:" << std::endl << " "; std::copy (result.begin (), result.end (), os_iter_t (std::cout, " ")); std::cout << std::endl; return 0; } Program Output:
The vector: 4 5 5 9 -1 -1 -1 3 7 5 5 5 6 7 7 7 4 2 1 1 Has the following unique elements: 4 5 9 -1 3 7 5 6 7 4 2 1
ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 25.2.8