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 ...
随机推荐
- 聊聊@Transactional 的失效场景,有哪些坑?
先别急着回答,看完再说也不迟嘛.我们都知道在 Spring 项目中,我们可以直接使用注解 @Transactional 来标识一个事务方法.然而,你可能并不知道这个事务是不是按照你想的方式执行.下面我 ...
- .Net Core微服务——网关(1):ocelot集成及介绍
网关是什么 简单来说,网关就是暴露给外部的请求入口.就和门卫一样,外面的人想要进来,必须要经过门卫.当然,网关并不一定是必须的,后端服务通过http也可以很好的向客户端提供服务.但是对于业务复杂.规模 ...
- pip 更新方法
使用python -m pip install --upgrade pip 使用python -m pip install -U --force-reinstall pip 使用pip install ...
- Robot Framework操作MySQL数据库
1.安装databaselibrary.pymysql 通过cmd命令执行:pip install robotframework-databaselibrary cmd命令执行:pip install ...
- JMeter学习笔记--JDBC测试计划-连接Mysql
1.首先要下载jar包,mysql-connector-java-5.1.7-bin.jar 放到Jmeter的lib文件下ext下 2.添加JDBC Connection Configuration ...
- JDK 工具 HSDB 查看动态生成类
前置工作 1. 复制 JDK 安装目录\jre\bin\sawindbg.dll 到 JDK 安装目录同级的 jre\bin 目录下,否则会报错找不到 sawindbg.dll 文件. 比如我的 sa ...
- ssh密码登录
https://stackoverflow.com/a/16928662/8025086 https://askubuntu.com/a/634789/861079 #!/usr/bin/expect ...
- MarkdownPad2 注册码
邮箱: Soar360@live.com 授权秘钥: GBPduHjWfJU1mZqcPM3BikjYKF6xKhlKIys3i1MU2eJHqWGImDHzWdD6xhMNLGVpbP2M5SN6b ...
- Java学习(八)
今天学了类的封装知识与编译器的使用,和c++的大体一致,只有一些细节不同,像private的使用等. 小试牛刀,写了一个封装后的类,并且测试. public class Student { priva ...
- Effective C++ 总结笔记(五)
六.继承与面向对象设计 32.确定你的public继承塑模出is-a关系 public继承意味着is-a.适用于base class身上的每一件事情也一定适用于derived class身上.每一个d ...