std::string::replace , std::string::replace_if in C++

28 May, 2021
It replaces each element in the range [first, last) that is equal to oldValue with newValue. The function uses operator == to compare the individual elements to old_value.
template <class ForwardIterator, class T>
  void replace (ForwardIterator first, ForwardIterator last,
                const T& old_value, const T& new_value);
first, last : the range of elements to process
old_value : the value of elements to replace
new_value : the value to use as replacement
C++
// CPP code to demonstrate replace() #include <iostream>using namespace std; // Function for demonstrationvoid replaceDemo(string s1, string s2, string s3, string s4){    // Replaces 7 characters from 0th index by s2    s1.replace(0, 7, s2);    cout << s1 << endl;     // Replaces 3 characters from 0th index with "Hello"    s4.replace(0, 3, "Hello ");    cout << s4 << endl;     // Replaces 5 characters from 6th index of s4 with    // 5 characters from 0th of s3    s4.replace(6, 5, s3, 0, 5);    cout << s4 << endl;     // Replaces 5 characters from 6th index of s4 with    // 6 characters from string "to all"    s4.replace(6, 5, "to all", 6);    cout << s4 << endl;     // Replaces 1 character from 12th  index of s4 with    // 3 copies of '!'    s4.replace(12, 1, 3, '!');    cout << s4 << endl;} // Driver codeint main(){    string s1 = "Example of replace";    string s2 = "Demonstration";    string s3 = "GeeksforGeeks";    string s4 = "HeyWorld !";     replaceDemo(s1, s2, s3, s4);    return 0;}
Output: 
Demonstration of replace
Hello World !
Hello Geeks !
Hello to all !
Hello to all!!!!
replace_if() replaces each element in the range [first, last) for which the unary predicate i.e. op(elem) yields true with newValue. 
template <class ForwardIterator, class UnaryPredicate, class T>
  void replace_if (ForwardIterator first, ForwardIterator last,
                   UnaryPredicate pred, const T& new_value );
Unary predicate are just functions that take a single parameter 
and return a Boolean value. It is used to check whether an 
element fits the criterion
C++
// CPP code to demonstrate replace_if() #include <iostream>#include <vector>#include <algorithm> // Header file for replace_ifusing namespace std; // Function to check if character is vowel or notbool IsVowel(char ch){    return (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U');} // Function to replace all vowels with 'V'void replace_ifDemo(vector<char>&v){    replace_if(v.begin(), v.end(), IsVowel, 'V');} // Function to print content of vectorvoid print(vector<char>&v){    int len = v.size();    for (int i = 0; i < len; i++)        cout << v[i] << " ";    cout << endl;} // Driver codeint main(){    vector<char> v;     for (int i = 0; i <= 6; i++)        v.push_back('A' + i);    cout << "Before replace_if " <<": ";    print(v);    replace_ifDemo(v);         cout << "After replace_if " << ": ";    print(v);         return 0;}
Output: 
Before replace_if : A B C D E F G 
After replace_if : V B C D V F G 
Related Article : std::string::replace_copy(), std::string::replace_copy_ifThis 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 write.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. 
anikaseth98
cpp-strings-library
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Bitwise Operators in C/C++
Priority Queue in C++ Standard Template Library (STL)
Set in C++ Standard Template Library (STL)
vector erase() and clear() in C++
unordered_map in C++ STL
Inheritance in C++
The C++ Standard Template Library (STL)
Object Oriented Programming in C++
C++ Classes and Objects
Sorting a vector in C++