作用:从序列中删除指定元素。

声明:

  1. #include <algorithm>
  2. template <class forwardItr,class Type>
  3. forwardItr remove(forwardItr first, forwardItr last, const Type& value);
  4. template <class forwardItr, class unaryPredicate>
  5. forwardItr remove_if(forwardItr first, forwardItr last, unaryPredicate op);
  6. template <class inputItr,class outputItr,class Type>
  7. outputItr remove_copy(inputItr first1, inputItr last1, outputItr destFirst, const Type& value);
  8. template <class inputItr,class outputItr, class unaryPredicate>
  9. outputItr remove_copy_if(inputItr first1, inputItr last1, outputItr destFirst, unaryPredicate op);

示例代码:

  1. #include <iostream>
  2. #include <list>
  3. #include <string>
  4. #include <numeric>
  5. #include <iterator>
  6. #include <vector>
  7. #include <functional>
  8. #include <algorithm>
  9. using namespace std;
  10. bool lessThanEqualTo50(int num) {
  11. return (num <= 50);
  12. }
  13. int main() {
  14. char cList[10] = {'A','a','A','B','A','c','D','e','F','A'};
  15. vector<char> charList(cList, cList + 10);
  16. vector<char>::iterator lastElem,newLastElem;
  17. ostream_iterator<char> screen(cout, " ");
  18. cout << "charList:" << endl;
  19. copy(charList.begin(),charList.end(),screen);
  20. cout << endl;
  21. // remove:去除容器中的所有的A
  22. // 函数返回新区间最后一个元素的下一个位置
  23. lastElem = remove(charList.begin(),charList.end(),'A');
  24. cout << "charList remove A:" << endl;
  25. copy(charList.begin(),lastElem,screen);
  26. cout << endl;
  27. // remove_if:去掉所有的大写字母
  28. // 返回新区间的最后一个元素的下一个位置。
  29. newLastElem = remove_if(charList.begin(),charList.end(),isupper);
  30. cout << "charList remove if Upper:" << endl;
  31. // 显示新区间的所有元素
  32. copy(charList.begin(),newLastElem,screen);
  33. cout << endl;
  34. int list[10] = {12,34,56,21,34,78,34,55,12,25};
  35. vector<int> intList(list,list+10);
  36. vector<int>::iterator endElement,newEndElement;
  37. ostream_iterator<int> screenInt(cout, " ");
  38. cout << "intList:" << endl;
  39. copy(intList.begin(),intList.end(),screenInt);
  40. cout << endl;
  41. vector<int> temp1(10);
  42. // 将 intList 中除值为34以外的元素,输出到temp1中,不改变 intList。
  43. endElement = remove_copy(intList.begin(),intList.end(),temp1.begin(),34);
  44. cout << "temp1:" << endl;
  45. copy(temp1.begin(),endElement,screenInt);
  46. cout << endl;
  47. cout << "intList:" << endl;
  48. copy(intList.begin(),intList.end(),screenInt);
  49. cout << endl;
  50. vector<int> temp2(10,0);
  51. // remove_copy_if
  52. // 将intList中大于50的元素输送到 temp2中。
  53. newEndElement = remove_copy_if(intList.begin(),intList.end(),temp2.begin(),lessThanEqualTo50);
  54. cout << "temp2:" << endl;
  55. copy(temp2.begin(),temp2.end(),screenInt);
  56. cout << endl;
  57. cout << "temp2:" << endl;
  58. copy(temp2.begin(),newEndElement,screenInt);
  59. cout << endl;
  60. return 0;
  61. }

