跳到主要内容

std::ranges::partition_copy() 算法

// (1)
constexpr partition_copy_result<I, O1, O2>
partition_copy( I first, S last, O1 out_true, O2 out_false,
Pred pred, Proj proj = {} );

// (2)
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 = {} );

参数类型是泛型的,并具有以下约束

  • I - std::input_iterator
  • S - std::sentinel_for<I>
  • O1, O2 - std::weakly_incrementable
  • R - std::ranges::forward_range
  • Pred:
    • (1) - std::indirect_unary_predicate<std::projected<I, Proj>>
    • (2) - std::indirect_unary_predicate<std::projected<ranges::iterator_t<R>, Proj>>
  • Proj - (无)

对于所有重载,Proj 模板参数的默认类型为 std::identity

此外,每个重载都有以下约束

  • (1) - indirectly_copyable<I, O1> && indirectly_copyable<I, O2>
  • (2) - indirectly_copyable<ranges::iterator_t<R>, O1> && indirectly_copyable<ranges::iterator_t<R>, O2>

(为提高可读性,此处省略了 std:: 命名空间)

  • (1) 根据谓词 pred 返回的值,将输入范围 [first; last) 中的元素复制到两个不同的输出范围。

    通过 proj 投影后满足谓词 pred 的元素被复制到以 out_true 开头的范围。
    其余元素被复制到以 out_false 开头的范围。

    未定义行为

    行为未定义

    如果输入范围与任何输出范围重叠。

  • (2)(1) 相同,但使用 r 作为范围,如同使用 ranges::begin(r) 作为 firstranges::end(r) 作为 last

本页描述的函数类实体是niebloids

参数

first
last

要从中复制元素的输入范围。

r

要从中复制元素的输入范围。

out_true

用于满足 pred 的元素的输出范围的开始。

out_false

用于**不**满足 pred 的元素的输出范围的开始。

pred

应用于投影元素的谓词。

proj

要应用于元素的投影。

返回值

{
last,
o1,
o2
}

其中 o1o2 分别是复制完成后输出范围的末尾。

复杂度

精确地 ranges::distance(first, last) 次应用相应的谓词 pred 和任何投影 proj

异常

(无)

可能的实现

partition_copy(1) 和 partition_copy(2)
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 {};

示例

Main.cpp
#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
本文源自此 CppReference 页面。它可能为了改进或编辑偏好而有所更改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。

std::ranges::partition_copy() 算法

// (1)
constexpr partition_copy_result<I, O1, O2>
partition_copy( I first, S last, O1 out_true, O2 out_false,
Pred pred, Proj proj = {} );

// (2)
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 = {} );

参数类型是泛型的,并具有以下约束

  • I - std::input_iterator
  • S - std::sentinel_for<I>
  • O1, O2 - std::weakly_incrementable
  • R - std::ranges::forward_range
  • Pred:
    • (1) - std::indirect_unary_predicate<std::projected<I, Proj>>
    • (2) - std::indirect_unary_predicate<std::projected<ranges::iterator_t<R>, Proj>>
  • Proj - (无)

对于所有重载,Proj 模板参数的默认类型为 std::identity

此外,每个重载都有以下约束

  • (1) - indirectly_copyable<I, O1> && indirectly_copyable<I, O2>
  • (2) - indirectly_copyable<ranges::iterator_t<R>, O1> && indirectly_copyable<ranges::iterator_t<R>, O2>

(为提高可读性,此处省略了 std:: 命名空间)

  • (1) 根据谓词 pred 返回的值,将输入范围 [first; last) 中的元素复制到两个不同的输出范围。

    通过 proj 投影后满足谓词 pred 的元素被复制到以 out_true 开头的范围。
    其余元素被复制到以 out_false 开头的范围。

    未定义行为

    行为未定义

    如果输入范围与任何输出范围重叠。

  • (2)(1) 相同,但使用 r 作为范围,如同使用 ranges::begin(r) 作为 firstranges::end(r) 作为 last

本页描述的函数类实体是niebloids

参数

first
last

要从中复制元素的输入范围。

r

要从中复制元素的输入范围。

out_true

用于满足 pred 的元素的输出范围的开始。

out_false

用于**不**满足 pred 的元素的输出范围的开始。

pred

应用于投影元素的谓词。

proj

要应用于元素的投影。

返回值

{
last,
o1,
o2
}

其中 o1o2 分别是复制完成后输出范围的末尾。

复杂度

精确地 ranges::distance(first, last) 次应用相应的谓词 pred 和任何投影 proj

异常

(无)

可能的实现

partition_copy(1) 和 partition_copy(2)
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 {};

示例

Main.cpp
#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
本文源自此 CppReference 页面。它可能为了改进或编辑偏好而有所更改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。