STL中erase()的用法
erase()是STL提供的容器中比较常用的方法之一,它的功能是删除容器中的某些元素,其中它的函数原型如下:
1.有两个参数,且参数类型都是size_t型:
string& erase ( size_t pos = 0, size_t n = npos );
功能是:删除容器中从pos位置开始的n个元素。返回值是经过删除操作后的容器。
示例:
#include<iostream>
using namespace std; int main()
{
string str = "hello world!";
string::iterator it_1 = str.begin();
string::iterator it_2 = str.begin() + 1;
//用法1
cout<<str.erase(0,1)<<endl;
}
结果:

(注:第一种erase用法是string容器所特有的,vectro和list等容器没有这种用法,更多erase的用法见:http://www.cplusplus.com/search.do?q=erase)
2.有一个参数,且参数类型为iterator:
iterator erase ( iterator position );
功能是:删除容器中position所指位置的元素。返回值是指向被删元素之后的那个元素(即下一个元素)的迭代器。
示例:
#include<iostream>
using namespace std; int main()
{
string str = "hello world!";
string::iterator it_1 = str.begin();
string::iterator it_2 = str.begin() + 1;
//用法2
str.erase(it_1);
cout<<str<<endl;
}
结果:

3.有两个参数,且参数类型都是iterator:
iterator erase ( iterator first, iterator last );
功能是:删除容器中first到last之间的所有元素(左闭右开),但不包括last所指的元素。(即删除fist~last -1所指的元素)返回值是一个迭代器,该迭代器指向last所指得的元素,可以理解为返回的就是last。
示例:
#include<iostream>
using namespace std; int main()
{
string str = "hello world!";
string::iterator it_1 = str.begin();
string::iterator it_2 = str.begin() + 1;
//用法3
str.erase(it_1,it_2);
cout<<str<<endl;
}
结果:

STL中erase()的用法的更多相关文章
- STL中mem_fun, mem_fun_ref用法
1.引言 先看一个STL中for_each的用法: #include <iostream> #include <vector> #include <algorithm&g ...
- STL中map的用法
map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候 ...
- (转)STL中set的用法
转载自here 1.关于set map容器是键-值对的集合,好比以人名为键的地址和电话号码.相反地,set容器只是单纯的键的集合.例如,某公司可能定义了一个名为bad_checks的set容器,用于记 ...
- STL中的Set用法(详+转)
set是STL中一种标准关联容器(vector,list,string,deque都是序列容器,而set,multiset,map,multimap是标准关联容器),它底层使用平衡的搜索树——红黑树实 ...
- STL中erase的小心使用
先看如下一道改错题: #include<iostream> #include<vector> using namespace std; void print(vector< ...
- STL中erase()的陷阱
最近在刷stl源码剖析这本书时,对于vector的erase()函数引起了我的注意 在删除单个元素时是这样定义的: iterator erase(iterator position){ !=end() ...
- STL中bitset的用法
终于又来写博客了 == bitset存储的是二进数位,就和一个bool性数组差不多.用法上和数组的操作方式也差不多. 每位只占一个字节,大大优化了空间,可以通过数组形式访问. bitset定义 可以用 ...
- C++ STL 中erase()的使用需要小心
C++ STL极大的方便了用户编写程序,但是同时一不小心也会犯一些错误,如erase()造成迭代器失效经常会引起错误. 错误示例: std::list< int> List; std::l ...
- STL中set的用法
set,顾名思义,就是数学上的集合——每个元素最多只出现一次,并且set中的元素已经从小到大排好序. 头文件:#include<set> 常用的函数: begin() 返回set容 ...
随机推荐
- Factor Graph因子图
参考链接1: 参考链接2: 参考ppt3: Factor Graph 是概率图的一种,概率图有很多种,最常见的就是Bayesian Network (贝叶斯网络)和Markov Random Fiel ...
- Estimating Linguistic Complexity for Science Texts--paper
http://aclweb.org/anthology/W18-0505 https://sites.google.com/site/nadeemf0755/research/linguistic-c ...
- linux zip tar 压缩打包命令
zip 压缩命令:(可压缩文件或目录) 压缩文件: zip new_name.zip file_name unzip name.zip 解压 压缩文件或目录: 指定解压位置: unzip na ...
- python django day 1
cmd d:\python\python.exe pip install --upgrade pip d:\python\python.exe pip install Django 123.py im ...
- Tomcat环境变量配置命令行报错:The JRE_HOME environment variable is not defined correctl This environment variable is needed to run this program
1. tomcat——>bin——>setclasspath.bat,使用记事本打开. 2. 添加如下代码即可: 为自己实际的环境变量配置为准!!! set JAVA_HOME=D:\ID ...
- 最新apache多域名多站点配置
httpd.conf===> Listen Listen ServerName 用IP地址作为servername LoadModule rewrite_module modules/mod_r ...
- bfprt
bfprt //找第k小的数 #include <iostream> #include <vector> #include <algorithm> using na ...
- Array.asList()
package study.stage2;import java.util.*; /** * Created by Sandy.Liu on 2017/7/19. */public class Asl ...
- html+css常用总结
一,HTML结构: 1,DOCTYPE 2,head: title:网站的标题 meta charset 3,body: 二,HTML标签: 块状元素和内联元素: 常见的块级元素有:div.p.add ...
- [转]Spring中property-placeholder的使用与解析
我们在基于spring开发应用的时候,一般都会将数据库的配置放置在properties文件中. 代码分析的时候,涉及的知识点概要: NamespaceHandler 解析xml配置文件中的自定义命名空 ...