#include <iostream>
#include <string>
int main (){
std::string str="to be question";
std::string str2="the ";
std::string str3="or not to be";
std::string::iterator it;
// used in the same order as described above:
str.insert(6,str2); // to be (the )question
str.insert(6,str3,3,4); // to be (not )the question
str.insert(10,"that is cool",8); // to be not (that is )the question
str.insert(10,"to be "); // to be not (to be )that is the question
str.insert(15,1,':'); //加一个'.' to be not to be(:) that is the question
it = str.insert(str.begin()+5,','); // to be(,) not to be: that is the question
str.insert (str.end(),3,'.'); // to be, not to be: that is the question(...)
str.insert (it+2,str3.begin(),str3.begin()+3); // (or )
std::cout << str << '\n';
return 0;
}
//output to be, or not to be: that is the question...
毕竟官方用例,hin nice
link:
http://www.cplusplus.com/reference/string/string/insert/
本文通过一个具体的C++示例程序,详细介绍了如何使用std::string的insert方法来实现字符串的插入操作。示例中涵盖了多种insert方法的调用方式,包括插入字符串、字符以及特定位置的单个字符。

4979

被折叠的 条评论
为什么被折叠?



