std::ranges::unique_copy, std::ranges::unique_copy_result
From cppreference.com
1)ソース範囲
[first, last) の要素を、隣接する等しい要素がないように、宛先範囲の result から始まる位置にコピーします。等しい要素の各グループの最初の要素のみがコピーされます。 隣接する2つの要素 *(i - 1) と *i は、std::invoke(comp, std::invoke(proj, *(i - 1)), std::invoke(proj, *i)) == true の場合に等しいと見なされます。ここで、
i は範囲 [first + 1, last) 内のイテレータです。2) (1) と同じですが、範囲
r を使用し、まるで first として ranges::begin(r)、last として ranges::end(r) を使用しているかのようです。このページで説明されている関数のようなエンティティは、アルゴリズム関数オブジェクト(非公式にはニーブロイドとして知られている)です。つまり、
- これらのいずれかを呼び出す際に、明示的なテンプレート引数リストを指定することはできません。
- これらのいずれも実引数依存の名前探索には見えません。
- これらのいずれかが関数呼び出し演算子の左側の名前として通常の非修飾名探索によって見つかった場合、実引数依存の名前探索は抑制されます。
目次 |
[編集] パラメータ
| first, last | - | 処理する要素のソース 範囲を定義するイテレータ-センチネルペア |
| r | - | ソース範囲の要素 |
| 結果 | - | 宛先範囲の要素 |
| comp | - | 射影された要素を比較するための二項述語 |
| proj | - | 要素に適用される射影 |
[編集] 戻り値
{last, result + N}
[編集] 計算量
対応する述語 comp の適用はちょうど N - 1 回、射影 proj の適用は最大でその2倍です。
[編集] 実装例
libstdc++ および MSVC STL (およびサードパーティライブラリ: cmcstl2、NanoRange、range-v3) での実装を参照してください。
struct unique_copy_fn { template<std::input_iterator I, std::sentinel_for<I> S, std::weakly_incrementable O, class Proj = std::identity, std::indirect_equivalence_relation<std::projected<I, Proj>> C = ranges::equal_to> requires std::indirectly_copyable<I, O> && (std::forward_iterator<I> || (std::input_iterator<O> && std::same_as<std::iter_value_t<I>, std::iter_value_t<O>>) || std::indirectly_copyable_storable<I, O>) constexpr ranges::unique_copy_result<I, O> operator()(I first, S last, O result, C comp = {}, Proj proj = {}) const { if (!(first == last)) { std::iter_value_t<I> value = *first; *result = value; ++result; while (!(++first == last)) { auto&& value2 = *first; if (!std::invoke(comp, std::invoke(proj, value2), std::invoke(proj, value))) { value = std::forward<decltype(value2)>(value2); *result = value; ++result; } } } return {std::move(first), std::move(result)}; } template<ranges::input_range R, std::weakly_incrementable O, class Proj = std::identity, std::indirect_equivalence_relation<std::projected<ranges::iterator_t<R>, Proj>> C = ranges::equal_to> requires std::indirectly_copyable<ranges::iterator_t<R>, O> && (std::forward_iterator<ranges::iterator_t<R>> || (std::input_iterator<O> && std::same_as<ranges::range_value_t<R>, std::iter_value_t<O>>) || std::indirectly_copyable_storable<ranges::iterator_t<R>, O>) constexpr ranges::unique_copy_result<ranges::borrowed_iterator_t<R>, O> operator()(R&& r, O result, C comp = {}, Proj proj = {}) const { return (*this)(ranges::begin(r), ranges::end(r), std::move(result), std::move(comp), std::move(proj)); } }; inline constexpr unique_copy_fn unique_copy {}; |
[編集] 例
このコードを実行
#include <algorithm> #include <cmath> #include <iostream> #include <iterator> #include <list> #include <string> #include <type_traits> void print(const auto& rem, const auto& v) { using V = std::remove_cvref_t<decltype(v)>; constexpr bool sep{std::is_same_v<typename V::value_type, int>}; std::cout << rem << std::showpos; for (const auto& e : v) std::cout << e << (sep ? " " : ""); std::cout << '\n'; } int main() { std::string s1{"The string with many spaces!"}; print("s1: ", s1); std::string s2; std::ranges::unique_copy( s1.begin(), s1.end(), std::back_inserter(s2), [](char c1, char c2) { return c1 == ' ' && c2 == ' '; } ); print("s2: ", s2); const auto v1 = {-1, +1, +2, -2, -3, +3, -3}; print("v1: ", v1); std::list<int> v2; std::ranges::unique_copy( v1, std::back_inserter(v2), {}, // default comparator std::ranges::equal_to [](int x) { return std::abs(x); } // projection ); print("v2: ", v2); }
出力
s1: The string with many spaces! s2: The string with many spaces! v1: -1 +1 +2 -2 -3 +3 -3 v2: -1 +2 -3
[編集] 関連項目
| (C++20) |
範囲内の連続する重複要素を削除する (アルゴリズム関数オブジェクト) |
| (C++20)(C++20) |
要素の範囲を新しい場所にコピーする (アルゴリズム関数オブジェクト) |
| (C++20) |
等しい(または指定された述語を満たす)最初の2つの隣接する項目を見つける (アルゴリズム関数オブジェクト) |
| 連続する重複を含まない要素の範囲のコピーを作成する (関数テンプレート) |