C++ String 的erase、remove和pop

您所在的位置:网站首页 括号删干净 C++ String 的erase、remove和pop

C++ String 的erase、remove和pop

2023-08-22 09:17| 来源: 网络整理| 查看: 265

一. erase()删除

erase 方法原型:

1. basic_string & erase(size_type pos=0, size_type n=npos);

   即erase(pos,n);从给定起始位置pos处开始删除, 删除n个字符, 返回值修改后的string对象引用。比如erase(0,1)就是删除第一个字符。

2. iterator erase(const_iterator position) 即erase(positon),删除迭代器位置处的单个字符(position是个string类型的迭代器), 并返回下个元素的迭代器。

如果position 不是迭代器位置,则删除该位置及之后的所有字符。

3. iterator erase(const_iterator first, const_iterator last)  即erase(first,last);删除迭代器(first, last)区间的所有字符(first和last都是迭代器),返回一个指向被删除的最后一个元素的下一个字符的迭代器.

#include #include using namespace std; int main() { string str = "hello the world!"; string str1 = "hello the world!"; string str2 = "hello the world!"; string str3 = "hello the world!"; string str4 = "hello the world!"; /****************第1种用法:earse(pos,n)**************************/ str.erase(6, 2); cout


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3