作用:

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

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

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

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

所以在使用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. C#基础(201)--常量枚举

    本文知识点: 1.掌握常量的定义和使用方法 2.理解枚举的作用和特点 3.掌握枚举的使用方法 1.1.常量的定义语法 const  数据类型   常量名称  =  值: 1.2.常见错误 1.3常量的 ...

  2. meta、link、script的常用写法

    meta 设置编码格式 <meta charset='utf-8'> 设置关键字 <meta name="keywords" content="音乐 播 ...

  3. RabbitMQ 环境搭建

    安装基础环境 yum install net-tools yum install yum yum install gcc glibc-devel make ncurses-devel openssl- ...

  4. Tomcat_记一次tomcatwar包应用简单部署过程

    记一次tomcat war包应用简单部署过程 by:授客 QQ:1033553122 1.  实践环境 Linux apache-tomcat-7.0.73 2.  实践步骤 # 解压tomcat压缩 ...

  5. chrome离线包出现的小问题

    网友使用离线包时出现的一些小问题,在此做个记录: 1. @200258 这个版本就是个坑.chrome低版本显示空白,高版本界面乱掉,有反馈出54可以,有说56可以 亲测:即使不用离线包直接FQ,也会 ...

  6. wap2app(一)-- 网站快速打包成app

    工具:HBuilder,下载地址:http://www.dcloud.io/ 下载并安装HBuilder后,打开编辑器,选择:文件 -> 新建 -> 项目,出现如下图: 选择wap2app ...

  7. git 入门教程之github 教程

    github 教程 github 是一个基于 git 的代码托管平台,是平时工作学习的好帮手,学会如何用好 github 网站能够帮助我们更好分享代码或者与其他开发人员合作. 注册 github 账号 ...

  8. 智能ERP主副机设置

    智能ERP主副机设置 1. 将主机的电脑设置成固定IP,IP地址请自行设置,设置好后需要记住,配置副机的时候会用到 2. 在主机上安装智能ERP,安装完后,会弹出数据库配置,主机直接点校验 3. 校验 ...

  9. [20190213]测试服务端打开那些端口.txt

    [20190213]测试服务端打开那些端口.txt --//前几天测试使用发送信息到/dev/tcp/ip_address/port,测试端口是否打开.写简单写一个脚本验证看看. $ seq 1 65 ...

  10. MapFileParser.sh: Permission denied

    Unity项目,需要用Xcode运行,结果报了错误. 解决方案: 1.打开终端, 2.输入以下命令: chmod +x   /Users/......./MapFileParser.sh (MapFi ...