C++ remove remove_if erase
#include <iostream>
#include <algorithm>
#include <list>
#include <vector>
#include <functional>
using namespace std;
int main()
{
list<int> list1;
for (int k=0;k<10;k++)
{
list1.push_back(k);
list1.push_front(k);
}
list<int>::iterator list_iter1;
for (list_iter1 = list1.begin();list_iter1 != list1.end();++list_iter1)
{
cout << *list_iter1 << " ";
}
cout << endl;
cout << "list1 size: " << list1.size() << endl;
cout << "----------------------------------------" << endl;
list<int>::iterator list_iter_end;
list_iter_end = remove(list1.begin(),list1.end(),3);
for (list_iter1 = list1.begin(); list_iter1 != list1.end(); ++list_iter1)
{
cout << *list_iter1 << " ";
}
cout << endl;
cout << "list1 size: " << list1.size() << endl;
cout << "----------------------------------------" << endl;
for (list_iter1 = list1.begin(); list_iter1 != list_iter_end; ++list_iter1)
{
cout << *list_iter1 << " ";
}
cout << endl;
cout << "list1 size: " << list1.size() << endl;
cout << "----------------------------------------" << endl;
cout << "删除元素的个数:" <<distance(list_iter_end,list1.end())<< endl;
//真正的删除
list<int>::iterator list_iter3 = list1.begin();
++list_iter3;
++list_iter3;
++list_iter3;
list1.erase(list1.begin(), list_iter3);
for (list_iter1 = list1.begin(); list_iter1 != list1.end(); ++list_iter1)
{
cout << *list_iter1 << " ";
}
cout << endl;
cout << "list1 size: " << list1.size() << endl;
cout << "----------------------------------------" << endl;
vector<int> vec1;
for(int k=0;k<10;k++)
{
vec1.push_back(k);
}
for (int k =6; k<16; k++)
{
vec1.push_back(k);
}
vector<int>::iterator vec_iter1;
for (vec_iter1 = vec1.begin();vec_iter1 != vec1.end();++vec_iter1)
{
cout << *vec_iter1 << " ";
}
cout << endl;
cout << "vec1 size: " << vec1.size() << endl;
cout << "----------------------------------------" << endl;
vector<int>::iterator vec_iter2 = remove(vec1.begin(),vec1.end(),11);
vec1.erase(vec_iter2, vec1.end());
for (vec_iter1 = vec1.begin(); vec_iter1 != vec1.end(); ++vec_iter1)
{
cout << *vec_iter1 << " ";
}
cout << endl;
cout << "vec1 size: " << vec1.size() << endl;
cout << "----------------------------------------" << endl;
vector<int>::iterator vec_iter4 = remove_if(vec1.begin(),vec1.end(),bind2nd(less<int>(),6));
vec1.erase(vec_iter4,vec1.end());
for (vec_iter1 = vec1.begin(); vec_iter1 != vec1.end(); ++vec_iter1)
{
cout << *vec_iter1 << " ";
}
cout << endl;
cout << "vec1 size: " << vec1.size() << endl;
cout << "----------------------------------------" << endl;
system("pause");
return 0;
}
========================================
9 8 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 9
list1 size: 20
----------------------------------------
9 8 7 6 5 4 2 1 0 0 1 2 4 5 6 7 8 9 8 9
list1 size: 20
----------------------------------------
9 8 7 6 5 4 2 1 0 0 1 2 4 5 6 7 8 9
list1 size: 20
----------------------------------------
删除元素的个数:2
6 5 4 2 1 0 0 1 2 4 5 6 7 8 9 8 9
list1 size: 17
----------------------------------------
0 1 2 3 4 5 6 7 8 9 6 7 8 9 10 11 12 13 14 15
vec1 size: 20
----------------------------------------
0 1 2 3 4 5 6 7 8 9 6 7 8 9 10 12 13 14 15
vec1 size: 19
----------------------------------------
6 7 8 9 6 7 8 9 10 12 13 14 15
vec1 size: 13
----------------------------------------
请按任意键继续. . .

