std::ranges::partition_copy, std::ranges::partition_copy_result
ja.cppreference.net より
| ヘッダ <algorithm>
|
||
| 呼び出しシグネチャ |
||
template< std::input_iterator I, std::sentinel_for<I> S,
std::weakly_incrementable O1, std::weakly_incrementable O2,
class Proj = std::identity,
std::indirect_unary_predicate<std::projected<I, Proj>> Pred >
requires std::indirectly_copyable<I, O1> &&
std::indirectly_copyable<I, O2>
constexpr partition_copy_result<I, O1, O2>
partition_copy( I first, S last, O1 out_true, O2 out_false,
Pred pred, Proj proj = {} );
|
(1) | (C++20 から) |
template< ranges::input_range R,
std::weakly_incrementable O1, std::weakly_incrementable O2,
class Proj = std::identity,
std::indirect_unary_predicate<std::projected<iterator_t<R>, Proj>> Pred >
requires std::indirectly_copyable<ranges::iterator_t<R>, O1> &&
std::indirectly_copyable<ranges::iterator_t<R>, O2>
constexpr partition_copy_result<ranges::borrowed_iterator_t<R>, O1, O2>
partition_copy( R&& r, O1 out_true, O2 out_false,
Pred pred, Proj proj = {} );
|
(2) | (C++20 から) |
| ヘルパー型 |
||
template< class I, class O1, class O2 >
using partition_copy_result = ranges::in_out_out_result<I, O1, O2>;
|
(3) | (C++20 から) |
1) は、入力範囲
[first, last) の要素を、述語 pred が返す値に応じて2つの異なる出力範囲にコピーします。述語 pred を射影 proj を適用した後に満たす要素は、範囲 out_true にコピーされます。残りの要素は範囲 out_false にコピーされます。2) (1) と同じですが、
r をソース範囲として使用し、ranges::begin(r) を first として、ranges::end(r) を last として使用します。このページで説明されている関数のようなエンティティは、アルゴリズム関数オブジェクト(非公式には niebloids として知られています)です。すなわち:
- 明示的なテンプレート引数リストは、それらを呼び出すときに指定できません。
- それらは 実引数依存の名前探索 からは見えません。
- それらのいずれかが 通常の非修飾名前探索 によって関数呼び出し演算子の左側の名前として見つかった場合、実引数依存の名前探索 は抑制されます。
パラメータ
| first, last | - | コピー元の要素を定義するイテレータ-センチネルペア range |
| r | - | コピー元の要素のソース範囲 |
| out_true | - | pred を満たす要素の出力範囲の先頭 |
| out_false | - | pred を満たさない要素の出力範囲の先頭 |
| pred | - | 投影された要素に適用する述語 |
| proj | - | 要素に適用する投影 |
戻り値
{
last, o1, o2
}
、ここで
o1
と
o2
はそれぞれコピー完了後の出力範囲の終端を表します。
計算量
対応する述語 comp および任意の射影 proj の適用回数は、厳密に ranges:: distance ( first, last ) 回です。
実装例
struct partition_copy_fn { template<std::input_iterator I, std::sentinel_for<I> S, std::weakly_incrementable O1, std::weakly_incrementable O2, class Proj = std::identity, std::indirect_unary_predicate< std::projected<I, Proj>> Pred> requires std::indirectly_copyable<I, O1> && std::indirectly_copyable<I, O2> constexpr ranges::partition_copy_result<I, O1, O2> operator()(I first, S last, O1 out_true, O2 out_false, Pred pred, Proj proj = {}) const { for (; first != last; ++first) if (!!std::invoke(pred, std::invoke(proj, *first))) *out_true = *first, ++out_true; else *out_false = *first, ++out_false; return {std::move(first), std::move(out_true), std::move(out_false)}; } template<ranges::input_range R, std::weakly_incrementable O1, std::weakly_incrementable O2, class Proj = std::identity, std::indirect_unary_predicate<std::projected<iterator_t<R>, Proj>> Pred> requires std::indirectly_copyable<ranges::iterator_t<R>, O1> && std::indirectly_copyable<ranges::iterator_t<R>, O2> constexpr ranges::partition_copy_result<ranges::borrowed_iterator_t<R>, O1, O2> operator()(R&& r, O1 out_true, O2 out_false, Pred pred, Proj proj = {}) const { return (*this)(ranges::begin(r), ranges::end(r), std::move(out_true), std::move(out_false), std::move(pred), std::move(proj)); } }; inline constexpr partition_copy_fn partition_copy {}; |
例
このコードを実行
#include <algorithm> #include <cctype> #include <iostream> #include <iterator> #include <vector> int main() { const auto in = {'N', '3', 'U', 'M', '1', 'B', '4', 'E', '1', '5', 'R', '9'}; std::vector<int> o1(size(in)), o2(size(in)); auto pred = [](char c) { return std::isalpha(c); }; auto ret = std::ranges::partition_copy(in, o1.begin(), o2.begin(), pred); std::ostream_iterator<char> cout {std::cout, " "}; std::cout << "in = "; std::ranges::copy(in, cout); std::cout << "\no1 = "; std::copy(o1.begin(), ret.out1, cout); std::cout << "\no2 = "; std::copy(o2.begin(), ret.out2, cout); std::cout << '\n'; }
出力:
in = N 3 U M 1 B 4 E 1 5 R 9 o1 = N U M B E R o2 = 3 1 4 1 5 9
関連項目
(C++20) |
要素の範囲を2つのグループに分割する (アルゴリズム関数オブジェクト) |
(C++20) |
要素を2つのグループに分割し、各グループ内での相対的な順序を保持する (アルゴリズム関数オブジェクト) |
(C++20)(C++20) |
要素の範囲を新しい場所にコピーする (アルゴリズム関数オブジェクト) |
(C++20)(C++20) |
特定の基準を満たす要素を省いて範囲をコピーする (アルゴリズム関数オブジェクト) |
(C++11) |
要素の範囲を2つのグループに分割しながらコピーする (関数テンプレート) |