string数据插入
算法:
#include<iostream>
using namespace std;
int main() {
string s1 = "Heworld";
s1.insert(2, 2, 'l');
cout << s1 << endl;
s1.insert(4,"o ");
cout << s1 << endl;
s1.insert(s1.size(), " 嘿嘿嘿");//插到尾部
cout << s1 << endl;
s1.insert(s1.begin(), ':');
cout << s1 << endl;
return 0;
}
运行结果:
Hellworld
Hello world
Hello world 嘿嘿嘿
:Hello world 嘿嘿嘿
string数据删除
算法:
#include<iostream>
using namespace std;
int main() {
string s1;
//1
s1 = "Hello woooorld";
s1.erase();//全删
cout << s1 << endl;
//2
s1 = "Hello woooorld";
s1.erase(7);//第七位后全删
cout << s1 << endl;
//3
s1 = "Hello woooorld";
s1.erase(7,3);//第七位开始删三位
cout << s1 << endl;
//4
s1 = "Hello woooorld";
s1.erase(s1.begin());//删第一个 只要传入迭代器就删一个字符
cout << s1 << endl;
//5
s1 = "Hello woooorld";
s1.erase(s1.begin()+7,s1.begin()+10);
cout << s1 << endl;
return 0;
}
运行结果:
Hello w
Hello world
ello woooorld
Hello world