string::replace

string (1)
string& replace (size_t pos,        size_t len,        const string& str);
string& replace (const_iterator i1, const_iterator i2, const string& str);
substring (2)
string& replace (size_t pos,        size_t len,        const string& str,
                 size_t subpos, size_t sublen);
c-string (3)
string& replace (size_t pos,        size_t len,        const char* s);
string& replace (const_iterator i1, const_iterator i2, const char* s);
buffer (4)
string& replace (size_t pos,        size_t len,        const char* s, size_t n);
string& replace (const_iterator i1, const_iterator i2, const char* s, size_t n);
fill (5)
string& replace (size_t pos,        size_t len,        size_t n, char c);
string& replace (const_iterator i1, const_iterator i2, size_t n, char c);
range (6)
template <class InputIterator>
  string& replace (const_iterator i1, const_iterator i2,
                   InputIterator first, InputIterator last);
initializer list (7)
string& replace (const_iterator i1, const_iterator i2, initializer_list<char> il);

#include <iostream>
#include<string.h>

using namespace std;
int main()
{
string s1 = "i love zhh";
string s2 = "lyy";
string s3 = "zhh";
const char *s4 = "love";
const char *s5 = "like";
const char *s6 = "enjoyooo";
string s7 = "00be fond of11";
s1.replace(7, 4, s2);
cout << s1 << endl;
s1.replace(7, 4, s3);
cout << s1 << endl;
try{
s1.replace(20, 1, s3);
}catch(out_of_range){
cout << "out_of_range" << endl;
}
cout << "2" << s1 << endl;
s1.replace(s1.begin() + 7, s1.end(), s2);
cout << s1 << endl;

s1.replace(2, 4, s3, 0, 4);
cout << s1 << endl;
try{
s1.replace(22, 4, s3, 0, 4);
}catch(out_of_range){

cout << "out_of_range" << endl;
}
cout << s1 << endl;

s1.replace(2, 3, s4);
cout << s1 << endl;

s1.replace(s1.begin() + 2, s1.begin() + 6, s5);
cout << s1 << endl;

s1.replace(2, 4, s6, 5);
cout << s1 << endl;

s1.replace(s1.begin() + 2, s1.begin() + 7, s5, 4);
cout << s1 << endl;

s1.replace(2, 4, 5, '$');
cout << s1 << endl;

s1.replace(s1.begin() + 2, s1.begin() + 7, 4, '@');
cout << s1 << endl;

s1.replace(s1.begin() + 2, s1.begin() + 6, s7.begin() + 2, s7.end() -2);
cout << s1 << endl;

initializer_list<char> s8 = {'l', 'o', 'v', 'e'};//interesting  !!!
s1.replace(s1.begin() + 2, s1.end() - 4, s8);
cout << s1 << endl;

return 0

}

posted @ 2019-12-20 10:27  MoonXu  阅读(592)  评论(0编辑  收藏  举报