变动性算法modifying algorithms

目的:变动内容

注意:目标区间不能是关联式容器

所有具有单一目标区间的算法,都返回最后一个被复制元素的下一个位置

一。复制

1.OutputIterator copy(InputIterator sourceBegin, InputIterator sourceEnd, OutputIterator destBegin);

2.BidirectionalIterator1 copy_backward(BidirectionalIterator1 sourceBegin, BidirectionalIterator1 sourceEnd,

                                                               BidirectionalIterator2 destEnd);

copy正向遍历,copy_backward逆向遍历

注意:当源区间和目标区间重复时,1.复制到前端,则用copy,2. 复制到后端,用copy_backward

逆向遍历:reverse_copy();

二。转换

1.OutputIterator transform(InputIterator sourceBegin, InputIterator sourceEnd, OutIterator destBegin,

                                           UnaryFunc op);                              //op(elem);返回最后一个被转换的元素的下一个位置

   OutputIterator transform(InputIterator1 source1Begin, InputIterator1 source1End,

                                            InputIterator2 source2Begin, OutputIterator destBegin, BinaryFunc op);

   // op(source1Elem,source2Elem);         返回最后一个被转换元素的下一个元素

   确保目标区间有足够空间,或用插入迭代器。

三。互换

1.ForwardIterator2 swap_ranges(ForwardIterator1 begin1,ForwardIterator1 end1,ForwardIterator2 begin2);

             //两区间不能不能重叠

这个函数主要用于指定区间交换,swap()主要是全部元素交换

四。赋值

1.void fill(ForwardIterator begin,ForwardIterator end, const T&value);          //[begin,end)赋予value

2.void fill_n(OutputIterator begin,Size num, const T&value);  //从begin开始前num个元素赋予value

3.void generatr(ForwardIterator begin, ForwardIterator end, Func op);   //op();产生新值给[begin,end);

4.void generate_n(OutputIterator begin, Size num, Func op); //op()产生num个新值给begin开始的区间

五。替换

1.void replace(ForwardIterator begin, ForwardIterator end, const T&oldValue, const T&newValue);

  //把[begin,end)的oldValue值替换为newValue

2.void replace_if(ForwardIterator  begin, ForwardIterator end, UnaryPredicate op, const T&newValue);

//op(elem)为true的元素替换为newValue

3.OutputIterator replace_copy(InputIterator sourceBegin, InputIterator sourceEnd, OutputIterator destBegin,

                                                 const T&oldValue, const T&newValue);

   replace_copy 是copy再replace。

4.OutputIterator replace_copy_if(InputIterator sourceBegin, InputIterator sourceEnd, OutputIterator destBegin,

                                                     UnaryPredicate op, const T&newValue);

  replace_copy_if是copy和replace_if的结合。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值