跳到主要内容

std::partition_copy() 算法

// (1)
template< class InputIt, class OutputIt1,
class OutputIt2, class UnaryPredicate >
constexpr std::pair<OutputIt1, OutputIt2>
partition_copy( InputIt first, InputIt last,
OutputIt1 d_first_true, OutputIt2 d_first_false,
UnaryPredicate p );

// (2)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2,
class ForwardIt3, class UnaryPredicate >
std::pair<ForwardIt2, ForwardIt3>
partition_copy( ExecutionPolicy&& policy,
ForwardIt1 first, ForwardIt1 last,
ForwardIt2 d_first_true, ForwardIt3 d_first_false,
UnaryPredicate p );
  • (1) 根据谓词 p 返回的值,将范围 [first; last) 中的元素复制到两个不同的范围。

    满足谓词 p 的元素被复制到以 d_first_true 开头的范围。其余元素被复制到以 d_first_false 开头的范围。

    未定义行为

    行为未定义

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

  • (2)(1) 相同,但根据 policy 执行。

    重载决议

    这些重载只有在 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>  (直到 C++20) std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>  (自 C++20 起)true 时才参与重载决议。

参数

first
last

要复制的元素范围。

d_first_true

满足 p 的元素的输出范围的开始

d_first_false

满足 p 的元素的输出范围的开始

policy

要使用的执行策略。有关详细信息,请参阅执行策略

p

一元谓词,对所需元素返回 true

对于类型为(可能为 const)VT 的每个参数 v,表达式 p(v) 必须可转换为 bool,其中 VTInputIt 的值类型,无论值类别如何,并且不得修改 v。因此,不允许使用参数类型 VT& ,也不允许使用 VT,除非对于 VT,移动等同于复制。 (自 C++11 起)

类型要求

InputIt旧式输入迭代器 (LegacyInputIterator)
解引用 InputIt可复制赋值 (CopyAssignable)
OutputIt1
OutputIt2
旧式输出迭代器 (LegacyOutputIterator)
ForwardIt1
ForwardIt2
ForwardIt3
旧式前向迭代器 (LegacyForwardIterator)
一元谓词 (UnaryPredicate)谓词 (Predicate)

ForwardIt1::value_type 必须

返回值

一个由指向 d_first_true 范围末尾的迭代器和指向 d_first_false 范围末尾的迭代器构造的 std::pair

复杂度

p 的应用次数恰好为 std::distance(first, last)

对于带有 ExecutionPolicy 的重载,如果 ForwardIt 的值类型不是可复制构造的 (CopyConstructible),则可能会有性能开销。

异常

带有模板参数 ExecutionPolicy 的重载报告错误如下

  • 如果作为算法一部分调用的函数执行抛出异常,并且 ExecutionPolicy标准策略之一,则调用std::terminate。对于任何其他 ExecutionPolicy,行为是实现定义的.
  • 如果算法未能分配内存,则抛出 std::bad_alloc

可能的实现

partition_copy (1)
template<class InputIt, class OutputIt1,
class OutputIt2, class UnaryPredicate>
std::pair<OutputIt1, OutputIt2>
partition_copy(InputIt first, InputIt last,
OutputIt1 d_first_true, OutputIt2 d_first_false,
UnaryPredicate p)
{
for (; first != last; ++first)
{
if (p(*first))
{
*d_first_true = *first;
++d_first_true;
}
else
{
*d_first_false = *first;
++d_first_false;
}
}

return std::pair<OutputIt1, OutputIt2>(d_first_true, d_first_false);
}

示例

Main.cpp
#include <algorithm>
#include <iostream>
#include <utility>

void print(auto rem, auto const& v)
{
for (std::cout << rem; auto const& x : v)
std::cout << x << ' ';
std::cout << '\n';
}

