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
If you like GeeksforGeeks(We know you do!) and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to [email protected].
Similar Reads
std::string::remove_copy(), std::string::remove_copy_if() in C++ remove_copy() It is an STL function in c++ which is defined in algorithm library. It copies the elements in the range [first, last) to the range beginning at result, except those elements that compare equal to given elements. The resulting range is shorter than [first,last) by as many elements as ma
5 min read
std::replace and std::replace_if in C++ std::replace Assigns new_value to all the elements in the range [first, last) that compare to old_value. The function use operator == to compare the individual elements to old_value Function Template : void replace (ForwardIterator first, ForwardIterator last, const T& old_value, const T& ne
4 min read
std::string::append vs std::string::push_back() vs Operator += in C++ To append characters, you can use operator +=, append(), and push_back(). All of them helps to append character but with a little difference in implementation and application. Operator += : appends single-argument values. Time complexity : O(n)append() : lets you specify the appended value by using
6 min read
std::string::replace() in C++ The string::replace() function in C++ is used to replace a single or multiple characters from a given index. It is the member function of std::string class. In this article, we will learn how to use the string::replace() function in our C++ program.SyntaxThe string::replace() function provides 6 dif
6 min read
string::rbegin() and string::rend() in C++ The std::string::rbegin() and std::string::rend() functions in C++ are used to fetch the reverse iterators to the string. They are the member function of std::string and are used when we want to iterate the string in reverse. In this article, we will learn how to use string::rbegin() and string::ren
2 min read