std::ranges::partition_copy, std::ranges::partition_copy_result
定義於標頭檔案 <algorithm> |
||
呼叫簽名 (Call signature) |
||
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 的返回值複製到兩個不同的輸出範圍。經過 proj 投影后滿足謂詞 pred 的元素被複制到始於 out_true 的範圍。其餘元素被複制到始於 out_false 的範圍。如果輸入範圍與任何一個輸出範圍重疊,則行為未定義。本頁描述的類函式實體是 演算法函式物件(非正式地稱為 niebloids),即
目錄 |
[編輯] 引數
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) |
將一個範圍的元素分成兩組 (演算法函式物件) |
(C++20) |
將元素分成兩組,同時保留它們的相對順序 (演算法函式物件) |
(C++20)(C++20) |
將一個範圍的元素複製到一個新位置 (演算法函式物件) |
(C++20)(C++20) |
複製一個範圍的元素,忽略那些滿足特定條件的元素 (演算法函式物件) |
(C++11) |
複製一個範圍,並將元素分成兩組 (函式模板) |