本节所列的算法是根据元素值或某一准则,在一个区间内移除某些元素。

这些算法并不能改变元素的数量,它们只是将原本置于后面的“不移除元素”向前移动,覆盖那些被移除的元素。

这些算法都返回逻辑上的新终点

移除某些特定元素

1.移除某序列内的元素

ForwardIterator

remove(ForwardIterator beg,ForwardIterator end,

const T& value)

ForwardIterator

remove_if(ForwardIterator beg,ForwardIterator end,

UnaryPredicate op)

1.remove()会移除区间[beg,end)中每一个“与value相等”的元素

2.remove_if()会移除区间[beg,end)中每一个“令以下一元判断式”: op(elem) 为true的元素。

下面程序示范remove()和remove_if()的用法:

 #include "algostuff.hpp"
using namespace std; int main()
{
vector<int> coll;
INSERT_ELEMENTS(coll,,);
INSERT_ELEMENTS(coll,,);
INSERT_ELEMENTS(coll,,);
PRINT_ELEMENTS(coll,"coll: ");
vector<int>::iterator pos;
pos=remove(coll.begin(),coll.end(),);
PRINT_ELEMENTS(coll,"size not changed: ");
coll.erase(pos,coll.end());
PRINT_ELEMENTS(coll,"size changed: ");
coll.erase(remove_if(coll.begin(),coll.end(),
bind2nd(less<int>(),)),coll.end());
PRINT_ELEMENTS(coll,"< 4 removed: ");
}

2.复制时一并移除元素

OutputIterator

remove_copy(InputIterator sourceBeg,InputIterator sourceEnd,

OutputIterator destBeg,

const T& value)

OutputIterator

remove_copy_if(InputIterator sourceBeg,InputIterator sourceEnd,

OutputIterator destBeg,

UnaryPredicate op)

1.remove_copy()是copy()和remove()组合。它将源区间[beg,end)内的所有元素赋值到“以destBeg为起点”的目标区间内。

并在复制过程中移除“与value相等”的所有元素

2.remove_copy_if是copy()和remove_if()的组合

以下程序示范remove_copy()和remove_copy_if的用法

 #include <iterator>
#include "algostuff.hpp"
using namespace std; int main()
{
list<int> coll1;
INSERT_ELEMENTS(coll1,,);
INSERT_ELEMENTS(coll1,,);
PRINT_ELEMENTS(coll1);
remove_copy(coll1.begin(),coll1.end(),ostream_iterator<int>(cout," "),);
cout<<endl;
remove_copy_if(coll1.begin(),coll1.end(),ostream_iterator<int>(cout," "),bind2nd(greater<int>(),));
cout<<endl;
multiset<int> coll2;
remove_copy_if(coll1.begin(),coll1.end(),inserter(coll2,coll2.end()),bind2nd(less<int>(),));
PRINT_ELEMENTS(coll2);
}

移除重复元素

1.移除连续重复元素

ForwardIterator

unique(ForwardIterator beg,ForwardIterator end)

ForwardIterator

unique(ForwardIterator beg,ForwardIterator end

BinaryPredicate op)

1.以上两种形式都会移除连续重复元素中的多余元素

2.第一形式将区间[beg,end)内所有“与前一元素相等的元素“移除。

3.第二形式将每一个”位于元素e之后并且造成以下二元判断式:op(elem,e)结果为true”的所有elem元素移除。

换言之此判断式并非用来将元素和其原本的前一元素比较,而是将它和未被移除的前一元素比较。

下面程序示范unique()的用法

 #include <iterator>
#include "algostuff.hpp"
using namespace std; int main()
{
int source[]={,,,,,,,,,,,,,,,,};
int sourceNum=sizeof(source)/sizeof(source[]);
list<int> coll;
copy(source,source+sourceNum,back_inserter(coll));
PRINT_ELEMENTS(coll);
list<int>::iterator pos;
pos=unique(coll.begin(),coll.end());
copy(coll.begin(),pos,ostream_iterator<int>(cout," "));
cout<<endl<<endl;
copy(source,source+sourceNum,coll.begin());
PRINT_ELEMENTS(coll);
coll.erase(unique(coll.begin(),coll.end(),greater<int>()),coll.end());
PRINT_ELEMENTS(coll);
}

2.复制过程中移除重复元素

OutputIterator

unique_copy(InputIterator sourceBeg,InputIterator sourceEnd,

OutputIterator destBeg)

OutputIterator

unique_copy(InputIterator sourceBeg,InputIterator sourceEnd,

OutputIterator destBeg,

BinaryPredicate op)

两种形式都是copy()和unique()的组合

下面程序示范unique_copy()的用法

 #include <iterator>
#include "algostuff.hpp"
using namespace std; bool differenceOne(int elem1,int elem2)
{
return elem1+==elem2||elem1-==elem2;
} int main()
{
int source[]={,,,,,,,,,,,,,,,,};
int sourceNum=sizeof(source)/sizeof(source[]);
list<int> coll;
copy(source,source+sourceNum,back_inserter(coll));
PRINT_ELEMENTS(coll);
unique_copy(coll.begin(),coll.end(),ostream_iterator<int>(cout," "));
cout<<endl;
unique_copy(coll.begin(),coll.end(),ostream_iterator<int>(cout," "),differenceOne);
cout<<endl;
}

