STL 之remove,remove_if,remove_copy,remove_copy_if
作用:从序列中删除指定元素。
声明:
- #include <algorithm>
- template <class forwardItr,class Type>
- forwardItr remove(forwardItr first, forwardItr last, const Type& value);
- template <class forwardItr, class unaryPredicate>
- forwardItr remove_if(forwardItr first, forwardItr last, unaryPredicate op);
- template <class inputItr,class outputItr,class Type>
- outputItr remove_copy(inputItr first1, inputItr last1, outputItr destFirst, const Type& value);
- template <class inputItr,class outputItr, class unaryPredicate>
- outputItr remove_copy_if(inputItr first1, inputItr last1, outputItr destFirst, unaryPredicate op);
示例代码:
- #include <iostream>
- #include <list>
- #include <string>
- #include <numeric>
- #include <iterator>
- #include <vector>
- #include <functional>
- #include <algorithm>
- using namespace std;
- bool lessThanEqualTo50(int num) {
- return (num <= 50);
- }
- int main() {
- char cList[10] = {'A','a','A','B','A','c','D','e','F','A'};
- vector<char> charList(cList, cList + 10);
- vector<char>::iterator lastElem,newLastElem;
- ostream_iterator<char> screen(cout, " ");
- cout << "charList:" << endl;
- copy(charList.begin(),charList.end(),screen);
- cout << endl;
- // remove:去除容器中的所有的A
- // 函数返回新区间最后一个元素的下一个位置
- lastElem = remove(charList.begin(),charList.end(),'A');
- cout << "charList remove A:" << endl;
- copy(charList.begin(),lastElem,screen);
- cout << endl;
- // remove_if:去掉所有的大写字母
- // 返回新区间的最后一个元素的下一个位置。
- newLastElem = remove_if(charList.begin(),charList.end(),isupper);
- cout << "charList remove if Upper:" << endl;
- // 显示新区间的所有元素
- copy(charList.begin(),newLastElem,screen);
- cout << endl;
- int list[10] = {12,34,56,21,34,78,34,55,12,25};
- vector<int> intList(list,list+10);
- vector<int>::iterator endElement,newEndElement;
- ostream_iterator<int> screenInt(cout, " ");
- cout << "intList:" << endl;
- copy(intList.begin(),intList.end(),screenInt);
- cout << endl;
- vector<int> temp1(10);
- // 将 intList 中除值为34以外的元素,输出到temp1中,不改变 intList。
- endElement = remove_copy(intList.begin(),intList.end(),temp1.begin(),34);
- cout << "temp1:" << endl;
- copy(temp1.begin(),endElement,screenInt);
- cout << endl;
- cout << "intList:" << endl;
- copy(intList.begin(),intList.end(),screenInt);
- cout << endl;
- vector<int> temp2(10,0);
- // remove_copy_if
- // 将intList中大于50的元素输送到 temp2中。
- newEndElement = remove_copy_if(intList.begin(),intList.end(),temp2.begin(),lessThanEqualTo50);
- cout << "temp2:" << endl;
- copy(temp2.begin(),temp2.end(),screenInt);
- cout << endl;
- cout << "temp2:" << endl;
- copy(temp2.begin(),newEndElement,screenInt);
- cout << endl;
- return 0;
- }
运行结果:
charList:
A a A B A c D e F A
charList remove A:
a B c D e F
charList remove if Upper:
a c e e
intList:
12 34 56 21 34 78 34 55 12 25
temp1:
12 56 21 78 55 12 25
intList:
12 34 56 21 34 78 34 55 12 25
temp2:
56 78 55 0 0 0 0 0 0 0
temp2:
56 78 55
STL 之remove,remove_if,remove_copy,remove_copy_if相关推荐
- STL之remove、remove_copy、remove_if、remove_copy_if
需要的头文件: algorithm 源码: //remove template <class _ForwardIter, class _Tp> _ForwardIter remove(_F ...
- C++ remove、remove_copy、remove_if和remove_copy_if函数使用详解
如果不知道具体的场景,即元素保存在什么样的容器中,是不能从序列中移除元素的.因此,"移除元素的"算法也无法做到这一点,它们只会重写被选择的元素或者忽略复制的元素.移除操作不会改变被 ...
- 开课吧:C++STL常用remove算法有哪些?
C++标准库提供了大量可以用来对容器及其他序列进行算法操作的函数.这些组件可以是函数或者函数模板,大部分由头文件 提供.以下四个remove相关的函数就包含在头文件algorithm中. 四个remo ...
- STL的remove函数和list的remove成员函数
今天看书刚刚看的,就记录下来吧.这可能是老生常谈了,权且作为一个警醒的例子吧. 大家都知道STL有两个非常重要的组成部分,容器和算法. 算法就是一个个的函数,通过迭代器和容器关联在一起,完成一些工作. ...
- c++高级编程学习笔记4
C++运算符重载 运算符重载概述 根据第 1 章的描述,C++中的运算符是一些类似于+.<.*和<<的符号.这些运算符可应用于内建类型,例如 int 和 double,从而实现算术操 ...
- 【C++】C++STL标准模板库
一.STL的基本概念 1.什么是STL STL (Standard Template Librany)标准准模板庠是惠普实验室开发的一系列软件的统称.现在主要出现在C++中,但在被引入C++之前该技木 ...
- 【C++】Effective STL:50条有效使用STL的经验
第一条:慎重选择容器类型 1.C++容器:先混个眼熟 序列容器:array.vector.string.deque.list.forward_list 有序关联容器:set.map.multiset. ...
- 最全ACM常用STL
STL 中专门用于排列的函数(可以处理存在重复数据集的排列问题) 头文件:#include <algorithm> using namespace std; 调用: next_permut ...
- ACM竞赛常用STL(二)之STL--algorithm
<algorithm>无疑是STL 中最大的一个头文件,它是由一大堆模板函数组成的. 下面列举出<algorithm>中的模板函数: adjacent_find / binar ...
最新文章
- PHPExcel对于Excel中日期和时间类型的处理
- JavaScript高级程序设计44.pdf
- Python 实现程序的单一实例
- 求最大和 java_三种算法求最大子段和问题——Java实现
- 苹果收购Siri的八年,是成还是败?
- 云栖社区 mysql_mysql
- vscode安卓html扩展,vscode扩展信息.html
- 【Python】导入类
- 【COCOS2DX-LUA 脚本开发之十二】Hybrid模式-利用AssetsManager实现在线更新脚本文件lua、js、图片等资源(免去平台审核周期)...
- flask-script插件
- 【KITTI可视化】kitti三维目标标注可视化
- Python实现Excel和TXT文件格式的转换
- A7芯片 IOS降级 跳过ID | ipad Mini2降级 10.3.3
- 使用ensp搭建简单校园网拓扑
- 转《腾讯大讲堂11 拍拍ce工作经验分享》
- js自执行函数(function(){})()前加个分号是什么意思?
- 用dxdiag命令show计算机系统配置
- 【2016】【论文笔记】差频可调谐THz技术——
- 史上最“奇葩”相亲男惊呆网友:你那么普通,却那么自信
- 力扣-无重复字符的最长子串
热门文章
- TortoiseSVN操作
- Hmac - Java加密与安全
- exi 虚拟服务器,图文教程:如何在ESXi主机上部署VMware Tools 10
- mysql5.58_mysql5.58编译安装手记
- brother标签打印软件_标签打印软件如何设计食品留样标签模板
- MySQL 下载与配置教程(免安装版)
- 200922阶段一C++关联容器map
- 【报错笔记】MAVEN pom.xml 报错解决方法
- HID接口设备-简介
- C# textBox1.Append/Text实现换行