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

std::copy_if

Defined in header <algorithm>

?

?

template< class InputIt, class OutputIt > OutputIt copy( InputIt first, InputIt last, OutputIt d_first );

(1)

?

template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > ForwardIt2 copy( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first );

(2)

(since C++17)

template< class InputIt, class OutputIt, class UnaryPredicate > OutputIt copy_if( InputIt first, InputIt last, OutputIt d_first, UnaryPredicate pred );

(3)

(since C++11)

template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class UnaryPredicate > ForwardIt2 copy_if( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first, UnaryPredicate pred );

(4)

(since C++17)

复制范围中的元素,[first, last),到另一个范围,从d_first...

1%29拷贝范围内的所有元素[first, last)如果d_first在范围内[first, last).在这种情况下,std::copy_backward可能会被使用。

3%29只复制谓词所使用的元素。pred回报true.保留未删除的元素的顺序。如果源和目标范围重叠,则行为未定义。

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

参数

first, last

-

the range of elements to copy

d_first

-

the beginning of the destination range.

policy

-

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

pred

-

unary predicate which returns ?true for the required elements. 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. ?

类型要求

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

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

-前进1,前进2必须符合先行者的要求。

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

返回值

将迭代器输出到目标范围内的元素,其中一次超过复制的最后一个元素。

复杂性

1-2%29last - first任务。

3-4%29last - first谓词的应用。

对于执行策略的重载,如果ForwardIt1%27S值[医]类型不是MoveConstructible...

例外

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

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

注记

在实践中,std::copy避免多个外派,并使用大容量复制函数,如std::memmove如果值类型为TriviallyCopyable...

复制重叠范围时,std::copy当复制到目标范围的左方向%28时,目标范围的开头位于源范围%29之外,而std::copy_backward当将目标范围的%28端复制到源范围%29以外的右侧时,则是适当的。

可能的实施

第一版

*。

模板<类InputIt,class OutputIt>OutputIt复制%28 InputIt First,InputIt Lest,OutputIt d[医]第一%29{而%281%21=最后%29{%2A丁[医]第一++=%2A第一++;}返回d[医]第一;}

第二版

模板<类Inputit,类OutputIt,类UnaryPredicate>OutputIt复制[医]如果%28首先输入,最后输入,输出输出d[医]第一,单预言pred%29{而%281%21=最后%29{if%28 pred%28。%2A第一%29%29%2A丁[医]第一++=%2A首先;第一++;}返回d[医]第一;}

以下代码使用Copy将一个向量的内容复制到另一个向量中,并显示结果向量:

二次

代码语言:javascript
复制
#include <algorithm>
#include <iostream>
#include <vector>
#include <iterator>
#include <numeric>
 
int main()
{
    std::vector<int> from_vector(10);
    std::iota(from_vector.begin(), from_vector.end(), 0);
 
    std::vector<int> to_vector;
    std::copy(from_vector.begin(), from_vector.end(),
              std::back_inserter(to_vector));
// or, alternatively,
//  std::vector<int> to_vector(from_vector.size());
//  std::copy(from_vector.begin(), from_vector.end(), to_vector.begin());
// either way is equivalent to
//  std::vector<int> to_vector = from_vector;
 
    std::cout << "to_vector contains: ";
 
    std::copy(to_vector.begin(), to_vector.end(),
              std::ostream_iterator<int>(std::cout, " "));
    std::cout << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
to_vector contains: 0 1 2 3 4 5 6 7 8 9

二次

另见

copy_backward

copies a range of elements in backwards order (function template)

reverse_copy

creates a copy of a range that is reversed (function template)

copy_n (C++11)

copies a number of elements to a new location (function template)

fill

copy-assigns the given value to every element in a range (function template)

remove_copyremove_copy_if

copies a range of elements omitting those that satisfy specific criteria (function template)

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

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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com