std::unique

Header: <algorithm>

Removes all except the first element from every consecutive group of equivalent elements from the range [first,last) and returns a past-the-end iterator for the new end of the range.

# Declarations

template< class ForwardIt >
ForwardIt unique( ForwardIt first, ForwardIt last );

(constexpr since C++20)

template< class ExecutionPolicy, class ForwardIt >
ForwardIt unique( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last );

(since C++17)

template< class ForwardIt, class BinaryPred >
ForwardIt unique( ForwardIt first, ForwardIt last, BinaryPred p );

(constexpr since C++20)

template< class ExecutionPolicy,
class ForwardIt, class BinaryPred >
ForwardIt unique( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last, BinaryPred p );

(since C++17)

# Parameters

# Return value

A ForwardIt to the new end of the range.

# Notes

A call to unique is typically followed by a call to a container’s erase member function to actually remove elements from the container.

# Example

#include <algorithm>
#include <iostream>
#include <vector>
 
int main()
{
    // a vector containing several duplicate elements
    std::vector<int> v{1, 2, 1, 1, 3, 3, 3, 4, 5, 4};
    auto print = [&](int id)
    {
        std::cout << "@" << id << ": ";
        for (int i : v)
            std::cout << i << ' ';
        std::cout << '\n';
    };
    print(1);
 
    // remove consecutive (adjacent) duplicates
    auto last = std::unique(v.begin(), v.end());
    // v now holds {1 2 1 3 4 5 4 x x x}, where 'x' is indeterminate
    v.erase(last, v.end());
    print(2);
 
    // sort followed by unique, to remove all duplicates
    std::sort(v.begin(), v.end()); // {1 1 2 3 4 4 5}
    print(3);
 
    last = std::unique(v.begin(), v.end());
    // v now holds {1 2 3 4 5 x x}, where 'x' is indeterminate
    v.erase(last, v.end());
    print(4);
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 202C++98the behavior was unclear if the elements arecompared using a non-equivalence relationthe behavior isundefined in this case

# See also