std::ranges:: unique
| ヘッダー <algorithm> で定義 |
||
| 呼び出しシグネチャ |
||
| template< std::permutable I, std::sentinel_for<I> S, class Proj = std::identity, std::indirect_equivalence_relation<std::projected<I, Proj>> |
(1) | (C++20以降) |
| template< ranges::forward_range R, class Proj = std::identity, std::indirect_equivalence_relation<std::projected<ranges::iterator_t<R>, Proj>> |
(2) | (C++20以降) |
[first, last) の連続する同値な要素の各グループから、最初の要素を除くすべてを削除し、範囲[ret, last) を返します。ここでretは、範囲の新しい終端を示す過去のイテレータです。*(i - 1)と*iは、範囲[first + 1, last) 内のイテレータiに対して、std::invoke(comp, std::invoke(proj, *(i - 1)), std::invoke(proj, *i)) == true の場合に同値とみなされます。r を使用し、まるで first として ranges::begin(r)、last として ranges::end(r) を使用しているかのようです。このページで説明されている関数のようなエンティティは、アルゴリズム関数オブジェクト(非公式にはニーブロイドとして知られている)です。つまり、
- これらのいずれかを呼び出す際に、明示的なテンプレート引数リストを指定することはできません。
- これらのいずれも実引数依存の名前探索には見えません。
- これらのいずれかが関数呼び出し演算子の左側の名前として通常の非修飾名探索によって見つかった場合、実引数依存の名前探索は抑制されます。
目次 |
[編集] パラメータ
| first, last | - | 処理する要素の範囲を定義するイテレータとセンチネルのペア |
| r | - | 処理する要素の範囲 |
| comp | - | 射影された要素を比較するための二項述語 |
| proj | - | 要素に適用される射影 |
[編集] 戻り値
範囲の新しい終端を示す過去のイテレータretからなる{ret, last}を返します。
[編集] 計算量
空でない範囲の場合、対応する述語compの適用回数はちょうどranges::distance(first, last) - 1回、射影projの適用回数は最大でもその2倍です。
[編集] 注意
削除は、範囲内の要素を移動代入によって、削除されない要素が範囲の先頭に来るようにシフトすることで行われます。残る要素の相対的な順序は保持され、コンテナの**物理的な**サイズは変更されません。範囲[ret, last)(もしあれば)内のイテレータは依然としてデリファレンス可能ですが、要素自体の値は未規定となります(MoveAssignableの事後条件による)。
ranges::uniqueの呼び出しの後、コンテナのeraseメンバ関数が呼び出され、未規定の値を持つ要素が削除され、コンテナの**物理的な**サイズが新しい**論理的な**サイズに一致するように縮小されることがよくあります。この2つの呼び出しを組み合わせることで、**Erase–remove** イディオムがモデル化されます。
[編集] 実装例
struct unique_fn { template<std::permutable I, std::sentinel_for<I> S, class Proj = std::identity, std::indirect_equivalence_relation<std::projected<I, Proj>> C = ranges::equal_to> constexpr ranges::subrange<I> operator()(I first, S last, C comp = {}, Proj proj = {}) const { first = ranges::adjacent_find(first, last, comp, proj); if (first == last) return {first, first}; auto i {first}; ++first; while (++first != last) if (!std::invoke(comp, std::invoke(proj, *i), std::invoke(proj, *first))) *++i = ranges::iter_move(first); return {++i, first}; } template<ranges::forward_range R, class Proj = std::identity, std::indirect_equivalence_relation<std::projected<ranges::iterator_t<R>, Proj>> C = ranges::equal_to> requires std::permutable<ranges::iterator_t<R>> constexpr ranges::borrowed_subrange_t<R> operator()(R&& r, C comp = {}, Proj proj = {}) const { return (*this)(ranges::begin(r), ranges::end(r), std::move(comp), std::move(proj)); } }; inline constexpr unique_fn unique {}; |
[編集] 例
#include <algorithm> #include <cmath> #include <complex> #include <iostream> #include <vector> struct id { int i; explicit id(int i) : i {i} {} }; void print(id i, const auto& v) { std::cout << i.i << ") "; std::ranges::for_each(v, [](auto const& e) { std::cout << e << ' '; }); std::cout << '\n'; } int main() { // a vector containing several duplicated elements std::vector<int> v {1, 2, 1, 1, 3, 3, 3, 4, 5, 4}; print(id {1}, v); // remove consecutive (adjacent) duplicates const auto ret = std::ranges::unique(v); // v now holds {1 2 1 3 4 5 4 x x x}, where 'x' is indeterminate v.erase(ret.begin(), ret.end()); print(id {2}, v); // sort followed by unique, to remove all duplicates std::ranges::sort(v); // {1 1 2 3 4 4 5} print(id {3}, v); const auto [first, last] = std::ranges::unique(v.begin(), v.end()); // v now holds {1 2 3 4 5 x x}, where 'x' is indeterminate v.erase(first, last); print(id {4}, v); // unique with custom comparison and projection std::vector<std::complex<int>> vc { {1, 1}, {-1, 2}, {-2, 3}, {2, 4}, {-3, 5} }; print(id {5}, vc); const auto ret2 = std::ranges::unique(vc, // consider two complex nums equal if their real parts are equal by module: [](int x, int y) { return std::abs(x) == std::abs(y); }, // comp [](std::complex<int> z) { return z.real(); } // proj ); vc.erase(ret2.begin(), ret2.end()); print(id {6}, vc); }
出力
1) 1 2 1 1 3 3 3 4 5 4 2) 1 2 1 3 4 5 4 3) 1 1 2 3 4 4 5 4) 1 2 3 4 5 5) (1,1) (-1,2) (-2,3) (2,4) (-3,5) 6) (1,1) (-2,3) (-3,5)
[編集] 関連項目
| (C++20) |
連続する重複を含まない要素の範囲のコピーを作成する (アルゴリズム関数オブジェクト) |
| (C++20) |
等しい(または指定された述語を満たす)最初の2つの隣接する項目を見つける (アルゴリズム関数オブジェクト) |
| (C++20)(C++20) |
特定の基準を満たす要素を削除する (アルゴリズム関数オブジェクト) |
| 範囲内の連続する重複要素を削除する (関数テンプレート) | |
| 連続する重複した要素を削除する ( std::list<T,Allocator> のパブリックメンバ関数) | |
| 連続する重複した要素を削除する ( std::forward_list<T,Allocator> のパブリックメンバ関数) |