1.在特定位置插入const char *字符串
(1)string &insert(int p0, const char *s)
功能:在原字符串下标为pos的字符前插入字符串str
返回值:插入字符串后的结果(2)string &insert(int p0, const char *s, int n);
功能:在p0位置插入字符串s的前n个字符
返回值:插入字符串后的结果
string str1="Working people ";
string str2="Working soul ";
string str3="Working people are Exalted";
//string &insert(int p0, const char *s)
//在原字符串下标为pos的字符前插入字符串str
//返回值:插入字符串后的结果
auto str=str1.insert(15,"Working soul ");
cout<<"str1: "<<str1<<endl; //str1: Working people Working soul
cout<<"str: "<<str<<endl; //str: Working people Working soul
//string &insert(int p0, const char *s, int n);
//在p0位置插入字符串s的前n个字符
//返回值:插入字符串后的结果
str=str1.insert(28,"Working people are Exalted",26);
cout<<"str1: "<<str1<<endl; //str1: Working people Working soul Working people are Exalted
cout<<"str: "<<str<<endl; //str: Working people Working soul Working people are Exalted
2.在特定位置插入 const string型字符串
(1)string &insert(int p0,const string &s);
功能:在p0位置插入字符串s
返回值:插入字符串后的结果
(2)string &insert(int p0,const string &s, int pos, int n);
功能:在p0位置插入字符串s从pos开始的连续n个字符
返回值:插入字符串后的结果
//string &insert(int p0,const string &s);
//在p0位置插入字符串s
//返回值:插入字符串后的结果
str1="Working people Working soul Working people are Exalted";
string str4="I am ";
str=str1.insert(0,str4);
cout<<"str1: "<<str1<<endl; //str1: I am Working people Working soul Working people are Exalted
cout<<"str: "<<str<<endl; //str: I am Working people Working soul Working people are Exalted
//string &insert(int p0,const string &s, int pos, int n);
//在p0位置插入字符串s从pos开始的连续n个字符
//返回值:插入字符串后的结果
str1="Hello ";
str2="World";
str=str1.insert(6,str2,0,5);
cout<<"str1: "<<str1<<endl; //str1: Hello World
cout<<"str: "<<str<<endl; //str: Hello World
3.在特定位置插入char型字符.
(1)string &insert(int p0, int n, char c);
功能:在p0处插入n个字符c
返回值:插入字符串后的结果
(2)void insert(iterator it, int n, char c);
功能:在it处插入n个字符c
(3)iterator insert(iterator it, char c)
功能:在it处插入字符c
返回值:插入字符的位置
//string &insert(int p0, int n, char c);
//在p0处插入n个字符c
//返回值:插入字符串后的结果
str1="Hello ";
str=str1.insert(6,5,'y');
cout<<"str1: "<<str1<<endl; //str1: Hello yyyyy
cout<<"str: "<<str<<endl; //str:Hello yyyyy
//void insert(iterator it, int n, char c);
//在it处插入n个字符c
str1="Hello ";
str1.insert(str1.end(),5,'y');
cout<<"str1: "<<str1<<endl; //str1: Hello yyyyy
//iterator insert(iterator it, char c)
//在it处插入字符c
//返回值:插入字符的位置
str1="ello ";
string::iterator it=str1.insert(str1.begin(),'H');
cout<<"str: "<<str1<<endl; //str:Hello
cout<<"*it: "<<*it<<endl; //*it: H
4.插入迭代器所指的字符串
(1)void insert(iterator it, const_iterator first, const_iterator last);
功能:在it处插入从first开始至last-1的所有字符
//void insert(iterator it, const_iterator first, const_iterator last);
//在it处插入从first开始至last-1的所有字符
str1="Hello ";
str2="World";
str1.insert(str1.end(),str2.begin(),str2.end());
cout<<"str1: "<<str1<<endl; //str1: Hello World