GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • cpprefjp - C++日本語リファレンス
  • GitHub Project

This view

  • Original page
  • New address

GeezerVison is showing cpprefjp.github.io — std::ranges::unique - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. algorithm
  3. unique

最終更新日時: 2026年09月11日 13時06分31秒 (JST)
Akira Takahashi が更新

履歴 編集

function template
<algorithm>

std::ranges::unique(C++20)

namespace std::ranges {
  template <permutable I,
            sentinel_for<I> S,
            class Proj = identity,
            indirect_equivalence_relation<projected<I, Proj>> C = ranges::equal_to>
  constexpr subrange<I>
    unique(I first,
           S last,
           C comp = {},
           Proj proj = {}); // (1) C++20

  template <forward_range R,
            class Proj = identity,
            indirect_equivalence_relation<projected<iterator_t<R>, Proj>> C = ranges::equal_to>
    requires permutable<iterator_t<R>>
  constexpr borrowed_subrange_t<R>
    unique(R&& r,
           C comp = {},
           Proj proj = {}); // (2) C++20

  template <execution-policy Ep,
            random_access_iterator I,
            sized_sentinel_for<I> S,
            class Proj = identity,
            indirect_equivalence_relation<projected<I, Proj>> C = ranges::equal_to>
    requires permutable<I>
  subrange<I>
    unique(Ep&& exec,
           I first,
           S last,
           C comp = {},
           Proj proj = {}); // (3) C++26

  template <execution-policy Ep,
            sized-random-access-range R,
            class Proj = identity,
            indirect_equivalence_relation<projected<iterator_t<R>, Proj>> C = ranges::equal_to>
    requires permutable<iterator_t<R>>
  borrowed_subrange_t<R>
    unique(Ep&& exec,
           R&& r,
           C comp = {},
           Proj proj = {}); // (4) C++26
}

概要

重複した要素を除ける。

  • (1): イテレータ範囲を指定する
  • (2): Rangeを直接指定する
  • (3): (1)の並列アルゴリズム版。実行ポリシーを指定する
  • (4): (2)の並列アルゴリズム版。実行ポリシーを指定する

この関数は、隣り合った重複要素を除いた要素を、範囲の先頭に集める。この関数によってコンテナから直接要素が削除され、コンテナの要素数が減るようなことはない。コンテナから実際に要素を削除する場合は、この関数の戻り値として、先頭に集められた重複なし範囲の末尾の次を指すイテレータが返るため、そのイテレータを介してコンテナのerase()メンバ関数などを呼び出し、削除を行うこと。

この関数の戻り値として返されるイテレータ以降の値は未規定。

効果

[first,last) が空の範囲でない場合、[first + 1,last) 内のイテレータ i について、

  • (1) では*(i - 1) == *i
  • (2) ではpred(*(i - 1), *i) != false

による等値の比較によって連続したグループに分け、それぞれのグループの先頭以外を取り除く。

戻り値

jを重複を除いた範囲の末尾の次を指すイテレータとするとき、{j, last}

計算量

[first,last) が空の範囲でない場合、正確に last - first - 1 回の比較または述語の適用を行う

例

基本的な使い方

#include <algorithm>
#include <iostream>
#include <vector>

void print(const char* tag, const std::vector<int>& v) {
  std::cout << tag << " : ";
  bool first = true;
  for (int x : v) {
    if (first) {
      first = false;
    }
    else {
      std::cout << ',';
    }
    std::cout << x;
  }
  std::cout << std::endl;
}

int main() {
  // 入力の配列がソート済みではない場合、
  // 隣り合った重複要素が取り除かれる
  {
    std::vector<int> v = {2, 5, 3, 3, 1, 2, 4, 2, 1, 1, 4, 4, 3, 3, 3};

    auto result = std::ranges::unique(v);

    // [v.begin(), result.begin())の範囲に、重複を除いた結果が入っている。
    // 不要になった要素を削除
    v.erase(result.begin(), result.end());

    ::print("unsorted unique", v);
  }

  // 入力の配列がソート済みである場合、
  // 重複している全ての要素が取り除かれて一意になる
  {
    std::vector<int> v = {2, 5, 3, 3, 1, 2, 4, 2, 1, 1, 4, 4, 3, 3, 3};

    std::sort(v.begin(), v.end());
    auto result = std::ranges::unique(v);

    // 不要になった要素を削除
    v.erase(result.begin(), result.end());

    ::print("sorted unique", v);
  }
}

出力

unsorted unique : 2,5,3,1,2,4,2,1,4,3
sorted unique : 1,2,3,4,5

並列アルゴリズムの例 (C++26)

#include <algorithm>
#include <iostream>
#include <vector>
#include <execution>

int main() {
  std::vector<int> v = {1, 1, 2, 3, 3, 3, 4, 4, 5};

  // 並列に隣り合った重複要素を除去する
  auto result = std::ranges::unique(std::execution::par, v);
  v.erase(result.begin(), result.end());

  for (int x : v) {
    std::cout << x << ' ';
  }
  std::cout << std::endl;
}

出力

1 2 3 4 5

バージョン

言語

  • C++20

処理系

  • Clang: ??
  • GCC: 10.1.0 ✅
  • Visual C++: 2019 Update 10 ✅

参照

  • N4861 25 Algorithms library
  • P3179R9 C++ parallel range algorithms
  • LWG Issue 3186. Ranges remove, partition, and partial_sort_copy algorithms discard useful information
    • C++20で、これらのアルゴリズムが処理後の終端イテレータ等の有用な情報を返すよう、戻り値がsubrangeや結果型に変更された
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.