Skip to content
Related Articles

Related Articles

std::string::replace , std::string::replace_if in C++
  • Difficulty Level : Easy
  • Last Updated : 06 Jul, 2017


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

// CPP code to demonstrate replace()
#include <iostream>
using namespace std;
// Function for demonstration
void 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;
    // Repalces 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 charcaters 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 code
int main()
    string s1 = "Example of replace";
    string s2 = "Demonstration";
    string s3 = "GeeksforGeeks";
    string s4 = "HeyWorld !";
    replaceDemo(s1, s2, s3, s4);
    return 0;


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

// CPP code to demonstrate replace_if()
#include <iostream>
#include <vector>
#include <algorithm> // Header file for replace_if
using namespace std;
// Function to check if character is vowel or not
bool 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 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_if " <<": "
    cout << "After replace_if " << ": ";
    return 0;


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_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 or mail your article to 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.

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :