string的增很简单,直接用”+“,”+=“操作符,可以方便的对字符串进行添加
int main() {
string s1;
string s2;
s1 = "abc";
s2 = "abc";
cout << s1 + s2 << endl;
return 0;
}
//输出 abcabc
append()操作
int main() {
string s1;
string s2;
s1 = "str1";
s2 = "str2";
s1.append("123"); //直接添加字符串至尾部
cout << s1 << endl;
s1.append("1230", 2); //从参数字符串起始位置开始添加2个字符到s1
cout << s1 << endl;
s1.append(s2, 0, 1); //把s2的区间[0,1)的字符添加至s1
cout << s2 << endl << s1 << endl;
return 0;
}

append是把参数添加至字符串的尾部,参数字符串可以是string,也可以是c_str,在参数字符串的后面可以参数字符串的起始位置和结束位置。总结为:append(string,[start,end))。
push_back()
push_back(

本文介绍了C++中string对象的常用操作,包括使用append追加字符串,通过push_back添加单个字符,利用insert在特定位置插入内容,以及erase和pop_back进行删除操作。
最低0.47元/天 解锁文章
702

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



