std::unique适用于将排过序的数据结构重复的部分全部放在结尾

但用的时候发现会将原先容器中的内容改掉,看了源码发现这个函数会将不重复的数据结构直接覆盖到前一个重复的位置上,下面看源码

该函数std::unique位于头文件<algorithm>声明1如下:

template< class ForwardIt >
ForwardIt unique( ForwardIt first, ForwardIt last );

声明2如下:

template< class ForwardIt, class BinaryPredicate >
ForwardIt unique( ForwardIt first, ForwardIt last, BinaryPredicate p );

该函数的作用为: 删除[first, last)之间所有连续重复的元素, 只保留一个。 注意, 是连续重复。 要删除所有重复的元素, 只需要排序之后, 然后调用这个函数即可实现。 第一个版本通过==判断是否重复, 第二个版本通过二元谓词p判断是否重复。

二元谓词p, 就是binary predicate which returns ​true if the elements should be treated as equal.

版本1的可能的实现方式:

template<class ForwardIt>
ForwardIt unique(ForwardIt first, ForwardIt last)
{
if (first == last)
return last; ForwardIt result = first;
while (++first != last) {
if (!(*result == *first)) {
*(++result) = *first;
}
}
return ++result;
}

所一, 最终返回的一个迭代器指向任务结束的位置past the end.
版本二的实现方式:

template<class ForwardIt, class BinaryPredicate>
ForwardIt unique(ForwardIt first, ForwardIt last,
BinaryPredicate p)
{
if (first == last)
return last; ForwardIt result = first;
while (++first != last) {
if (!p(*result, *first)) {
*(++result) = *first;
}
}
return ++result;

std::unique实现的更多相关文章

  1. LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>

    LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...

  2. #424 Div2 Problem C Jury Marks (二分 && 暴力 && std::unique && 思维)

    题目链接 :http://codeforces.com/contest/831/problem/C 题意 :选手有一个初始积分,接下来有k个裁判为他加分或减分(时间顺序给出),然后告诉你n(1< ...

  3. std::unique

    类属性算法unique的作用是从输入序列中“删除”所有相邻的重复元素. 该算法删除相邻的重复元素,然后重新排列输入范围内的元素,并且返回一个迭代器(容器的长度没变,只是元素顺序改变了),表示无重复的值 ...

  4. C++算法库学习__std::sort__对 vector进行排序_排序后就可以进行使用std::lower_bound进行二分查找(查找第一个大于等于指定值的迭代器的位置)__std::unique

    std::sort      对vector成员进行排序; std::sort(v.begin(),v.end(),compare);   std::lower_bound 在排序的vector中进行 ...

  5. unique &unique_copy

      unique (ForwardIterator first, ForwardIterator last); unique (ForwardIterator first, ForwardIterat ...

  6. algorithm之unique

    #include<iostream> #include<algorithm> usingnamespace std; /**< unique函数的算法思想 */ vect ...

  7. STL中unique的使用

    作用 unique函数可以删除有序数组中的重复元素,即去重(并不是真正的删除,后面会讲) 定义在头文件<algorithm>中 函数原型 1.只有两个参数,且参数类型都是迭代器: iter ...

  8. STL_算法_删除(unique、unique_copy)

    C++ Primer 学习中. . . 简单记录下我的学习过程 (代码为主) 全部容器适用 unique(b,e) unique(b,e,p) unique_copy(b1,e1,b2) unique ...

  9. C++ STD Gems02

    remove.remove_if.replace.replace_if.remove_copy_if.unique #include <iostream> #include <str ...

随机推荐

  1. 游戏反编译工具dnSpy

    dnSpy使用的工具下载地址为: https://github.com/cnxy/dnSpy/archive/v4.0.0.zip 或 dnSpy官方下载地址: https://github.com/ ...

  2. WPF中的快捷键(累积中)

    1. 显示可选属性, ctrl + J 如上图,当不知道Background的可选择时,可以输入 ctrl + J,系统就会显示所有可选属性

  3. Centos7 安装mysql教程

    参考原文:http://www.centoscn.com/mysql/2016/0315/6844.html 环境 CentOS 7.1 (64-bit system) MySQL 5.6.24 Ce ...

  4. 记一次数据、逻辑、视图分离的原生JS项目实践

    一切的开始源于这篇文章:一句话理解Vue核心内容. 在文章中,作者给出了这样一个思考: 假设现在有一个这样的需求,有一张图片,在被点击时,可以记录下被点击的次数. 这看起来很简单吧, 按照上面提到到开 ...

  5. java.lang.RuntimeException Unable to instantiate application Caused by: java

    参考:http://blog.csdn.net/xufazhong/article/details/71155528 dependencies { classpath 'com.android.too ...

  6. JavaScript对象中的this属性

    this属性表示当前对象,如果在全局作用范围内使用this,则指代当前页面对象window: 如果在函数中使用this,则this指代什么是根据运行时此函数在什么对象上被调用. 我们还可以使用appl ...

  7. easyui内嵌iframe问题解决

    项目中使用easyui的tab页,每个tab页均内嵌iframe,现在要在tab页中控制新增一个同级别的tab页,记录如下: 首先是main.html主页面: <div class=" ...

  8. Struts2 学习(三)

    一.访问Servlet API 的三种方式 1.什么是 Action 访问 Servlet 的 API 1.访问 Servlet 的API: 1.获取 request 对象. 2.接受请求参数. 3. ...

  9. 用图片替代cursor光标样式

    鼠标光标样式有限,可参考http://css-cursor.techstream.org/,自定义光标样式可用设置cursor:url('xxx.cur'),auto;.还有一种办法,就是用图片替代鼠 ...

  10. Docker问题集合

    1. 安装后启动出现 解决办法: 删除以下文件夹重新启动docker服务即可: 可能原因:(1) 之前docker进程出现错误并保存在keys.json文件中 (2) 删除之前配置了阿里云镜像,生成了 ...