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

std::replace_copy

Defined in header <algorithm>

template< class InputIt, class OutputIt, class T > OutputIt replace_copy( InputIt first, InputIt last, OutputIt d_first, const T& old_value, const T& new_value );

(1)

template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class T > ForwardIt2 replace_copy( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first, const T& old_value, const T& new_value );

(2)

(since C++17)

template< class InputIt, class OutputIt, class UnaryPredicate, class T > OutputIt replace_copy_if( InputIt first, InputIt last, OutputIt d_first, UnaryPredicate p, const T& new_value );

(3)

template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class UnaryPredicate, class T > ForwardIt2 replace_copy_if( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first, UnaryPredicate p, const T& new_value );

(4)

(since C++17)

从范围复制所有元素[first, last)到另一个范围,从d_first将满足特定标准的所有元素替换为new_value.源范围和目标范围不能重叠。

1%29替换所有等于old_value...

3%29替换谓词的所有元素。p回报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

old_value

-

the value of elements to replace

policy

-

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

p

-

unary predicate which returns ​true if the element value should be replaced. 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. ​

new_value

-

the value to use as replacement

类型要求

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

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

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

返回值

Iterator到元素的最后一个复制元素。

复杂性

一点儿没错last - first谓词的应用。

例外

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

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

可能的实施

第一版

*。

模板<类InputIt,类OutputIt,class T>OutputIt替换[医]复制%28 InputIt First,InputIt Lest,OutputIt d[医]首先,康斯特·T&奥尔德[医]价值,康斯特T&新[医]值%29{表示%28;第一%21=最后;++第一%29{%2A丁[医]第一++=%28%2A第一=老[医]价值%29?新[医]价值:%2A第一;}返回d[医]第一;}

第二版

模板<类Inputit,类OutputIt,类UnaryPredicate,类T>OutputIt替换[医]复制[医]如果%28首先输入,最后输入,输出输出d[医]第一,单预言p,Const T&New[医]值%29{表示%28;第一%21=最后;++第一%29{%2A丁[医]第一++=p%28%2A第一%29?新[医]价值:%2A第一;}返回d[医]第一;}

下面的副本打印一个向量,将5以上的所有值替换为99。

二次

代码语言:javascript
复制
#include <algorithm>
#include <vector>
#include <iostream>
#include <iterator>
#include <functional>
 
int main()
{
    std::vector<int> v{5, 7, 4, 2, 8, 6, 1, 9, 0, 3};
    std::replace_copy_if(v.begin(), v.end(),
                         std::ostream_iterator<int>(std::cout, " "),
                         [](int n){return n > 5;}, 99);
    std::cout << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
5 99 4 2 99 99 1 99 0 3

二次

另见

removeremove_if

removes elements satisfying specific criteria (function template)

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

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

扫码关注腾讯云开发者

领取腾讯云代金券