int main()
{
int arr[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int true_arr[5] = {0};
int false_arr[5] = {0};

std::partition_copy(std::begin(arr), std::end(arr),
std::begin(true_arr), std::begin(false_arr),
[](int i) { return 4 < i; });

print("true_arr: ", true_arr);
print("false_arr: ", false_arr);
}
输出
true_arr:  5 6 7 8 9
false_arr: 0 1 2 3 4
本文档源自此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”查看此文档的所有更改。
悬停查看原始许可证。

std::partition_copy() 算法

// (1)
template< class InputIt, class OutputIt1,
class OutputIt2, class UnaryPredicate >
constexpr std::pair<OutputIt1, OutputIt2>
partition_copy( InputIt first, InputIt last,
OutputIt1 d_first_true, OutputIt2 d_first_false,
UnaryPredicate p );

// (2)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2,
class ForwardIt3, class UnaryPredicate >
std::pair<ForwardIt2, ForwardIt3>
partition_copy( ExecutionPolicy&& policy,
ForwardIt1 first, ForwardIt1 last,
ForwardIt2 d_first_true, ForwardIt3 d_first_false,
UnaryPredicate p );
  • (1) 根据谓词 p 返回的值,将范围 [first; last) 中的元素复制到两个不同的范围。

    满足谓词 p 的元素被复制到以 d_first_true 开头的范围。其余元素被复制到以 d_first_false 开头的范围。

    未定义行为

    行为未定义

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

  • (2)(1) 相同,但根据 policy 执行。

    重载决议

    这些重载只有在 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>  (直到 C++20) std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>  (自 C++20 起)true 时才参与重载决议。

参数

first
last

要复制的元素范围。

d_first_true

满足 p 的元素的输出范围的开始

d_first_false

满足 p 的元素的输出范围的开始

policy

要使用的执行策略。有关详细信息,请参阅执行策略

p

一元谓词,对所需元素返回 true

对于类型为(可能为 const)VT 的每个参数 v,表达式 p(v) 必须可转换为 bool,其中 VTInputIt 的值类型,无论值类别如何,并且不得修改 v。因此,不允许使用参数类型 VT& ,也不允许使用 VT,除非对于 VT,移动等同于复制。 (自 C++11 起)

类型要求

InputIt旧式输入迭代器 (LegacyInputIterator)
解引用 InputIt可复制赋值 (CopyAssignable)
OutputIt1
OutputIt2
旧式输出迭代器 (LegacyOutputIterator)
ForwardIt1
ForwardIt2
ForwardIt3
旧式前向迭代器 (LegacyForwardIterator)
一元谓词 (UnaryPredicate)谓词 (Predicate)

ForwardIt1::value_type 必须

返回值

一个由指向 d_first_true 范围末尾的迭代器和指向 d_first_false 范围末尾的迭代器构造的 std::pair

复杂度

p 的应用次数恰好为 std::distance(first, last)

对于带有 ExecutionPolicy 的重载,如果 ForwardIt 的值类型不是可复制构造的 (CopyConstructible),则可能会有性能开销。

异常

带有模板参数 ExecutionPolicy 的重载报告错误如下

  • 如果作为算法一部分调用的函数执行抛出异常,并且 ExecutionPolicy标准策略之一,则调用std::terminate。对于任何其他 ExecutionPolicy,行为是实现定义的.
  • 如果算法未能分配内存,则抛出 std::bad_alloc

可能的实现

partition_copy (1)
template<class InputIt, class OutputIt1,
class OutputIt2, class UnaryPredicate>
std::pair<OutputIt1, OutputIt2>
partition_copy(InputIt first, InputIt last,
OutputIt1 d_first_true, OutputIt2 d_first_false,
UnaryPredicate p)
{
for (; first != last; ++first)
{
if (p(*first))
{
*d_first_true = *first;
++d_first_true;
}
else
{
*d_first_false = *first;
++d_first_false;
}
}

return std::pair<OutputIt1, OutputIt2>(d_first_true, d_first_false);
}

示例

Main.cpp
#include <algorithm>
#include <iostream>
#include <utility>

void print(auto rem, auto const& v)
{
for (std::cout << rem; auto const& x : v)
std::cout << x << ' ';
std::cout << '\n';
}

int main()
{
int arr[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int true_arr[5] = {0};
int false_arr[5] = {0};

std::partition_copy(std::begin(arr), std::end(arr),
std::begin(true_arr), std::begin(false_arr),
[](int i) { return 4 < i; });

print("true_arr: ", true_arr);
print("false_arr: ", false_arr);
}
输出
true_arr:  5 6 7 8 9
false_arr: 0 1 2 3 4
本文档源自此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”查看此文档的所有更改。
悬停查看原始许可证。