作用:

  元素去重,即“删除”序列中所有相邻的重复元素(只保留一个)

(此处的删除,并不是真的删除,而是指重复元素的位置被不重复的元素占领了)

(其实就是把多余的元素放到了最后面)

由于它“删除”的是相邻的重复元素

所以在使用unique函数之前,一般都会将目标序列进行排序

(用unique之前,最好用个sort等排序来先把目标排个序)

  • 现在总结一下unique,unique的作用是“去掉”容器中相邻元素的重复元素(不一定要求数组有序),它会把重复的元素添加到容器末尾(所以数组大小并没有改变),而返回值是去重之后的尾地址,下面举个例子。
  • 由于返回的是容器末尾,所以如果想得到去重后的size,需要减去初始地址,lower_bound是得到地址,稍微不同。
sz = unique(b + ,b + n + )-(b + );
  sz = unique(a,a + n) - a;

应用:

sort(words.begin(), words.end());
vector<string>::iterator end_unique = unique(words.begin(), words.end());
words.erase(end_unique, words.end());

来一份完整代码

#include <iostream>
#include <cassert>
#include <algorithm>
#include <vector>
#include <string>
#include <iterator>
using namespace std;
int main()
{
const int N=;
int array1[N]={,,,,,,,,,,};
vector<int> vector1;
for (int i=;i<N;++i)
vector1.push_back(array1[i]); vector<int>::iterator new_end;
new_end=unique(vector1.begin(),vector1.end()); //"删除"相邻的重复元素
assert(vector1.size()==N); vector1.erase(new_end,vector1.end()); //删除(真正的删除)重复的元素
copy(vector1.begin(),vector1.end(),ostream_iterator<int>(cout," "));
cout<<endl; return ;
}

unique的更多相关文章

  1. [LeetCode] Unique Substrings in Wraparound String 封装字符串中的独特子字符串

    Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...

  2. [LeetCode] Minimum Unique Word Abbreviation 最短的独一无二的单词缩写

    A string such as "word" contains the following abbreviations: ["word", "1or ...

  3. [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

  4. [LeetCode] Unique Word Abbreviation 独特的单词缩写

    An abbreviation of a word follows the form <first letter><number><last letter>. Be ...

  5. [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  6. [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  7. [LeetCode] Unique Paths II 不同的路径之二

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  8. [LeetCode] Unique Paths 不同的路径

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  9. 2 Unique Binary Search Trees II_Leetcode

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  10. Constraint5:unique 约束和null

    unique约束使用unique index来限制列值的唯一性: 创建unique约束之后,column中允许插入null值,unique 约束将两个null值看作是相同的(即null=null为tr ...

随机推荐

  1. 洛谷P4103 [HEOI2014]大工程(虚树 树形dp)

    题意 链接 Sol 虚树. 首先建出虚树,然后直接树形dp就行了. 最大最小值直接维护子树内到该节点的最大值,然后合并两棵子树的时候更新一下答案. 任意两点的路径和可以考虑每条边两边的贡献,\(d[x ...

  2. iOS----------关于UDID和UUID的一些理解

    一.UDID(Unique Device Identifier)  UDID是Unique Device Identifier的缩写,中文意思是设备唯一标识. 在很多需要限制一台设备一个账号的应用中经 ...

  3. Python 捕捉traceback异常栈信息

    捕捉traceback异常栈信息   by:授客 QQ:1033553122 相关函数简介 sys.exc_info() 返回包含3个元素(type, value, traceback)的元组,提供关 ...

  4. 初见jQuery EasyUI

    本文通过一个简单的小例子,简述jQuery EasyUI的使用方法,仅供学习分享使用,如有不足之处,还请指正. 什么是jQuery EasyUI ? 引用官网的一句话:jQuery EasyUI fr ...

  5. Android-启动页“android:windowBackground”变型?

    <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android=" ...

  6. Android广播机制的基本使用

    一提到广播我们第一感觉就会联想到小时候村里面的广播,安卓的广播机制也是类似于大喇叭.有发送广播的地方,也有接收广播的地方.但是具体怎么操作呢,我们来一步一步的看下去~ 安卓的广播种类 系统发送的广播: ...

  7. 【效率工具】SSH一键登录脚本(可一键从跳板机登录线上服务器)

    说明 前阵子上线,一次性上了十个服务,一直上到凌晨才完事,期间每个服务都要先输入跳板机的登录信息来登录跳板机,然后再输入线上服务器的信息来登录线上服务器,实在是太过于麻烦,而且有些服务还有好几台服务器 ...

  8. php 接口与前端数据交互实现

    最近在做前后端数据交互的尝试,也跳了很多坑,使用的是php+bootstrap-table+js,把一些收获记录在这里,方便查询. 这个小项目,仅有3个文件,分别为: crud.html data.p ...

  9. python数据类型分类以及运算类型

    一.python数据类型 目录: 1.数字(整数.小数) 2.字符串(单引号.双引号.三引号) 3.元组 #元素确定之后不能修改 4.列表 #元素可以修改 5.集合  #不讲顺序,得到的结果没有重复元 ...

  10. 了解 IP 地址,默认网关,子网掩码,DNS 的概念和作用。

    DNS(Domain Name System):域名解析服务器,在 Internet 上域名与 IP 地址一一对应,域名便于人记忆,但是机器只认识 IP 地址,他们之间的转换工作称为 ‘域名解析’,域 ...