STL学习笔记(移除性算法)的更多相关文章

  1. Effective STL 学习笔记 31:排序算法

    Effective STL 学习笔记 31:排序算法 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...

  2. STL学习笔记(非变动性算法)

    辅助函数 本节跟以后几节将对所有STL算法逐一详细讨论.为了简化这些例子,我们使用了一些辅助函数,分别用于对容器进行输出跟插入操作. #ifndef ALGOSTUFF_HPP #define ALG ...

  3. STL学习笔记7 ---- algorithm(算法)

    STL中算可以分为三种, 1.变序型队列算法,可以改变容器内的数据: 2.非变序型队列算法,处理容器内的数据而不改变他们 : 3.通用数值算法,这涉及到很多专业领域的算术操作,这里不做介绍. 第一是变 ...

  4. Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据

    Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据 */--> div.org-src-container { font-size: 85%; font-fam ...

  5. STL学习笔记--算法

    关于STL算法需要注意的是: (1) 所有STL算法被设计用来处理一个或多个迭代器区间.第一个区间通常以起点和终点表示,至于其他区间,多数情况下只需提供起点即可,其终点可自动以第一区间的元素数推导出来 ...

  6. STL学习笔记(算法概述)

    算法头文件 要运用C++标准程序库的算法,首先必须包含头文件<algorithm> 使用STL算法时,经常需要用到仿函数以及函数配接器.它们定义域<functional>头文件 ...

  7. 机器学习实战(Machine Learning in Action)学习笔记————08.使用FPgrowth算法来高效发现频繁项集

    机器学习实战(Machine Learning in Action)学习笔记————08.使用FPgrowth算法来高效发现频繁项集 关键字:FPgrowth.频繁项集.条件FP树.非监督学习作者:米 ...

  8. 机器学习实战(Machine Learning in Action)学习笔记————07.使用Apriori算法进行关联分析

    机器学习实战(Machine Learning in Action)学习笔记————07.使用Apriori算法进行关联分析 关键字:Apriori.关联规则挖掘.频繁项集作者:米仓山下时间:2018 ...

  9. Effective STL 学习笔记 39 ~ 41

    Effective STL 学习笔记 39 ~ 41 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...

  10. Effective STL 学习笔记 Item 38 : Design functor classes for pass-by-value

    Effective STL 学习笔记 Item 38 : Design functor classes for pass-by-value */--> div.org-src-container ...

随机推荐

  1. 【程序打包工具 Inno Setup】CreateProcess 失败:代码 740(Inno Setup打包的程序提升为管理员权限)

    原文参考 https://www.cnblogs.com/SnailProgramer/p/4243666.html http://blog.csdn.net/x356982611/article/d ...

  2. jquery_EasyUI使用细节注意

    一.属性key不加双引号,value加双引号,对于url的value,添加‘’单引号:url的访问地址可以使用以下格式: datagrid中的url格式: var datagrid; $(functi ...

  3. 关于多态的理解,有助于理解TStream抽象类的多态机制。

    有的时候 不是很明白流的机制,因为有内存流  文件流 图片流 等等 他们之间的相互转化 靠的就是流的多态性.... unit Unit11; interface uses Winapi.Windows ...

  4. 网易 监控 openstack

    http://www.360doc.com/content/16/0416/08/13792507_551022987.shtml

  5. win2008通过计划任务定时执行bat文件

    前段时间在Windows Server 2008安装了一套基于MySQL数据库的软件,处于数据安全的考虑,希望每天能够自动进行数据库备份.我在别人脚本的基础上自己写了一个数据库备份的bat脚本,双击该 ...

  6. Hadoop打包成jar包在集群上运行时出现的各种问题以及解决方案

    之前将eclipse下编好的mapreduce代码放到集群上面跑,发现速度很慢,namenode节点的cpu和内存使用率很低,datanode节点基本上处于没有运行的状态,然后通过查看hadoop-e ...

  7. Codeforces Round #444 (Div. 2)A. Div. 64【进制思维】

    A. Div. 64 time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  8. UOJ 180【UR #12】实验室外的攻防战

    http://uoj.ac/contest/25/problem/180 从前往后对比串A,B 当$A_i,B_i$不相同时找到$B_i$在A中的位置j 若$min{A_1,A_2,A_3...... ...

  9. mysql----kill慢查询

    每个与mysqld的连接都在一个独立的线程里运行,您可以使用SHOW PROCESSLIST语句查看哪些线程正在运行,并使用KILL thread_id语句终止一个线程. 如果您拥有SUPER权限,您 ...

  10. 动态规划 001 - 编辑距离(Levenshtein Distance)问题

    问题 字符串的编辑距离也被称为距Levenshtein距离(Levenshtein Distance),属于经典算法,常用方法使用递归,更好的方法是使用动态规划算法,以避免出现重叠子问题的反复计算,减 ...