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

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

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

移除某些特定元素

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. nginx部署web.py项目

    = =测试环境直接就python index.py就好啦 生产环境nginx + web.py + uwsgi 安装uwsgi... pip install uwsgi 首先把自己的代码小改一下... ...

  2. Charles安装

    Charles 是一个网络抓包工具,在做 APP 抓包的时候会用到,相比 Fiddler 来说,Charles 的功能更为强大,而且跨平台支持更好,所以在这里我们选用 Charles 来作为主要的移动 ...

  3. 你不一定知道的、并没有什么卵用的一些python库

    1. delorean,用来处理时间的库 import datetime import pytz # 一般情况下,我们想表示时间的话 est = pytz.timezone("Asia/Sh ...

  4. hdu 1856(hash+启发式并查集)

    More is better Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others) ...

  5. J.U.C并发框架源码阅读(十五)CopyOnWriteArrayList

    基于版本jdk1.7.0_80 java.util.concurrent.CopyOnWriteArrayList 代码如下 /* * Copyright (c) 2003, 2011, Oracle ...

  6. POJ 2184 Cow Exhibition【01背包+负数(经典)】

    POJ-2184 [题意]: 有n头牛,每头牛有自己的聪明值和幽默值,选出几头牛使得选出牛的聪明值总和大于0.幽默值总和大于0,求聪明值和幽默值总和相加最大为多少. [分析]:变种的01背包,可以把幽 ...

  7. hadoop之linux常用命令

    Linux的命令后面会有命令选项,有的选项还有选项值.选项的前面有短横线“-”,命令.选项.选项值之间使用空格隔开.有的命令没有选项,会有参数.选项是命令内置的功能,参数是用户提供的符合命令格式的内容 ...

  8. Spring/Spring MVC/Spring Boot实现跨域

    说明:Spring MVC和Spring Boot其实用的都是同一套. CORS介绍请看这里:https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Acc ...

  9. 自动生成logo的网址

    1.https://www.logaster.com/logo/

  10. html 打印代码,支持翻页

    ylbtech_html_print html打印代码,支持翻页 <html> <head> <meta name=vs_targetSchema content=&qu ...