内容:string的插入与删除操作。
(ps:今天本来想摆烂的,后来想了想,还是算了……)
#include <iostream>
#include <string>
using namespace std;
/*
注意:
1. string& insert(int pos, const string& str); --> 插入string型字符串
string& insert(int pos, const char* str); --> 插入char*型字符串
string& insert(int pos, int n, char c); --> 从位置pos开始插入n个字符c
2. string& erase(int pos, int n = npos); --> 从pos位置开始删除后面所有字符
string& erase(int pos, int n); --> 从pos位置开始删除n个字符
3.字符串首位置为0
*/
void test01()
{
string str1 = "aaaaaa";
str1.insert(1, "bbb"); // here,插入02
cout << str1 << endl;
str1.erase(4); // here,删除01
cout << str1 << endl;
str1.erase(1, 3); // here,删除02
cout << str1 << endl;
}
void test02()
{
string str2 = "aaaaaa";
string str3 = "ccc";
str2.insert(1, str3); // here,插入01
cout << str2 << endl;
str3.insert(1, 3, 'd'); // here,插入03
cout << str3 << endl;
}
int main()
{
test01();
test02();
return 0;
}