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环境下的图形系统和AMD R600显卡编程(11)——R600指令集
1 低级着色语言tgsi OpenGL程序使用GLSL语言对可编程图形处理器进行编程,GLSL语言(以下高级着色语言就是指GLSL)是语法类似C的高级语言,在GLSL规范中,GLSL语言被先翻译成教低 ...
- flask的orm框架(SQLAlchemy)-操作数据
# 原创,转载请留言联系 Flask-SQLAlchemy 实现增加数据 用 sqlalchemy 添加数据时,一定要注意,不仅仅要连接到数据表,并且你的创建表的类也必须写进来.而且字段和约束条件要吻 ...
- asp.net生成word文档服务器配置
一.asp.net生成word文档,布署到正式的服务器上就出现 错误:System.Runtime.InteropServices.COMException (0x800A1098 ...
- [BZOJ4760][Usaco2017 Jan]Hoof, Paper, Scissors dp
4760: [Usaco2017 Jan]Hoof, Paper, Scissors Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 136 Solv ...
- 前端设计的常用属性,CSS的盒模型,页面布局的利器
在CSS和HTML结合布局页面的过程中,有一组被人们称为“盒属性”的CSS样式,被广泛的使用到.相信经常布局写页面的朋友们对盒属性一定不陌生.在CSS技术的发展过程中,盒属性也有了许多次改进,今天小编 ...
- 原来是adblock惹的祸
一个在本地开发好的网站,放到服务器就不行了.花了好几个小时的时间,最后试着把adblock关了,然后正常了.
- 华农oj Problem J: 幻化【贪心/抽屉原理】
Problem J: 幻化 Time Limit: 2 Sec Memory Limit: 128 MB Submit: 18 Solved: 3 [Submit][Status][Web Board ...
- #420 Div2 D
#420 Div2 D 题意 给出一个方格矩阵,其中存在亮着的方格,只能在亮着的方格上行走,可以在初始亮的方格上花费一枚硬币临时点亮任意一行或一列,地图上同一时间只能存在一个这样的行或列,问走到终点最 ...
- Reverse Words in a String II -- LeetCode
Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...
- List和ArrayList的区别和联系
1. List是一个接口,而ArrayList是List接口的一个实现类. ArrayList类继承并实现了List接口. 因此,List接口不能被构造,也就是我们说的不能创建实例对象,但是我们 ...