C++ remove remove_if erase的更多相关文章
- STL:remove和erase区别
C++ STL中的remove和erase函数曾经让我迷惑,同样都是删除,两者有什么区别呢? vector中的remove的作用是将等于value的元素放到vector的尾部,但并不减少vector的 ...
- remove、erase
remove: remove不是真正的删除,删除后数量并没有变化. 它接收一对迭代器,而不是一个容器,所以不知道它作用于哪个容器. 而且没有办法从一个迭代器获取对应于它的容器 实现 remove会用需 ...
- 移除元素(remove,remove_if...unique...)
remove 因为本算法作用的是iterator,所以并不会改变Container大小,会返回一个新的iterator new_last,是的first到new_last中的元素都不等于value,左 ...
- STL笔记(4)关于erase,remove
STL笔记(4)关于erase,remove 你要erase的元素很容易识别.它们是从区间的“新逻辑终点”开始持续到区间真的终点的原来区间的元素.要除去那些元素,你要做的所有事情就是用那两个迭代器调用 ...
- C++ count_if/erase/remove_if 用法详解
每次使用这几个算法时都要去查CPP reference,为了能够加深印象,整理一下基本应用. cout/cout_if: return the number of elements satisfyi ...
- C++中vector的remove用法
我将从remove的复习开始这个条款,因为remove是STL中最糊涂的算法.误解remove很容易,驱散所有关于remove行为的疑虑——为什么它这么做,它是怎么做的——是很重要的. 这是rem ...
- STL源代码分析——STL算法remove删除算法
前言 因为在前文的<STL算法剖析>中,源代码剖析许多.不方便学习,也不方便以后复习,这里把这些算法进行归类.对他们单独的源代码剖析进行解说.本文介绍的STL算法中的remove删除算法. ...
- c++转载系列 std::vector模板库用法介绍
来源:http://blog.csdn.net/phoebin/article/details/3864590 介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作 ...
- C++ 中的std::vector介绍(转)
vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单地说,vec ...
随机推荐
- Linux命令——shutdown、halt、poweroff、reboot、cal、date
shutdown shutdown在关机的时候会通知所有用户 shutdown –r now 现在重启 shutdown now 现在关机 shutdown +5 过5分钟关机 shutdown –c ...
- linux-2.6.38 IIC驱动框架分析
在linux-2.6内核中,IIC的驱动程序可以大概分为三部分: (1)IIC核心代码:/drivers/i2c/i2c-core.c IIC核心提供了IIC总线驱动和设备驱动的注册.注销方法和IIC ...
- Java字节码方法表与属性表深度剖析
方法表: 在上一次咱们已经分析到了字段信息了,如下: 紧接着就是方法相关的信息了: 而它展开之后的结构为: 所以往后数2个字节,看一下方法的总数: 3个方法,可咱们只定义了两个方法呀: 因为编译器会为 ...
- 极光推送出现 超时问题:Connect timeout. Please retry later. Error:7
检查之后均没有什么太大的问题, 最后发现出现77这种错误码,有一种可能就是系统的ca包没有更新 包名为 ca-certificates 使用命令 yum install ca-certificates ...
- python_面向对象——对象之间的关联关系
1.将类中的对象关联起来(简单的方法) class Person: def __init__(self,name,age,sex): self.name = name self.age = age s ...
- 前端知识体系:JavaScript基础-原型和原型链-new一个对象的详细过程,手动实现一个 new操作符
可以描述 new一个对象的详细过程,手动实现一个 new操作符 1. new 一个对象的详细过程:(原文地址) 首先我们看下new Person输出什么? var Person = function( ...
- IIS上传限制大小
加入下面的配置即可 <?xml version="1.0" encoding="UTF-8"?> <configuration> < ...
- Java进阶知识22 Spring execution 切入点表达式
1.概述 切入点(execution ):可以对指定的方法进行拦截,从而给指定的类生成代理对象.(拦截谁,就是在谁那里切入指定的程序/方法) 格式: execution(modifiers-pat ...
- AtCoder Beginner Contest 132
目录 Contest Info Solutions A. Fifty-Fifty B. Ordinary Number C. Divide the Problems D. Blue and Red B ...
- python基础-跨域问题
跨域 -- 浏览器的同源策略 阻止ajax请求 不阻止src请求 -- jsonp -- 我们利用src发送请求 -- core -- class MyCore(MiddlewareMixin): d ...