1. string类的insert
1.1 string:string& insert (size_t pos, const string& str);
- 原型:string& insert (size_t pos, const string& str);
- 说明:在源字符串下标为pos处插入string型字符串str。
代码示例:
#include <iostream> #include <string> using namespace std; int main () { string str="to be question"; string str1="the "; cout<<"string& insert (size_t pos, const string& str);"<<endl; cout<<"源字符串 :"<<str<<endl; str.insert(6, str1); cout<<"目标字符串:"<<str<<endl; system("pause"); return 0; } =>string& insert (size_t pos, const string& str); 源字符串 :to be question 目标字符串:to be the question
1.2 substring:string& insert (size_t pos, const string& str, size_t subpos, size_t sublen = npos);
- 原型:string& insert (size_t pos, const string& str, size_t subpos, size_t sublen = npos);
- 说明:在源字符串下标为pos处插入string型字符串str中以下标处为subpos开始的sublen个字符组成的子字符串。
代码示例:
#include <iostream> #include <string> using namespace std; int main () { string str="to be question"; string str2="or not to be the "; cout<<"string& insert (size_t pos, const string& str, size_t subpos, size_t sublen = npos);"<<endl; cout<<"源字符串 :"<<str<<endl; str.insert(6, str2, 13, 4); cout<<"目标字符串:"<<str<<endl; system("pause"); return 0; } =>string& insert (size_t pos, const string& str, size_t subpos, size_t sublen = npos); 源字符串 :to be question 目标字符串:to be the question
1.3 c-string:string& insert (size_t pos, const char* s);
- 原型:string& insert (size_t pos, const char* s);
- 说明:在源字符串下标为pos处插入char型字符串s。
代码示例:
#include <iostream> #include <string> using namespace std; int main () { string str="to be question"; cout<<"string& insert (size_t pos, const char* s);"<<endl; cout<<"源字符串 :"<<str<<endl; str.insert(6, "the "); cout<<"目标字符串:"<<str<<endl; system("pause"); return 0; } =>string& insert (size_t pos, const char* s); 源字符串 :to be question 目标字符串:to be not question
1.4 buffer:string& insert (size_t pos, const char* s, size_t n);
- 原型:string& insert (size_t pos, const char* s, size_t n);
- 说明:在源字符串下标为pos处插入char型字符串s的前n个字符组成的子字符串。
代码示例:
#include <iostream> #include <string> using namespace std; int main () { string str="to be question"; cout<<"string& insert (size_t pos, const char* s, size_t n);"<<endl; cout<<"源字符串 :"<<str<<endl; str.insert(6, "the question ", 4); cout<<"目标字符串:"<<str<<endl; system("pause"); return 0; } =>string& insert (size_t pos, const char* s, size_t n); 源字符串 :to be question 目标字符串:to be not question
1.5 fill:string& insert (size_t pos, size_t n, char c);
- 原型:string& insert (size_t pos, size_t n, char c);
- 说明:在源字符串下标为pos处插入n个字符c。
代码示例:
#include <iostream> #include <string> using namespace std; int main () { string str="to be question"; cout<<"string& insert (size_t pos, size_t n, char c);"<<endl; cout<<"源字符串 :"<<str<<endl; str.insert(6, 1, 'a'); str.insert(7, 1, ' '); cout<<"目标字符串:"<<str<<endl; system("pause"); return 0; } =>string& insert (size_t pos, size_t n, char c); 源字符串 :to be question 目标字符串:to be a question
1.6 fill:iterator insert (const_iterator p, size_t n, char c);
- 原型:iterator insert (const_iterator p, size_t n, char c);
- 说明:在源字符串的迭代器p处插入n个字符c,返回插入后迭代器的位置。
代码示例:
#include <iostream> #include <string> using namespace std; int main () { string str="to be question"; cout<<"iterator insert (const_iterator p, size_t n, char c);"<<endl; cout<<"源字符串 :"<<str<<endl; str.insert(str.begin()+6, 1, 'a'); str.insert(str.begin()+7, 1, ' '); cout<<"目标字符串:"<<str<<endl; system("pause"); return 0; } =>iterator insert (const_iterator p, size_t n, char c); 源字符串 :to be question 目标字符串:to be a question
1.7 single character:iterator insert (const_iterator p, char c);
- 原型:iterator insert (const_iterator p, char c);
- 说明:在源字符串的迭代器p处插入字符c,返回插入后迭代器的位置。
代码示例:
#include <iostream> #include <string> using namespace std; int main () { string str="to be question"; cout<<"iterator insert (const_iterator p, char c);"<<endl; cout<<"源字符串 :"<<str<<endl; str.insert(str.begin()+6, 'a'); str.insert(str.begin()+7, ' '); cout<<"目标字符串:"<<str<<endl; system("pause"); return 0; } =>iterator insert (const_iterator p, char c); 源字符串 :to be question 目标字符串:to be a question
1.8 range:template < class InputIterator>iterator insert (iterator p, InputIterator first, InputIterator last);
- 原型:template < class InputIterator>iterator insert (iterator p, InputIterator first, InputIterator last);
- 说明: Inserts a copy of the sequence of characters in the range [first,last), in the same order.
代码示例:
#include <iostream> #include <string> using namespace std; int main () { string str="to be question"; string str3="or not to be"; string::iterator it = str.begin()+6; cout<<"template < class InputIterator>iterator insert (iterator p, InputIterator first, InputIterator last);"<<endl; cout<<"源字符串 :"<<str<<endl; str.insert (it, str3.begin(), str3.end()); cout<<"目标字符串:"<<str<<endl; system("pause"); return 0; } =>template < class InputIterator>iterator insert (iterator p, InputIterator first, InputIterator last); 源字符串 :to be question 目标字符串:to be or not to bequestion
参考文献:
[1] 网络资源: http://www.cplusplus.com/reference/string/string/insert/