首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::partition_copy

Defined in header <algorithm>

?

?

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 );

(1)

(since C++11)

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 );

(2)

(since C++17)

1%29拷贝范围内的元素[first, last)根据谓词返回的值,到两个不同的范围。p满足谓词的元素p,则复制到从d_first_true其余的元素被复制到范围中,从d_first_false...

如果输入范围与输出范围中的任何一个重叠,则该行为是未定义的。

2%29与%281%29相同,但根据policy。此重载只参与以下情况下的过载解决方案:std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>是真的

参数

first, last

-

the range of elements to sort

d_first_true

-

the beginning of the output range for the elements that satisfy p

d_first_false

-

the beginning of the output range for the elements that do not satisfy p

policy

-

the execution policy to use. See execution policy for details.

p

-

unary predicate which returns ?true if the element should be placed in d_first_true. The signature of the predicate function should be equivalent to the following: bool pred(const Type &a); The signature does not need to have const &, but the function must not modify the objects passed to it. The type Type must be such that an object of type InputIt can be dereferenced and then implicitly converted to Type. ?

类型要求

-输入必须符合输入器的要求。

-取消引用投资的类型必须符合CopyAssignable的要求。

-OutputIt 1,OutputIt 2必须符合OutputIterator的要求。

-前进It 1、前进It 2、前进It 3必须符合先行者的要求。ForwardIt1%27s值类型必须是CopyAssignable,可以写到ForwardIt 2和ForwardIt 3,并且可以转换为UnaryPredicate%27s参数类型。

-单数预测必须满足谓词的要求。

返回值

pair从迭代器到d_first_true范围和迭代器到d_first_false范围。

复杂性

一点儿没错distance(first, last)的应用p...

对于执行策略的重载,如果Forwardit%27s值类型不是CopyConstructible...

例外

带有名为ExecutionPolicy报告错误如下:

  • 如果执行作为算法一部分调用的函数,则引发异常ExecutionPolicy是其中之一标准政策,,,std::terminate叫做。对于任何其他人ExecutionPolicy,行为是由实现定义的。
  • 如果算法不能分配内存,std::bad_alloc被扔了。

可能的实施

模板<类Inputit,类OutputIt 1,类OutputIt 2,类UnaryPredicate>std:对<OutputIt 1,OutputIt 2>分区[医]复制%28 InputIt First,InputIt Lest,OutputIt1 d[医]第一[医]真,输出2 d[医]第一[医]错误,单预言p%29{而%281%21=最后%29{if%28p%28%2A第1%29%29{%2A丁[医]第一[医]真=%2A第一;++d[医]第一[医]真;}{%2A丁[医]第一[医]假=%2A第一;++d[医]第一[医]FALSE;}++优先;}返回STD::结对<OutputIt 1,OutputIt 2>%28d[医]第一[医]真,d[医]第一[医]假%29;}

*。

二次

代码语言:javascript
复制
#include <iostream>
#include <algorithm>
#include <utility>
 
int main()
{
    int arr [10] = {1,2,3,4,5,6,7,8,9,10};
    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 i > 5;});
 
    std::cout << "true_arr: ";
    for (auto it = std::begin(true_arr); it != std::end(true_arr); ++it) {
        std::cout << *it << ' ';
    }
    std::cout << '\n'; 
 
    std::cout << "false_arr: ";
    for (auto it = std::begin(false_arr); it != std::end(false_arr); ++it) {
        std::cout << *it << ' ';
    }
    std::cout << '\n'; 
 
    return 0;
 
}

二次

产出:

二次

代码语言:javascript
复制
true_arr: 6 7 8 9 10
false_arr: 1 2 3 4 5

二次

另见

partition

divides a range of elements into two groups (function template)

stable_partition

divides elements into two groups while preserving their relative order (function template)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com