24 Algorithms library [algorithms]

24.6 Mutating sequence operations [alg.modifying.operations]

24.6.9 Unique [alg.unique]

template<class ForwardIterator> constexpr ForwardIterator unique(ForwardIterator first, ForwardIterator last); template<class ExecutionPolicy, class ForwardIterator> ForwardIterator unique(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last); template<class ForwardIterator, class BinaryPredicate> constexpr ForwardIterator unique(ForwardIterator first, ForwardIterator last, BinaryPredicate pred); template<class ExecutionPolicy, class ForwardIterator, class BinaryPredicate> ForwardIterator unique(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last, BinaryPredicate pred); namespace ranges { template<Permutable I, Sentinel<I> S, class Proj = identity, IndirectRelation<projected<I, Proj>> C = ranges::equal_to<>> constexpr I unique(I first, S last, C comp = {}, Proj proj = {}); template<ForwardRange R, class Proj = identity, IndirectRelation<projected<iterator_t<R>, Proj>> C = ranges::equal_to<>> requires Permutable<iterator_t<R>> constexpr safe_iterator_t<R> unique(R&& r, C comp = {}, Proj proj = {}); }
Let pred be equal_­to{} for the overloads with no parameter pred, and let E be
  • bool(pred(*(i - 1), *i)) for the overloads in namespace std, or
  • bool(invoke(comp, invoke(proj, *(i - 1)), invoke(proj, *i))) for the overloads in namespace ranges.
Requires: For the overloads in namepace std, pred shall be an equivalence relation and the type of *first shall meet the Cpp17MoveAssignable requirements (Table 28).
Effects: For a nonempty range, eliminates all but the first element from every consecutive group of equivalent elements referred to by the iterator i in the range [first + 1, last) for which E is true.
Returns: The end of the resulting range.
Complexity: For nonempty ranges, exactly (last - first) - 1 applications of the corresponding predicate and no more than twice as many applications of any projection.
template<class InputIterator, class OutputIterator> constexpr OutputIterator unique_copy(InputIterator first, InputIterator last, OutputIterator result); template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2> ForwardIterator2 unique_copy(ExecutionPolicy&& exec, ForwardIterator1 first, ForwardIterator1 last, ForwardIterator2 result); template<class InputIterator, class OutputIterator, class BinaryPredicate> constexpr OutputIterator unique_copy(InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate pred); template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> ForwardIterator2 unique_copy(ExecutionPolicy&& exec, ForwardIterator1 first, ForwardIterator1 last, ForwardIterator2 result, BinaryPredicate pred); namespace ranges { template<InputIterator I, Sentinel<I> S, WeaklyIncrementable O, class Proj = identity, IndirectRelation<projected<I, Proj>> C = ranges::equal_to<>> requires IndirectlyCopyable<I, O> && (ForwardIterator<I> || (InputIterator<O> && Same<iter_value_t<I>, iter_value_t<O>>) || IndirectlyCopyableStorable<I, O>) constexpr unique_copy_result<I, O> unique_copy(I first, S last, O result, C comp = {}, Proj proj = {}); template<InputRange R, WeaklyIncrementable O, class Proj = identity, IndirectRelation<projected<iterator_t<R>, Proj>> C = ranges::equal_to<>> requires IndirectlyCopyable<iterator_t<R>, O> && (ForwardIterator<iterator_t<R>> || (InputIterator<O> && Same<iter_value_t<iterator_t<R>>, iter_value_t<O>>) || IndirectlyCopyableStorable<iterator_t<R>, O>) constexpr unique_copy_result<safe_iterator_t<R>, O> unique_copy(R&& r, O result, C comp = {}, Proj proj = {}); }
Let pred be equal_­to{} for the overloads in namespace std with no parameter pred, and let E be
  • bool(pred(*i, *(i - 1))) for the overloads in namespace std, or
  • bool(invoke(comp, invoke(proj, *i), invoke(proj, *(i - 1)))) for the overloads in namespace ranges.
Requires:
  • The ranges [first, last) and [result, result+(last-first)) shall not overlap.
  • For the overloads in namespace std:
    • The comparison function shall be an equivalence relation.
    • The expression *result = *first shall be valid.
    • For the overloads with no ExecutionPolicy, let T be the value type of InputIterator.
      If InputIterator meets the Cpp17ForwardIterator requirements, then there are no additional requirements for T.
      Otherwise, if OutputIterator meets the Cpp17ForwardIterator requirements and its value type is the same as T, then T shall meet the Cpp17CopyAssignable (Table 29) requirements.
      Otherwise, T shall meet both the Cpp17CopyConstructible (Table 27) and Cpp17CopyAssignable requirements.
      [Note
      :
      For the overloads with an ExecutionPolicy, there may be a performance cost if the value type of ForwardIterator1 does not meet both the Cpp17CopyConstructible and Cpp17CopyAssignable requirements.
      end note
      ]
Effects: Copies only the first element from every consecutive group of equal elements referred to by the iterator i in the range [first, last) for which E holds.
Returns:
  • result + N for the overloads in namespace std, or
  • {last, result + N} for the overloads in namespace ranges
Complexity: Exactly last - first - 1 applications of the corresponding predicate and no more than twice as many applications of any projection.