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 ...
随机推荐
- SpringBoot整合多个RabbitMQ
一.背景 最近项目中需要用到了RabbitMQ来监听消息队列,监听的消息队列的 虚拟主机(virtualHost)和队列名(queueName)是不一致的,但是接收到的消息格式相同的.而且可能还存 ...
- Spring Security Jwt Token 自动刷新
token的自动刷新 一.功能需求 二.功能分析 1.token 的生成 2.token 的自动延长 3.系统资源的保护 4.用户如何传递 token 三.实现思路 1.生成 token 和 refr ...
- vscode插件集合整理
针对PEPE8进行代码规范提示,安装flake8之后写代码的时候编辑器就会提示哪里出错,代码格式不规范也会提示,具体安装方式如下: 1.pip install flake8 2.安装flake8成功后 ...
- sql 多表联合查询更新
sqlserver: update A a set a.i = b.k from B b where a.key = b.key oracle : update A a set a.i = (sele ...
- 进程间通信消息队列msgsnd执行:Invlid argument——万恶的经验主义
最近在搞进程间通信,首先在我的ubuntu 14.04上写了接口和测试demo,编译和执行都OK,,代码如下: 接口文件ipcmsg.h /* ipcmsg.h */ #ifndef H_MSGIPC ...
- python中将xmind转成excel
需求:最近公司项目使用tapd进行管理,现在遇到的一个难题就是,使用固定的模板编写测试用例,使用excel导入tapd进行测试用例管理,觉得太过麻烦,本人一直喜欢使用导图来写测试用例,故产生了这个工具 ...
- 在Delphi中高效执行JS代码
因为一些原因,需要进行encodeURIComponent和decodeURIComponent编码,在Delphi中找了一个,首先是发现不能正确编码+号,后面强制处理替换了,勉强可用. 后面发现多次 ...
- Pod 健康检查和服务可用性检查
Kubernetes 对 Pod 的健康状态可以通过两类探针来检查:LivenessProbe 和 ReadinessProbe,kubelet 定期执行这两类探针来针对容器的健康状况. Livene ...
- 访问kubernetes CRD的几种方式
访问kubernetes CRD的几种方式 最近在使用代码操作VictoriaMetrics Operator的CRD资源的过程中,探究了集中访问CRD资源的方式.下面以VictoriaMetrics ...
- js添加事件监听的方式与this
js添加事件监听与this js添加事件监听的方式与this 在标签中调用自定义函数 DOM0级事件处理程序 DOM2级事件处理程序 this 代表谁? js添加事件监听的方式与this <di ...