std::ranges:: partition_copy, std::ranges:: partition_copy_result
std::ranges
| Non-modifying sequence operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Modifying sequence operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Partitioning operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Sorting operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Binary search operations (on sorted ranges) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Set operations (on sorted ranges) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Heap operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Minimum/maximum operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Permutation operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Fold operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Operations on uninitialized storage | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Return types | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
定义于头文件
<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
的返回值复制到两个不同的输出范围。经过投影函数
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)
|
复制范围并将元素划分为两组
(函数模板) |