1.功能:对string字符串进行插入和删除的操作
2.函数原型:
- string &insert(int pos,const char *s); //插入字符
- string &insert(int pos,const string &str); //插入字符
- string &insert(int pos,int n,char c); //在指定位置插入n个字符c
- string &erase(int pos,int n=npos)l; //删除从pos开始的n个字符
#include<iostream> #include<string> using namespace std; void test1() { string s="hllo"; //插入 cout<<"插入之前 s = "<<s<<endl; s.insert(1,"e"); //在下标1位置插入e,原下标1及其之后的字符全部都后移 cout<<"插入之后 s = "<<s<<endl; //删除 cout<<"删除之前 s = "<<s<<endl; s.erase(1,3); //删除从下标1开始及其后面2个,共3个字符 cout<<"删除之后 s = "<<s<<endl; } void test2() { } int main() { test1(); test2(); return 0; } /* 打印结果: 插入之前 s = hllo 插入之后 s = hello 删除之前 s = hello 删除之后 s = ho */