replace_copy/replace_copy_if

41 篇文章 0 订阅
39 篇文章 2 订阅

replace_copy:这个函数和replace对应,唯一不同的是,它将得到的结果存在另一个容器里面

         // TEMPLATEFUNCTION replace_copy

template<class _InIt,

         class_OutIt,

         class_Ty> inline

         _OutIt _Replace_copy(_InIt _First,_InIt _Last,

                   _OutIt _Dest, const _Ty& _Oldval, const_Ty& _Newval)

         {       // copy replacing each matching _Oldval with _Newval

         for (;_First != _Last; ++_First, ++_Dest)

                   *_Dest = *_First == _Oldval ?_Newval : *_First;

         return(_Dest);

         }

replace_copy_if:这个函数和replace_if对应,唯一不同的是,它将得到的结果存在另一个容器里面

                   //TEMPLATE FUNCTION replace_copy_if

template<class _InIt,

         class_OutIt,

         class_Pr,

         class_Ty> inline

         _OutIt _Replace_copy_if(_InIt _First,_InIt _Last,

                   _OutIt _Dest, _Pr _Pred, const _Ty& _Val)

         {       // copy replacing each satisfying _Pred with _Val

         for (;_First != _Last; ++_First, ++_Dest)

                   *_Dest = _Pred(*_First) ?_Val : *_First;

         return(_Dest);

         }

函数同样也很简单.

举例:

bool isTrue( T _val )

{

         return( _val % 2 ) == 0;

}

int main()

{

         vector<int>vecInt;

         for ( int i = 0;i < 10;++ i)

         {

                   vecInt.push_back( i );

         }

         vector<int>vecDest;

         cout<<"vecIntsource:\n";

         copy(vecInt.begin(),vecInt.end(),ostream_iterator<int>(cout," " ) );

         replace_copy(vecInt.begin(),vecInt.end(),back_inserter( vecDest ),1,2 );

         cout<<"\nvecIntreplace:\n";

         copy(vecDest.begin(),vecDest.end(),ostream_iterator<int>(cout," " ) );

 

         replace_copy_if(vecInt.begin(),vecInt.end(),vecDest.begin(),isTrue<int>,10);

         cout<<"\nvecIntreplace:\n";

         copy(vecDest.begin(),vecDest.end(),ostream_iterator<int>(cout," " ) );

         system( "pause");

         return0;

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`replace_copy`函数和`replace_copy_if`函数都是C++标准库中的算法函数,用于将一个序列中的元素替换成另一个元素。 `replace_copy`函数将一个序列中的所有与指定值相等的元素替换为另一个值,并将替换后的结果存入另一个序列中。函数原型如下: ```cpp 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); ``` 其中,`first`和`last`表示要替换元素的输入序列的起始和终止迭代器,`d_first`表示替换后的输出序列的起始迭代器,`old_value`表示要替换的旧值,`new_value`表示替换后的新值。该函数返回替换后的输出序列的结束迭代器。 `replace_copy_if`函数则将一个序列中满足指定条件的所有元素替换为另一个值,并将替换后的结果存入另一个序列中。函数原型如下: ```cpp 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); ``` 其中,`first`和`last`表示要替换元素的输入序列的起始和终止迭代器,`d_first`表示替换后的输出序列的起始迭代器,`p`表示指定的条件,`new_value`表示替换后的新值。该函数返回替换后的输出序列的结束迭代器。 总的来说,`replace_copy`函数和`replace_copy_if`函数都是非破坏性算法,即它们不会改变输入序列,而是将替换后的结果保存在新的输出序列中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值