- 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