/* Usage one: Use str to replace the specified string with a string of length len starting from the starting position pos string &replace(size_t pos, size_t len, const string& str) */ #include <bits/stdc++.h> using namespace std; typedef long long ll; const double eps = 1e-7; const int maxn = 5e5 + 5; const double pi = acos(-1.0); intmain() { string s="this is@ a test string!"; s=s.replace(s.find("@"),1,""); cout<<s<<endl; } //this is a test string!
/* Usage two: Replace the string at the start and end of the iterator with str string &replace(const_iterator i1, const_iterator i2, const string& str) */ #include <bits/stdc++.h> using namespace std; typedef long long ll; const double eps = 1e-7; const int maxn = 5e5 + 5; const double pi = acos(-1.0); intmain() { string s="this is@ a@ test string!"; s=s.replace(s.begin(),s.begin()+6,""); cout<<s<<endl; } //s@ a@ test string!
/* Usage three: Replace the string from the specified position with the specified substring of substr (given the starting position and length) string &replace(size_t pos, size_t len, const string& str, size_t subpos, size_t sublen) */ #include <bits/stdc++.h> using namespace std; typedef long long ll; const double eps = 1e-7; const int maxn = 5e5 + 5; const double pi = acos(-1.0); intmain() { string s="this is@ a@ test string!"; string str="12345"; s=s.replace(0,5,str,str.find("1"),3); //Replace the string from position 0 to 5 with the specified substring of str (3 characters from position 1) s cout<<s<<endl; } //123is@ a@ test string!
From: Click to open the link