STL容器之string插入和删除

1.

/*
插入和删除操作
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);//删除从Pos开始的n个字符
*/
void test02()
{
    string s1 = "hello";
    //插入
    s1.insert(1, "123");    //string& insert(int pos, const char* s);
    cout << s1 << endl;     //h123ello

    //删除
    s1.erase(1, 3);
    cout << s1 << endl;     //hello
}

结果:

原文地址:https://www.cnblogs.com/yifengs/p/15189073.html