C++中string erase函数的使用

erase函数的原型如下:
(1)string& erase ( size_t pos = 0, size_t n = npos );
(2)iterator erase ( iterator position );
(3)iterator erase ( iterator first, iterator last );
也就是说有三种用法:
(1)erase(pos,n); 删除从pos开始的n个字符,比如erase(0,1)就是删除第一个字符
(2)erase(position);删除position处的一个字符(position是个string类型的迭代器)
(3)erase(first,last);删除从first到last之间的字符(first和last都是迭代器)
下面给你一个例子:
// string::erase
#include <iostream>
#include <string>
using namespace std;

int main ()
{
string str ("This is an example phrase.");
string::iterator it;

// 第(1)种用法
str.erase (10,8);
cout << str << endl; // "This is an phrase."

// 第(2)种用法
it=str.begin()+9;
str.erase (it);
cout << str << endl; // "This is a phrase."

// 第(3)种用法
str.erase (str.begin()+5, str.end()-7);
cout << str << endl; // "This phrase."
return 0;
}

你的假如是删除数组中的一项,并不能说明erase的用法。

原文链接: https://www.cnblogs.com/liyazhou/archive/2010/02/07/1665421.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    C++中string erase函数的使用

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/8043

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月6日 下午6:42
下一篇 2023年2月6日 下午6:42

相关推荐