STLalgorithm算法replace,replace_if和replace_copy,replace_copy_if(49)


STLalgorithm算法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

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


发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注