STL Algorithms 之 unique
C++的文档中说,STL中的unique是类似于这样实现的:
template <class ForwardIterator>
ForwardIterator unique ( ForwardIterator first, ForwardIterator last )
{
ForwardIterator result=first;
while (++first != last)
{
if (!(*result == *first)) // or: if (!pred(*result,*first)) for the pred version
*(++result)=*first;
}
return ++result;
}
仔细一看就知道,它并不是帮你直接把一个数组中所有重复的元素除去,而是对数组扫描一次,只看当前元素和前面一个元素,如果当前值和前面的值相等,那么跳过,否则就把这个值算上,迭代器递增,最后返回给你一个位置,表示我扫描到多少个当前值与其前面一个元素值不同的元素。
所以,要真正利用好unique,我们必须先对我们所需要进行unique操作的数组排序,然后再使用unique。
这样以后其实还是不满足我们的要求的,因为实际上unique函数实现的只是把不同的元素“unique”放到数组的前面,而数组的后面还有一段重复的没有去掉。这个时候就可以利用到unique函数的返回值啦,它返回的就是重复元素出现的第一个位置。
另外,unique函数可以接受两个参数(数组的开头,数组的末尾),也可以接受三个参数(数组的开头,数组的末尾,两个元素的比较(即定义怎样算元素相等))
看一下实例吧:
// resizing vector
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; bool myfunction (int i, int j) {
return (i==j);
} int main () {
int myints[] = {,,,,,,,,}; // 10 20 20 20 30 30 20 20 10
vector<int> myvector (myints,myints+);
vector<int>::iterator it; it = unique (myvector.begin(), myvector.end()); // 10 20 30 20 10 ? ? ? ?
//尖所指即it的位置 // ^ myvector.erase( it , myvector.end() ); // 第一种去掉末尾的方法 cout << "first: myvector contains:";
for (int i=;i<myvector.size();i++)
cout << " " << myvector[i];
cout<<endl; sort(myvector.begin(),myvector.end()); //先排序 it = unique (myvector.begin(), myvector.end(), myfunction);
// 使用比较函数,但此处是跟缺省的比较一样的。 myvector.resize( it - myvector.begin() ); // 10 20 30 cout << "second: myvector contains:";
for (it=myvector.begin(); it!=myvector.end(); ++it)
cout << " " << *it;
cout << endl; return ;
} /*
输出:
first: myvector contains: 10 20 30 20 10
second: myvector contains: 10 20 30
*/
STL Algorithms 之 unique的更多相关文章
- C++ STL算法系列 unique
类属性算法unique的作用是从输入序列中“删除”所有相邻的重复元素. 该算法删除相邻的重复元素,然后重新排列输入范围内的元素,并且返回一个迭代器(容器的长度没变,只是元素顺序改变了),表示无重复的值 ...
- C++STL中的unique函数解析
一.总述 unique函数属于STL中比较常用函数,它的功能是元素去重.即”删除”序列中所有相邻的重复元素(只保留一个).此处的删除,并不是真的删除,而是指重复元素的位置被不重复的元素给占领了(详细情 ...
- 数据离散化 ( 以及 stl 中的 unique( ) 的用法 )+ bzoj3289:Mato的文件管理
http://blog.csdn.net/gokou_ruri/article/details/7723378 ↑惯例Mark大神的博客 bzoj3289:Mato的文件管理 线段树求逆序对+莫队 ...
- STL中的unique()和lower_bound ,upper_bound
unique(): 作用:unique()的作用是去掉容器中相邻元素的重复元素(数组可以是无序的,比如数组可以不是按从小到大或者从大到小的排列方式) 使用方法:unique(初始地址,末地址): 这里 ...
- STL中的unique和unique_copy函数
一.unique函数 这个函数的功能就是删除相邻的重复元素,然后重新排列输入范围内的元素,并返回一个最后一个无重复值的迭代器(并不改变容器长度). 例如: vector<); ; i < ...
- C++STL中的unique函数
头文件:#include<iostream> 函数原型:iterator unique(iterator it_1,iterator it_2); 作用:元素去重,即”删除”序列中所有相邻 ...
- STL中map与hash_map容器的选择收藏
这篇文章来自我今天碰到的一个问题,一个朋友问我使用map和hash_map的效率问题,虽然我也了解一些,但是我不敢直接告诉朋友,因为我怕我说错了,通过我查询一些帖子,我这里做一个总结!内容分别来自al ...
- stl——vector详解
stl——vector详解 stl——vector是应用最广泛的一种容器,类似于array,都将数据存储于连续空间中,支持随机访问.相对于array,vector对空间应用十分方便.高效,迭代器使ve ...
- Effective STL 阅读笔记: Item 4 ~ 5: Call empty instead of checking size() against zero.
Table of Contents 1 Item 4: Call empty instead of checking size() against zero 2 Item 5: Prefer rang ...
随机推荐
- 【shell】shell编程(四)-循环语句
上篇我们学习了shell中条件选择语句的用法.接下来本篇就来学习循环语句.在shell中,循环是通过for, while, until命令来实现的.下面就分别来看看吧. for for循环有两种形式: ...
- Codeforces 271D - Good Substrings [字典树]
传送门 D. Good Substrings time limit per test 2 seconds memory limit per test 512 megabytes input stand ...
- R语言入门视频笔记--3-1--矩阵与数组
生成一个新矩阵,多用一些参数吧这次: x <- c(12,13,14,15) rname <- c("R1","R2") nname <- c ...
- Laravel 静态资源管理
<link rel="stylesheet" href="{{ asset('bootstrap/css/bootstrap.min.css') }}" ...
- Yii2之创建定时任务
yii开发的项目需要使用定时任务其实也可以使用一些单独的脚本文件来完成,但若是定时任务代码中需要使用到项目中的一些类,特别是需要使用应用对象Yii::$app的时候,单独的脚本想要完成就比较麻烦了.这 ...
- openssl搭建双向认证https
http://www.barretlee.com/blog/2015/10/05/how-to-build-a-https-server/ http://blog.163.com/fangjinbao ...
- Linux下使用curl进行http请求(转)
curl在Linux下默认已经安装,Windows需要自行安装. 下载地址:https://curl.haxx.se/download.html Windows离线版本:链接:http://pan.b ...
- android官方Api 理解Activity生命周期的回调机制(适合有基础的人看)
原文地址:http://www.android-doc.com/training/basics/activity-lifecycle/starting.html#lifecycle-states 此处 ...
- iOS类的合理设计,面向对象思想
每天更新的东西可能有反复的内容.当时每一部分的知识点是不同的,须要大家认真阅读 这里介绍了iOS类的合理设计.面向对象思想 main.m #import <Foundation/Foundati ...
- (转) SYSTEM_HANDLE_INFORMATION中ObjectTypeIndex的定义
typedef struct _SYSTEM_HANDLE_TABLE_ENTRY_INFO{ USHORT UniqueProcessId; USHORT CreatorBackTraceIndex ...