string中 用erase()能夠進行多種方式的刪除
方式一
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
string a = "hello world";
cout << a << endl;
a.erase(7);///從第七個位置的後面開始,後面的字符全都刪除。
cout << a << endl;
a.erase(2, 2);///從第二個位置的後面開始,刪除兩個字符。
cout << a << endl;
return 0;
}
運行結果
方式二
刪除迭代器[first, last)區間的所有字符
iterator erase(const_iterator first, const_iterator last)
方式三
刪除單個字符
iterator erase(const_iterator position)
返回值爲下個元素的迭代器
參考來源
https://blog.csdn.net/u010472607/article/details/80431604