具体实现见中间源码
function template
<algorithm>

std::unique

equality (1)
template <class ForwardIterator>
ForwardIterator unique (ForwardIterator first, ForwardIterator last);
predicate (2)
template <class ForwardIterator, class BinaryPredicate>
ForwardIterator unique (ForwardIterator first, ForwardIterator last,
BinaryPredicate pred);
Remove consecutive duplicates in range

Removes all but the first element from every consecutive group of equivalent elements in the range [first,last).



The function cannot alter the properties of the object containing the range of elements (i.e., it cannot alter the size of an array or a container): The removal is done by replacing the duplicate elements by the next element that is not a duplicate, and signaling
the new size of the shortened range by returning an iterator to the element that should be considered its new past-the-end element.



The relative order of the elements not removed is preserved, while the elements between the returned iterator and lastare left in a valid but unspecified state.



The function uses operator== to compare the pairs of elements (or pred, in version (2)).



The behavior of this function template is equivalent to:

1
2
3
4
5
6
7
8
9
10
11
12
13
template <class ForwardIterator>
ForwardIterator unique (ForwardIterator first, ForwardIterator last)
{
if (first==last) return last; ForwardIterator result = first;
while (++first != last)
{
if (!(*result == *first)) // or: if (!pred(*result,*first)) for version (2)
*(++result)=*first;
}
return ++result;
}
 

Parameters

