Skip to content
Related Articles

Related Articles

Improve Article
std::string::replace_copy(), std::string::replace_copy_if in C++
  • Last Updated : 28 Oct, 2020

replace_copy

replace_copy() is a combination of copy() and replace(). 

  • It copies the elements in the range [first, last) to the range beginning at result, replacing the appearances of old_value by new_value.
  • The range copied is [first, last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
  • The ranges shall not overlap in such a way that result points to an element in the range [first, last).
  • The function uses operator== to compare the individual elements to old_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); 
first, last : Input iterators to the initial and final positions 
in a sequence. 
old_value : Value to be replaced.
new_value : Replacement value.

Returns the position after the last copied element in the destination
range (the first element that is not overwritten). 

CPP




// CPP to illustrate
// replace_copy
 
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
 
// Function to replace 'A' at v.begin() with Z and copy it
// to v.begin() position
void replace_copyDemo(vector<char>& v)
{
    replace_copy(v.begin(), v.begin()+1,
                    v.begin(), 'A', 'Z' );
}
 
// Function to print content of vector
void print(vector<char>& v)
{
    int len = v.size();
    for (int i = 0; i < len; i++)
        cout << v[i] << " ";
    cout << endl;
}
 
// Driver code
int main()
{
 
    vector<char> v;
 
    for (int i = 0; i <= 6; i++)
        v.push_back('A' + i);
    cout << "Before replace_copy " <<": ";
    print(v);
    replace_copyDemo(v);
     
    cout << "After replace_copy " << ": ";
    print(v);
 
    return 0;
}

Output: 

Before replace_copy : A B C D E F G 
After replace_copy : Z B C D E F G 

replace_copy_if

replace_copy_if() is a combination of copy() and replace_if().  

  • Copies the elements in the range [first, last) to the range beginning at result, replacing those for which pred returns true by new_value.
  • In simpler terms, it Copies the elements of a sequence to another same-size sequence replacing any elements that satisfies a predicate, with another value.
  • Returns the position after the last copied element in the destination range (the first element that is not overwritten). 
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 : Input iterators to the initial and final positions 
in a sequence. 
old_value : Value to be replaced.
new_value : Replacement value.
pred : Unary function that accepts an element in the range as argument, 
and returns a value convertible to bool. The value returned indicates whether the 
element is to be replaced in the copy (if true, it is replaced).

CPP




// CPP code to illustrate
// replace_copy_if
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
 
// Function to check if number is even
int IsEven(int i)
{
    return ((i % 2) == 0);
}
 
// Function to print content of vector
void print(vector<int>& v)
{
    int len = v.size();
    for (int i = 0; i < len; i++)
        cout << v[i] << " ";
    cout << endl;
}
 
// Function to replace all
// even numbers from vector v1 and
// copying them to v2
void replace_copy_ifDemo(vector<int>& v1,
                            vector<int>& v2)
{
    replace_copy_if(v1.begin(), v1.end(),
                       v2.begin(), IsEven, 0);
}
 
// Driver Code
int main()
{
    vector<int> v1, v2;
 
    for (int i = 1; i <= 10; i++)
        v1.push_back(i);
 
    cout << "Before replace_copy_if : ";
    print(v1);
 
    v2.resize(v1.size()); // allocate space
    replace_copy_ifDemo(v1, v2);
 
    cout << "After replace_copy_if : ";
    print(v2);
 
    return 0;
}

Output: 

Before replace_copy_if : 1 2 3 4 5 6 7 8 9 10 
After replace_copy_if : 1 0 3 0 5 0 7 0 9 0 

NOTE: Both algorithms i.e. replace_copy and replace_copy_if, returns the position after the last copied element in the destination range (the first element that is not overwritten).
Related Article : std::string::replace, std::string::replace_if
This article is contributed by Sakshi Tiwari. If you like GeeksforGeeks(We know you do!) and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :