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都是迭代器)
下面给你一个例子:
#include <iostream>
#include <string>
using namespace std; int main ()
{
string str ("This is an example phrase.");
string::iterator it; // 第(1)种用法
str.erase (,);
cout << str << endl; // "This is an phrase." // 第(2)种用法
it=str.begin()+;
str.erase (it);
cout << str << endl; // "This is a phrase." // 第(3)种用法
str.erase (str.begin()+, str.end()-);
cout << str << endl; // "This phrase."
return ;
}
C++中string erase函数的使用的更多相关文章
- c++中string.erase()函数的用法(转)
erase函数的原型如下:(1)string& erase ( size_t pos = 0, size_t n = npos );(2)iterator erase ( iterator p ...
- java中string.trim()函数的使用
java中string.trim()函数的的作用是去掉字符串开头和结尾的空格,防止不必要的空格导致的错误. public static void main(String arg[]){ String ...
- C++中string中的erase函数怎么使用
erase函数的原型如下:(1)string& erase ( size_t pos = 0, size_t n = npos );(2)iterator erase ( iterator p ...
- 在容器中使用erase函数,迭代器的处理
在c++编程中,用到迭代器的时候,往往不知道如何删除当前迭代器指向的元素. erase函数: 返回下一个迭代器. 只使用vector的erase函数,记住,该函数是迭代器失效,返回下一个迭代器. ...
- C++中string常用函数用法总结
string(s小写)是C++标准库中的类,纯C中没有,使用时需要包含头文件#include<string>,注意不是<string.h>,下面记录一下string中比较常用的 ...
- C++中string::find()函数和string::npos函数的使用
1. string::find()函数和string::npos函数的介绍 我们在学习C++的时候必不可少的使用到string类中的find()函数,它是一个查找函数,功能还是很强大的,但是此处我们不 ...
- string中的erase()函数
erase()是对string类型的字符串进行删除元素操作的函数 1.erase(int index) 删除下标从index开始直到字符串结尾的元素 1 string s = "123215 ...
- 字符串的查找删除---C++中string.find()函数与string::npos
给定一个短字符串(不含空格),再给定若干字符串,在这些字符串中删除所含有的短字符串 输入: 输入只有一组数据 输入一个短字符串(不含空格),再输入若干字符串直到文件结束为止 输出: 删除输入的短字符串 ...
- C++中string.find()函数,string.find_first_of函数与string::npos
查找字符串a是否包含子串b,不是用strA.find(strB) > 0而是strA.find(strB) != string:nposstring::size_type pos = strA. ...
随机推荐
- ASP.NET MVC Razor语法及实例
1.混合HTML与Razor脚本 知识点:(1).cshtml怎样引用访问数据, (2).if for 与html嵌套 @using System.Data @using CIIC.TCP.Enti ...
- hadoop下HDFS基本命令使用
前提:启动hadoop 1. 查看hdfs下 " / " 的目录 hdfs dfs -ls / 2. 创建文件夹(在 " / " 创建hadoop文件夹) hd ...
- 【NOIP 2015】斗地主
题目描述 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的 A 到 K 加上大小王的共 54 张牌来进行的扑克牌游戏.在斗地主中,牌的大小关 系根据牌的数码表示如下: 3 ...
- From表单提交刷新页面?
form表单提交跳转 写作原因: 楼主的html水平一般,偶然想起周围人常说的form表单提交会刷新页面,闲来无事,就想想其中的原因 想来想去为什么会刷新,猜想了以下几条 1.先提交数据,等服务器 ...
- POJ1664 放苹果 (母函数)
放苹果 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 37515 Accepted: 23090 Description ...
- windows mysql导入sql文件
当需要的sql文件很大时(>200M)怎么办?答:修改my.ini文件,max_allowed_packet的值可以设置为1024M 进入mysql.exe目录下,执行如下命令: mysql - ...
- [转载]np.where()使用说明
转载自https://www.cnblogs.com/massquantity/p/8908859.html#4072620 numpy.where() 有两种用法: 1. np.where(cond ...
- pycharm、idea 2018软件安装教程
Python3.7安装: https://www.jb51.net/article/146326.htm pycharm软件: https://www.jianshu.com/p/cf77d74bef ...
- javascript的最重要的特性之一:闭包的解决方案
初始代码: for (var j = 0; j < lnglats.length; j++) { AMap.event.addListener(markers[j], 'mouseover', ...
- 优先队列priority_queue的简单应用
优先队列 引入 优先队列是一种特殊以及强大的队列. 那么优先队列是什么呢? 说白了,就是一种功能强大的队列. 它的功能强大在哪里呢? 四个字:自动排序. 优先队列的头文件&&声明 头文 ...