vector去重--unique
std::unique
equality (1) |
template <class ForwardIterator> |
---|---|
predicate (2) |
template <class ForwardIterator, class BinaryPredicate> |
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:
|
|
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 (iftrue
, 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
|
|
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的更多相关文章
- STL 去重 unique
一.unique函数 类属性算法unique的作用是从输入序列中"删除"所有相邻的重复元素. 该算法删除相邻的重复元素,然后重新排列输入范围内的元素,并且返回一个迭代器(容器的长度 ...
- javascript 数组去重 unique
晚上无事,偶然看到这么个小测试,拿来写一写,希望大家提建议: 直接上代码: Array.prototype.unique = function (isStrict) { if (this.length ...
- vector 去重
1.使用unique函数: sort(v.begin(),v.end()); v.erase(unique(v.begin(), v.end()), v.end()); //unique()函数将重复 ...
- Java代码工具箱_用Set给List/Vector去重
参考 方法一:需要2个容器,1个迭代去重,1个作为结果容器. 此方法其实想法比较简单也是正常思路: package com.yonyou.test; import java.util.List; im ...
- 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 ...
- $.unique()去重问题
var yearArray = new Array(2009, 2009, 2010, 2010, 2009, 2010);$.unique(yearArray); 返回 2009, 2010, 20 ...
- vector某元素是否存在、查找指定元素 、去重
vector.map 判断某元素是否存在.查找指定元素 [C++]判断元素是否在vector中,对vector去重,两个vector求交集.并集 PS:注意重载
- C++中unique函数
目录 介绍 用法举例 数组 vector 介绍 unique是STL比较实用的一个函数.用于"去除"容器内相邻的重复的元素(只保留一个).这里说的去除并不是真正将容器内的重复元素删 ...
- vector基础
//STL基础 //容器 //vector #include "iostream" #include "cstdio" #include "vecto ...
随机推荐
- zuul过滤器filter 的编写
通过上一节(zuul的各种配置)的学习,我们学会了zuul路由的各种配置,这一节我们来实现一下zuul的过滤器功能.那么为什么需要用到zuul的过滤器呢?我们知道zuul是我们实现外部系统统一访问的入 ...
- path-sum leetcode C++
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- 学会python永不加班系列之操作excel
python作为一种解释性语言,简单高效的模式逐渐火爆.同时存在多种扩展性. 永不加班系列 python正确操作excel 实验环境: 系统:win10 语言:python3.8 承载软件:pycha ...
- 手把手教你学Dapr - 4. 服务调用
上一篇:手把手教你学Dapr - 3. 使用Dapr运行第一个.Net程序 介绍 通过使用服务调用,您的应用程序可以使用标准的gRPC或HTTP协议与其他应用程序可靠.安全地通信. 为什么不直接用Ht ...
- 王爽汇编第十章,call和ret指令
目录 王爽汇编第十章,call和ret指令 call和ret指令概述: ret和retf ret指令 retf指令 call 和 ret 的配合使用 call指令详解 call原理 call指令所有写 ...
- TestNG 参数化应用
一.第一种参数化方式(testng.xml配置参数) 1.新建ParameterDemo04类 2.拷贝类的路径 3.testng.xml配置类地址及参数 <?xml version=" ...
- Java开发介绍之JDK JRE JVM 和 环境变量配置
一.JDK>JRE>JVM JDK(Java Development Kit):Java开发工具包 JDK中包含JRE,在JDK的安装目录下有一个名为jre的目录,里面有两个文件夹bin和 ...
- Linux USB (目录)
1.USB 总线简介 2.USB 协议分析 3.USB Host 详解 4.USB Device 详解 5.usbip (USB Over IP) 使用实例
- C#中OnLoad事件和Form1_Load事件的区别
在学习<GDI+高级编程>第二章的过程中遇到一个疑问,就是为何有的代码用的是覆写一个OnLoad事件,而平日里我用的一般是Form1_Load事件,这两个函数很相近,但是具体有什么关系呢? ...
- Django笔记&教程 5-3 综合使用示例
Django 自学笔记兼学习教程第5章第3节--综合使用示例 点击查看教程总目录 1 - 生成学号场景 场景描述: 教务管理系统中,学生注册账号,学生选择年级后,生成唯一学号. 细节分析: 学生学号由 ...