std::ranges:: unique_copy, std::ranges:: unique_copy_result
|
定義済みヘッダー
<algorithm>
|
||
|
呼び出しシグネチャ
|
||
|
template
<
std::
input_iterator
I,
std::
sentinel_for
<
I
>
S,
std::
weakly_incrementable
O,
class
Proj
=
std::
identity
,
|
(1) | (C++20以降) |
|
template
<
ranges::
input_range
R,
std::
weakly_incrementable
O,
class
Proj
=
std::
identity
,
|
(2) | (C++20以降) |
|
ヘルパー型
|
||
|
template
<
class
I,
class
O
>
using unique_copy_result = ranges:: in_out_result < I, O > ; |
(3) | (C++20以降) |
[
first
,
last
)
の要素を、結果範囲の先頭
result
に、連続する等しい要素が存在しないようにコピーします。等しい要素のグループごとに最初の要素のみがコピーされます。
i
は範囲
[
first
+
1
,
last
)
内のイテレータです。
このページで説明されている関数ライクなエンティティは、 アルゴリズム関数オブジェクト (非公式には niebloids として知られる) です。すなわち:
- 明示的なテンプレート引数リストは、いずれかを呼び出す際に指定することはできません。
- いずれも 実引数依存の名前探索 では可視になりません。
- いずれかが関数呼び出し演算子の左側の名前として 通常の非修飾名前探索 によって見つかった場合、 実引数依存の名前探索 は抑制されます。
目次 |
パラメータ
| first, last | - | 処理対象の要素のソース range を定義するイテレータ-センチネルペア |
| r | - | 要素のソース範囲 |
| result | - | 要素の宛先範囲 |
| comp | - | 投影された要素を比較する二項述語 |
| proj | - | 要素に適用する投影 |
戻り値
{ last, result + N }
計算量
正確に N - 1 回の対応する述語 comp の適用と、任意の射影 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
(注:指定された条件に基づき、HTMLタグ・属性は一切翻訳せず、C++固有の用語も翻訳していません。表示されるテキスト部分「std::indirectly_copyable_storable」はC++の概念名であるため、原文のまま保持しています)<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
(注:指定された条件に基づき、HTMLタグ・属性、
|
例
#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), {}, // デフォルト比較子 std::ranges::equal_to [](int x) { return std::abs(x); } // 射影 ); 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つの隣接する要素を検索する
(アルゴリズム関数オブジェクト) |
|
連続する重複を含まない要素範囲のコピーを作成する
(関数テンプレート) |