std::ranges::partition_copy, std::ranges::partition_copy_result
| ヘッダー <algorithm> で定義 |
||
| 呼び出しシグネチャ |
||
| template< std::input_iterator I, std::sentinel_for<I> S, std::weakly_incrementable O1, std::weakly_incrementable O2, |
(1) | (C++20以降) |
| template< ranges::input_range R, std::weakly_incrementable O1, std::weakly_incrementable O2, |
(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以降) |
[first, last) の要素を、述語 pred から返される値に応じて 2 つの異なる出力範囲にコピーします。pred が proj による射影後に満たされる要素は、out_true で始まる範囲にコピーされます。残りの要素は out_false で始まる範囲にコピーされます。入力範囲がどちらかの出力範囲と重複する場合、動作は未定義です。このページで説明されている関数のようなエンティティは、アルゴリズム関数オブジェクト(非公式にはニーブロイドとして知られている)です。つまり、
- これらのいずれかを呼び出す際に、明示的なテンプレート引数リストを指定することはできません。
- これらのいずれも実引数依存の名前探索には見えません。
- これらのいずれかが関数呼び出し演算子の左側の名前として通常の非修飾名探索によって見つかった場合、実引数依存の名前探索は抑制されます。
目次 |
[編集] パラメータ
| first, last | - | コピー元の要素の範囲を定義するイテレータ・センチネルペア |
| 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つのグループに分割しながら範囲をコピーする (関数テンプレート) |