Skip to content

Latest commit

 

History

History
82 lines (59 loc) · 1.96 KB

File metadata and controls

82 lines (59 loc) · 1.96 KB

unique

  • list[meta header]
  • std[meta namespace]
  • list[meta class]
  • function[meta id-type]
void unique();                // (1) C++03
size_type unique();           // (1) C++20
constexpr size_type unique(); // (1) C++26

template <class BinaryPredicate>
void unique(BinaryPredicate pred);                // (2) C++03
template <class BinaryPredicate>
size_type unique(BinaryPredicate pred);           // (2) C++20
template <class BinaryPredicate>
constexpr size_type unique(BinaryPredicate pred); // (2) C++26

概要

コンテナから重複した要素を削除する

要件

コンテナがソート済みであること。ソート済みでない場合、この関数の動作は未規定。

効果

イテレータ範囲[first + 1, last)の全てのイテレータiについて、オーバーロードごとに、以下の条件がtrueとなる要素を削除する。

  • (1) : *i == *(i - 1)
  • (2) : pred(*i, *(i - 1))

削除された要素に対するイテレータおよび参照は無効となる。

戻り値

  • (1), (2) :
    • C++03 : なし
    • C++20 : 削除された要素数を返す

例外

  • (1) : 型Tの等値比較が例外を投げない場合、この関数は例外を投げない
  • (2) : predが例外を投げない場合、この関数は例外を投げない

計算量

ちょうど(last - first) - 1回の等値比較、もしくは述語の適用を行う。

#include <iostream>
#include <list>

int main()
{
  std::list<int> ls = {3, 1, 4, 1};

  ls.sort();
  ls.unique();

  for (int x : ls) {
    std::cout << x << std::endl;
  }
}
  • unique()[color ff0000]
  • ls.sort()[link sort.md]

出力

1
3
4

参照