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

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

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

移除某些特定元素

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. memset和wmemset用法记录

    char cBuffer[50] ;TCHAR wBuffer[50]; 1.将cBuffer初始化为空格memset(cBuffer, ' ', sizeof(cBuffer));第三个参数为字节数 ...

  2. java 24小时倒计时案例

    import java.util.Calendar; import java.util.Date; public class Daojishi { static String Countdown=&q ...

  3. chrome报错a parser-blocking, cross-origin script, is invoked via document.write.

    确切的说是警告,内容是: A Parser-blocking, cross-origin script, http://s4.cnzz.com/stat.php?id=xxx&show=pic ...

  4. Jquery学习之路(一) 实现checkbox全选方法

    昨天早上有写到怎么利用Jquery实现全选 根据大家的意见对程序中一些写法不好的地方进行了修改,也是本人水平有限,存在各种考虑不到的地方. 文章最后我提出了一个问题,要写一个通用的方法来调用,于是就有 ...

  5. POJ 2528.Mayor's posters-线段树(成段替换、离散数据、简单hash)

    POJ2528.Mayor's posters 这道题真的是线段数的经典的题目,因为数据很大,直接建树的话肯定不可以,所以需要将数据处理一下,没有接触离散化的时候感觉离散化这个东西相当高级,其实在不知 ...

  6. (1)安装Xamarin

    ()一.安装 1.安装xamarin 2.下载jdk8 https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads- ...

  7. 牛客网练习赛18 A 【数论/整数划分得到乘积最大/快速乘】

    链接:https://www.nowcoder.com/acm/contest/110/A 来源:牛客网 题目描述 这题要你回答T个询问,给你一个正整数S,若有若干个正整数的和为S,则这若干的数的乘积 ...

  8. 宠物收养所 (SBT)

    宠物收养所 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特殊的公式,得 ...

  9. sql with multiply where

    I am wondering if this is a valid query: UPDATE table SET ID = 111111259 WHERE ID = 2555 AND SET ID ...

  10. Sharepoint 查阅项字段和计算值字段的定义

    查阅项字段定义 <Field Type="Lookup" DisplayName="test2" Required="FALSE" E ...