first, last
Forward iterators to the initial and final positions of the sequence of move-assignable elements.
The range used is[first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
pred
Binary function that accepts two elements in the range as argument, and returns a value convertible to bool. The value returned indicates whether both arguments are considered equivalent (if true, they
are equivalent and one of them is removed).

The function shall not modify any of its arguments.

This can either be a function pointer or a function object.

Return value

An iterator to the element that follows the last element not removed.

The range between first and this iterator includes all the elements in the sequence that were not considered duplicates.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// unique algorithm example
#include <iostream> // std::cout
#include <algorithm> // std::unique, std::distance
#include <vector> // std::vector bool myfunction (int i, int j) {
return (i==j);
} int main () {
int myints[] = {10,20,20,20,30,30,20,20,10}; // 10 20 20 20 30 30 20 20 10
std::vector<int> myvector (myints,myints+9); // using default comparison:
std::vector<int>::iterator it;
it = std::unique (myvector.begin(), myvector.end()); // 10 20 30 20 10 ? ? ? ?
// ^ myvector.resize( std::distance(myvector.begin(),it) ); // 10 20 30 20 10 // using predicate comparison:
std::unique (myvector.begin(), myvector.end(), myfunction); // (no changes) // print out content:
std::cout << "myvector contains:";
for (it=myvector.begin(); it!=myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n'; return 0;
}

Output:

myvector contains: 10 20 30 20 10

Complexity

For non-empty ranges, linear in one less than the distance between first and last: Compares each pair of consecutive elements,
and possibly performs assignments on some of them.

Data races

The objects in the range [first,last) are accessed and potentially modified.

Exceptions

Throws if any of pred, the element comparisons, the element assignments or the operations on iterators throws.

Note that invalid arguments cause undefined behavior.

另一个实例:

#include <iostream>

#include <vector>

#include <algorithm>

#include <iterator>

/* 删除容器内重复元素,分三步处理

* 1、先排序容器元素

* 2、取得重复元素首部迭代器

* 3、删除重复元素

*/

using namespace std;



int main()

{

    int a[]={1,2,3,1,2,4,4,5};

    const int len = sizeof(a)/sizeof(int);

    vector<int> va(len);// 定义一个与数组等长的容器

    copy(a, a + len, va.begin());

    ostream_iterator<int, char> oi(cout," ");//定义一个输出流迭代器

    copy(va.begin(), va.end(), oi);

    cout << " 容器内元素顺序输出结果" << endl;

    // sort(两个参数)默认升序排列元素,如果不是基本类型,请重载操作符opearte<.

    // sort (三个参数) sort(va.begin(),va.end(),handle_v);

    // 自己写函数实现处理两个元素参数 bool handle_v(const int & a,cont int & b){};

    sort(va.begin(), va.end());

    vector<int>::iterator it = unique(va.begin(),va.end());

    va.erase(it, va.end());

    copy(va.begin(), va.end(), oi);

    cout << " 删除重复元素后顺序输出结果" << endl;

    return 0;

}

vector去重--unique的更多相关文章

  1. STL 去重 unique

    一.unique函数 类属性算法unique的作用是从输入序列中"删除"所有相邻的重复元素. 该算法删除相邻的重复元素,然后重新排列输入范围内的元素,并且返回一个迭代器(容器的长度 ...

  2. javascript 数组去重 unique

    晚上无事,偶然看到这么个小测试,拿来写一写,希望大家提建议: 直接上代码: Array.prototype.unique = function (isStrict) { if (this.length ...

  3. vector 去重

    1.使用unique函数: sort(v.begin(),v.end()); v.erase(unique(v.begin(), v.end()), v.end()); //unique()函数将重复 ...

  4. Java代码工具箱_用Set给List/Vector去重

    参考 方法一:需要2个容器,1个迭代去重,1个作为结果容器. 此方法其实想法比较简单也是正常思路: package com.yonyou.test; import java.util.List; im ...

  5. LeetCode OJ:Remove Duplicates from Sorted Array(排好序的vector去重)

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  6. $.unique()去重问题

    var yearArray = new Array(2009, 2009, 2010, 2010, 2009, 2010);$.unique(yearArray); 返回 2009, 2010, 20 ...

  7. vector某元素是否存在、查找指定元素 、去重

    vector.map 判断某元素是否存在.查找指定元素 [C++]判断元素是否在vector中,对vector去重,两个vector求交集.并集 PS:注意重载

  8. C++中unique函数

    目录 介绍 用法举例 数组 vector 介绍 unique是STL比较实用的一个函数.用于"去除"容器内相邻的重复的元素(只保留一个).这里说的去除并不是真正将容器内的重复元素删 ...

  9. vector基础

    //STL基础 //容器 //vector #include "iostream" #include "cstdio" #include "vecto ...

随机推荐

  1. mongodb的索引操作

    在mongodb中,当我们一个集合中的数据量非常大时,比如几百万条数据,如果不使用索引,对数据的查询就会进行全表扫描,这个时候查询的速度就会非常的慢,此时我们就需要为集合建立上索引,从而加快查询的速度 ...

  2. Hash算法:双重散列

    双重散列是线性开型寻址散列(开放寻址法)中的冲突解决技术.双重散列使用在发生冲突时将第二个散列函数应用于键的想法. 此算法使用: (hash1(key) + i * hash2(key)) % TAB ...

  3. 创建线程 出现SIGSEGV crash

    在Opwrt平台上测试ok的一个网络传输延时测试demo程序移植到Android平台后,运行出现莫名其妙的SIGSEGV crash. 仔细检查过源码,特别是指针等后未发现问题. --------- ...

  4. 解决boa网页操作出现502 Bad Gateway The CGI was not CGI/1.1 compliant的一种可能

    最近在把一套网页操作的接口从原来Android5.0上移植到Android7.0上. 客户端连接验证的时候主页显示异常 502 Bad Gateway The CGI was not CGI/1.1 ...

  5. Luogu P2081 [NOI2012]迷失游乐园 | 期望 DP 基环树

    题目链接 基环树套路题.(然而各种错误调了好久233) 当$m=n-1$时,原图是一棵树. 先以任意点为根做$dp$,求出从每一个点出发,然后只往自己子树里走时路径的期望长度. 接着再把整棵树再扫一遍 ...

  6. Latex使用CJK包添加字体

    最近写论文时有个中文期刊提供的LaTeX模板使用CJK宏包,大致是这样的: \documentclass{article} \usepackage{CJK} \begin{document} \beg ...

  7. 纯 CSS 自定义多行省略:从原理到实现

    文字溢出怎么展示,你的需求是什么?单行还是多行?截断,省略,自定义样式,自适应高度?在这里你都能找到答案.接下来我会由浅入深,从原理到实现,带你一步步揭开多行省略的面纱.我们先从最简单的单行溢出省略开 ...

  8. IDEA中Update resources和Update classes and resources、Redeploy、Restart server的区别

    选项 描述 update resources 所有更改的资源都会更新(HTML,JSP,JavaScript,CSS和图像文件) update classes and resources 更改的资源将 ...

  9. 监控框架 - prometheus - 参数指标

    基于SpringBoot2.0+ Actuator metrics的监控(基于Oracle JDK9,G1) 引言 SpringBoot2在spring-boot-actuator中引入了microm ...

  10. 菜鸡的Java笔记 笔记

    // 雇员编号 姓名 职位 基本工资 佣金等信息 package study; class Enr{ private int number; // 编号 private String fullName ...