STL algorithm replace, replace_if and replace_copy, replace_copy_if (49)

Source: Internet
Author: User

Replace prototype:

STD: replace
template <class ForwardIterator, class T>  void replace (ForwardIterator first, ForwardIterator last,                const T& old_value, const T& new_value);
Replace a value in the specified range with a new value.

The behavior is similar:

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 prototype:

STD: replace_if
template <class ForwardIterator, class UnaryPredicate, class T>  void replace_if (ForwardIterator first, ForwardIterator last,                   UnaryPredicate pred, const T& new_value );
Replace the value of the element whose Pred return value is true with new_value.

The behavior is similar:

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;  }}


A simple example of combining the two is as follows:

#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;}
Run:






Replace_copy prototype:

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);
Copy the elements in the range [first, last) to the start position of result one by one. If the element value is old_valus, replace the value with new_value.

The returned value is the iterator at the next position of the last element to be overwritten.

The behavior is similar:

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 prototype:

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);
Copy the elements in the range [first, last) to the start position of result one by one. If the PRED returned value of an element is true, replace the value with new_value.

The returned value is the iterator at the next position of the last element to be overwritten.

The behavior is similar:

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;}


A simple example of combining the two is as follows:

#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;}
Run:





------------------------------------------------------------------

// For more instructions on writing errors or poor information, you can leave a message below or click the email address in the upper left corner to send an email to me, pointing out my errors and deficiencies, so that I can modify them, thank you for sharing it.

Reprinted please indicate the source: http://blog.csdn.net/qq844352155

Author: unparalleled

Email: [email protected]

Yu gdut

-------------------------------------------------








STL algorithm replace, replace_if and replace_copy, replace_copy_if (49)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.