C++ string 中 erase() 的多種用法

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

發佈了184 篇原創文章 · 獲贊 24 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章