STL algorithm算法replace,replace_if和replace_copy,replace_copy_if(49)

replace原型:

std::replace

template <class ForwardIterator, class T>
  void replace (ForwardIterator first, ForwardIterator last,
                const T& old_value, const T& new_value);
将范围内的某个值替换成一个新值。

行为类似于:

template <class ForwardIterator, class T>
  void replace (ForwardIterator first, ForwardIterator last,
                const T& old_value, const T& new_value)
{
  while (first!=last) {
    if (*first == old_value) *first=new_value;
    ++first;
  }
}


replace_if原型:

std::replace_if

template <class ForwardIterator, class UnaryPredicate, class T>
  void replace_if (ForwardIterator first, ForwardIterator last,
                   UnaryPredicate pred, const T& new_value );
将pred返回值为true的元素的值替换成new_value

行为类似于:

template < class ForwardIterator, class UnaryPredicate, class T >
  void replace_if (ForwardIterator first, ForwardIterator last,
                   UnaryPredicate pred, const T& new_value)
{
  while (first!=last) {
    if (pred(*first)) *first=new_value;
    ++first;
  }
}


两者合成一个简单的例子:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void replaceif()
{
    vector<int> vi{1,2,3,4,4,6};
    vector<int> v2{1,2,3,4,5,6};
    cout<<"vi=";
    for(int i:vi)
        cout<<i<<" ";
    cout<<endl;
    cout<<"v2=";
    for(int i:v2)
        cout<<i<<" ";
    cout<<endl;
    cout<<"vi.size()="<<vi.size()<<endl;
    cout<<"v2.size()="<<v2.size()<<endl;
    replace(vi.begin(),vi.end(),4,888);
    cout<<"after     replace(vi.begin(),vi.end(),4,888);"<<endl;
    cout<<"vi=";
    for(int i:vi)
        cout<<i<<" ";
    cout<<endl;
    replace_if(v2.begin(),v2.end(),[](int n){return n%2==0;},999);
    cout<<"after replace_if(v2.begin(),v2.end(),[](int n){return n%2==0;},999);"<<endl;
    cout<<"v2=";
    for(int i:v2)
        cout<<i<<" ";
    cout<<endl;
    cout<<"vi.size()="<<vi.size()<<endl;
    cout<<"v2.size()="<<v2.size()<<endl;

}

运行截图:






replace_copy原型:

std::replace_copy

template <class InputIterator, class OutputIterator, class T>
  OutputIterator replace_copy (InputIterator first, InputIterator last,
                               OutputIterator result,
                               const T& old_value, const T& new_value);
将范围[first,last)内的元素逐一复制到result开始的位置,其中如果有元素值为old_valus,则将其值替换成new_value.

返回值为最后一个被覆盖元素的下一个位置的迭代器。

其行为类似于:

template <class InputIterator, class OutputIterator, class T>
  OutputIterator replace_copy (InputIterator first, InputIterator last,
                               OutputIterator result, const T& old_value, const T& new_value)
{
  while (first!=last) {
    *result = (*first==old_value)? new_value: *first;
    ++first; ++result;
  }
  return result;
}


replace_copy_if原型:

std::replace_copy_if

template <class InputIterator, class OutputIterator, class UnaryPredicate, class T>
  OutputIterator replace_copy_if (InputIterator first, InputIterator last,
                                  OutputIterator result, UnaryPredicate pred,
                                  const T& new_value);
将范围[first,last)内的元素逐一复制到result开始的位置,其中如果有元素的pred返回值为ture,则将其值替换成new_value.

返回值为最后一个被覆盖元素的下一个位置的迭代器。

其行为类似于:

template <class InputIterator, class OutputIterator, class UnaryPredicate, class T>
  OutputIterator replace_copy_if (InputIterator first, InputIterator last,
                                  OutputIterator result, UnaryPredicate pred,
                                  const T& new_value)
{
  while (first!=last) {
    *result = (pred(*first))? new_value: *first;
    ++first; ++result;
  }
  return result;
}


两者合成一个简单的例子:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void replacecopyif()
{
    vector<int> vi{1,2,3,4,4,6};
    vector<int> vresult1(6);
    vector<int> vresult2(6);
    cout<<"vi=";
    for(int i:vi)
        cout<<i<<" ";
    cout<<endl;
    cout<<"vreult1=";
    for(int i:vresult1)
        cout<<i<<" ";
    cout<<endl;
    cout<<"vreult2=";
    for(int i:vresult2)
        cout<<i<<" ";
    cout<<endl;
    replace_copy(vi.begin(),vi.end(),vresult1.begin(),4,888);
    cout<<"after     replace(vi.begin(),vi.end(),vresult1.begin(),4,888);"<<endl;
    cout<<"vresult1=";
    for(int i:vresult1)
        cout<<i<<" ";
    cout<<endl;
    replace_copy_if(vi.begin(),vi.end(),vresult2.begin(),[](int n){return n%2==0;},999);
    cout<<"after replace_if(v2.begin(),v2.end(),vresult2.begin(),[](int n){return n%2==0;},999);"<<endl;
    cout<<"vresult2=";
    for(int i:vresult2)
        cout<<i<<" ";
    cout<<endl;
}


运行截图:





——————————————————————————————————————————————————————————————————

//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。

转载请注明出处:http://blog.csdn.net/qq844352155

author:天下无双

Email:coderguang@gmail.com

2014-9-25

于GDUT

—————————————————————————————————————————————————








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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值