STL学习笔记(移除性算法)
本节所列的算法是根据元素值或某一准则,在一个区间内移除某些元素。
这些算法并不能改变元素的数量,它们只是将原本置于后面的“不移除元素”向前移动,覆盖那些被移除的元素。
这些算法都返回逻辑上的新终点
移除某些特定元素
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学习笔记(移除性算法)的更多相关文章
- Effective STL 学习笔记 31:排序算法
Effective STL 学习笔记 31:排序算法 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
- STL学习笔记(非变动性算法)
辅助函数 本节跟以后几节将对所有STL算法逐一详细讨论.为了简化这些例子,我们使用了一些辅助函数,分别用于对容器进行输出跟插入操作. #ifndef ALGOSTUFF_HPP #define ALG ...
- STL学习笔记7 ---- algorithm(算法)
STL中算可以分为三种, 1.变序型队列算法,可以改变容器内的数据: 2.非变序型队列算法,处理容器内的数据而不改变他们 : 3.通用数值算法,这涉及到很多专业领域的算术操作,这里不做介绍. 第一是变 ...
- Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据
Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据 */--> div.org-src-container { font-size: 85%; font-fam ...
- STL学习笔记--算法
关于STL算法需要注意的是: (1) 所有STL算法被设计用来处理一个或多个迭代器区间.第一个区间通常以起点和终点表示,至于其他区间,多数情况下只需提供起点即可,其终点可自动以第一区间的元素数推导出来 ...
- STL学习笔记(算法概述)
算法头文件 要运用C++标准程序库的算法,首先必须包含头文件<algorithm> 使用STL算法时,经常需要用到仿函数以及函数配接器.它们定义域<functional>头文件 ...
- 机器学习实战(Machine Learning in Action)学习笔记————08.使用FPgrowth算法来高效发现频繁项集
机器学习实战(Machine Learning in Action)学习笔记————08.使用FPgrowth算法来高效发现频繁项集 关键字:FPgrowth.频繁项集.条件FP树.非监督学习作者:米 ...
- 机器学习实战(Machine Learning in Action)学习笔记————07.使用Apriori算法进行关联分析
机器学习实战(Machine Learning in Action)学习笔记————07.使用Apriori算法进行关联分析 关键字:Apriori.关联规则挖掘.频繁项集作者:米仓山下时间:2018 ...
- Effective STL 学习笔记 39 ~ 41
Effective STL 学习笔记 39 ~ 41 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
- 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 ...
随机推荐
- linux select函数详解【转】
转自:http://www.cnblogs.com/ccsccs/articles/4224253.html 在Linux中,我们可以使用select函数实现I/O端口的复用,传递给 select函数 ...
- Activity管理类
package com.yunpai.tms.application; import android.app.Activity; import android.app.ActivityManager; ...
- Nodejs将Buffer转化成Stream
编写接口的时候经常需要将上传的文件保存到数据库的情况,在nodejs中文件上传可以使用multer来接收上传的文件.如果不想保存到本地,而是直接保存到mongodb中,就要将buffer对象转化成流再 ...
- poj 1329(已知三点求外接圆方程.)
Circle Through Three Points Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3766 Acce ...
- hdu 1598(最小生成树)
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- PHP源码加密- php-beast
php-beast 详细介绍 使用案例: http://www.beastcoder.com/ PHP Beast是一个源码加密模块,使用这个模块可以把PHP源码加密并在此模块下运行. 为什么要用PH ...
- CentOS系统最小化安装没有wget解决方案
-bash: wget: command not found的两种解决方法 今天给服务器安装新LNMP环境时,wget 时提示 -bash:wget command not found,很明显没有安装 ...
- HDU 6336 子矩阵求和
Problem E. Matrix from Arrays Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 ...
- HDU 2639 Bone Collector II【01背包 + 第K大价值】
The title of this problem is familiar,isn't it?yeah,if you had took part in the "Rookie Cup&quo ...
- 51Nod - 1405 树的距离之和(树形DP)
1405 树的距离之和 题意 给定一棵无根树,假设它有n个节点,节点编号从1到n,求任意两点之间的距离(最短路径)之和. 分析 树形DP. 首先我们让 \(1\) 为根.要开两个数组 \(up \ d ...