1. #include<iostream>
  2. #include<algorithm>
  3. usingnamespace std;
  4. /**< unique函数的算法思想 */
  5. vector<int>::iterator uniqueElements(vector<int>::iterator begPos,vector<int>::iterator endPos)
  6. {
  7. auto currPos = begPos +1;
  8. while(currPos != endPos)
  9. {
  10. if(*begPos !=*currPos)
  11. *(++begPos)=*(currPos++);
  12. else
  13. ++currPos;
  14. }
  15. return begPos+1;
  16. }
  17. int main()
  18. {
  19. vector<int> intVector;
  20. for(int i =0; i <20;++i)
  21. {
  22. intVector.push_back(rand()%10);
  23. }
  24. for(auto val : intVector)
  25. {
  26. cout << val <<" ";
  27. }
  28. cout << endl;
  29. sort(intVector.begin(), intVector.end());
  30. for(auto val : intVector)
  31. {
  32. cout << val <<" ";
  33. }
  34. cout << endl;
  35. auto end_unique = unique(intVector.begin(), intVector.end());
  36. /**< 可以查看算法,unique并不是简单的将重复的元素放置到最后面去 */
  37. for(vector<int>::iterator it = intVector.begin(); it != intVector.end();++it)
  38. {
  39. if(it == end_unique)
  40. cout <<"##";
  41. cout <<*it <<" ";
  42. }
  43. cout << endl;
  44. intVector.erase(end_unique,intVector.end());
  45. for(auto val:intVector )
  46. cout << val <<" ";
  47. cout << endl;
  48. return0;
  49. }

 

algorithm之unique的更多相关文章

  1. STL中unique的使用

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

  2. 去重函数unique,sort,erase的应用

    std::unique 一.总述 unique函数属于STL中比较常用函数,它的功能是元素去重.即"删除"序列中所有相邻的重复元素(只保留一个).此处的删除,并不 是真的删除,而是 ...

  3. vector去重--unique

    具体实现见中间源码 function template <algorithm> std::unique equality (1) template <class ForwardIte ...

  4. C++算法接口使用参考

    C++算法接口参考 算法参考:[algorithm] 编译:g++ -std=c++11 xxx_algorithm.cpp 运行:./a.out 1.保持原序列运算 all_of template ...

  5. STL中算法分类

    操作对象 直接改变容器的内容 将原容器的内容复制一份,修改其副本,然后传回该副本 功能: 非可变序列算法 指不直接修改其所操作的容器内容的算法 计数算法        count.count_if 搜 ...

  6. 01--STL算法(算法基础)

    一:算法概述 算法部分主要由头文件<algorithm>,<numeric>和<functional>组成. <algorithm>是所有STL头文件中 ...

  7. C++复习:STL之算法

    算法 1算法基础 1.1算法概述 算法部分主要由头文件<algorithm>,<numeric>和<functional>组成. <algorithm> ...

  8. C++STL 算法

    算法部分主要由头文件<algorithm>,<numeric>和<functional>组成. <algorithm>是所有STL头文件中最大的一个,其 ...

  9. STL 算法介绍

    STL 算法介绍 算法概述 算法部分主要由头文件<algorithm>,<numeric>和<functional>组成.        <algorithm ...

随机推荐

  1. 在Struts2的Action中获得request response session几种方法

    转载自~ 在Struts2中,从Action中取得request,session的对象进行应用是开发中的必需步骤,那么如何从Action中取得这些对象呢?Struts2为我们提供了四种方式.分别为se ...

  2. 学些goosman-lei的博客感触

    在进行程序员职业规划标题搜索的时候,csdn推荐了<专访雷果国:从1.5K到18K 一个程序员的5年成长之路>感触颇深链接:https://www.csdn.net/article/201 ...

  3. 《Spring实战》 1-2

    第1章 Spring之旅 Spring容器 Spring中bean的生命周期 Spring框架中的模块 Spring Portfolio 第2章 装配Bean 自动化装配bean 通过Java代码装配 ...

  4. Java基础-正则表达式(Regular Expression)语法规则简介

    Java基础-正则表达式(Regular Expression)语法规则简介 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.正则表达式的概念 正则表达式(Regular Exp ...

  5. Linux QT数据库之登录注册

    视频链接:https://www.bilibili.com/video/av11673511/ main.cpp #include <QSqlDatabase> #include < ...

  6. windows下静态编译pthread

    1. Building the library as a statically linkable library-------------------------------------------- ...

  7. new FileInputStream(file)中file不能为文件夹

    今天闲来无事,写了如下程序: package com.xhj.test; import java.io.File; import java.io.FileFilter; import java.io. ...

  8. 【官方文档】Nginx负载均衡学习笔记(三) TCP和UDP负载平衡官方参考文档

    本章介绍如何使用NGINX Plus和NGINX开放源代理和负载平衡TCP和UDP流量. 目录 介绍 先决条件 配置反向代理 配置TCP或UDP负载平衡被动健康监控 选择负载平衡方法 配置会话持久性 ...

  9. JS对象中的原型

    对象的原型:每个对象都连接一个原型对象,并且它可以从中继承属性.所有通过对象字面量创建的对象都连接到object.prototype.当你创建一个新对象时,你可以选择某个对象作为它的原型.原型连接在更 ...

  10. 【CodeForces】700 D. Huffman Coding on Segment 哈夫曼树+莫队+分块

    [题目]D. Huffman Coding on Segment [题意]给定n个数字,m次询问区间[l,r]的数字的哈夫曼编码总长.1<=n,m,ai<=10^5. [算法]哈夫曼树+莫 ...