运行结果:

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相关推荐

  1. STL之remove、remove_copy、remove_if、remove_copy_if

    需要的头文件: algorithm 源码: //remove template <class _ForwardIter, class _Tp> _ForwardIter remove(_F ...

  2. C++ remove、remove_copy、remove_if和remove_copy_if函数使用详解

    如果不知道具体的场景,即元素保存在什么样的容器中,是不能从序列中移除元素的.因此,"移除元素的"算法也无法做到这一点,它们只会重写被选择的元素或者忽略复制的元素.移除操作不会改变被 ...

  3. 开课吧:C++STL常用remove算法有哪些?

    C++标准库提供了大量可以用来对容器及其他序列进行算法操作的函数.这些组件可以是函数或者函数模板,大部分由头文件 提供.以下四个remove相关的函数就包含在头文件algorithm中. 四个remo ...

  4. STL的remove函数和list的remove成员函数

    今天看书刚刚看的,就记录下来吧.这可能是老生常谈了,权且作为一个警醒的例子吧. 大家都知道STL有两个非常重要的组成部分,容器和算法. 算法就是一个个的函数,通过迭代器和容器关联在一起,完成一些工作. ...

  5. c++高级编程学习笔记4

    C++运算符重载 运算符重载概述 根据第 1 章的描述,C++中的运算符是一些类似于+.<.*和<<的符号.这些运算符可应用于内建类型,例如 int 和 double,从而实现算术操 ...

  6. 【C++】C++STL标准模板库

    一.STL的基本概念 1.什么是STL STL (Standard Template Librany)标准准模板庠是惠普实验室开发的一系列软件的统称.现在主要出现在C++中,但在被引入C++之前该技木 ...

  7. 【C++】Effective STL:50条有效使用STL的经验

    第一条:慎重选择容器类型 1.C++容器:先混个眼熟 序列容器:array.vector.string.deque.list.forward_list 有序关联容器:set.map.multiset. ...

  8. 最全ACM常用STL

    STL 中专门用于排列的函数(可以处理存在重复数据集的排列问题) 头文件:#include <algorithm> using namespace std; 调用: next_permut ...

  9. ACM竞赛常用STL(二)之STL--algorithm

    <algorithm>无疑是STL 中最大的一个头文件,它是由一大堆模板函数组成的. 下面列举出<algorithm>中的模板函数: adjacent_find / binar ...

最新文章

  1. PHPExcel对于Excel中日期和时间类型的处理
  2. JavaScript高级程序设计44.pdf
  3. Python 实现程序的单一实例
  4. 求最大和 java_三种算法求最大子段和问题——Java实现
  5. 苹果收购Siri的八年,是成还是败?
  6. 云栖社区 mysql_mysql
  7. vscode安卓html扩展,vscode扩展信息.html
  8. 【Python】导入类
  9. 【COCOS2DX-LUA 脚本开发之十二】Hybrid模式-利用AssetsManager实现在线更新脚本文件lua、js、图片等资源(免去平台审核周期)...
  10. flask-script插件
  11. 【KITTI可视化】kitti三维目标标注可视化
  12. Python实现Excel和TXT文件格式的转换
  13. A7芯片 IOS降级 跳过ID | ipad Mini2降级 10.3.3
  14. 使用ensp搭建简单校园网拓扑
  15. 转《腾讯大讲堂11 拍拍ce工作经验分享》
  16. js自执行函数(function(){})()前加个分号是什么意思?
  17. 用dxdiag命令show计算机系统配置
  18. 【2016】【论文笔记】差频可调谐THz技术——
  19. 史上最“奇葩”相亲男惊呆网友:你那么普通,却那么自信
  20. 力扣-无重复字符的最长子串

热门文章

  1. TortoiseSVN操作
  2. Hmac - Java加密与安全
  3. exi 虚拟服务器,图文教程:如何在ESXi主机上部署VMware Tools 10
  4. mysql5.58_mysql5.58编译安装手记
  5. brother标签打印软件_标签打印软件如何设计食品留样标签模板
  6. MySQL 下载与配置教程(免安装版)
  7. 200922阶段一C++关联容器map
  8. 【报错笔记】MAVEN pom.xml 报错解决方法
  9. HID接口设备-简介
  10. C# textBox1.Append/Text实现换行