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. 解题:洛谷 p1858 多人背包

    题面 设$dp[i][j]$表示容量为$i$时的第$j$优解,因为是优解,肯定$dp[i][j]$是随着$j$增大不断递减的,这样的话对于一个新加进来的物品,它只可能从两个容量的转移的前$k$优解中转 ...

  2. Linux crontab 命令格式与举例

    每五分钟执行  */5 * * * * 每小时执行     0 * * * * 每天执行        0 0 * * * 每周执行       0 0 * * 0 每月执行        0 0 1 ...

  3. bzoj 3224

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 16656  Solved: 7255[Submit][St ...

  4. 利用SHELL的PROMPT_COMMAND添加日志审计功能,实时记录任何用户的操作到日志文件中

    利用 PROMPT_COMMAND 实现命令审计功能:记录什么用户,在什么时间,做了什么操作,然后将查到的信息记录到一个文件里. 具体操作: 将以下内容追加到/etc/profile: ####### ...

  5. 设置CMD默认路径

    用CMD每一次都得切换路径,很麻烦. 所以,需要设置一下CMD默认路径: 1.打开注册表编辑器(WIN+R打开运行.输入regedit) 2.定位到: “HKEY_CURRENT_USER\Softw ...

  6. 转:zookeeper中Watcher和Notifications

    转自:http://www.tuicool.com/articles/B7FRzm 传统polling远程service服务 传统远程的service往往是这样服务的,服务提供者在远程service注 ...

  7. Liunx操作指令搜素引擎

    链接:http://wangchujiang.com/linux-command/c/vi.html

  8. ASP.NET CORE API Swagger+IdentityServer4授权验证

    简介 本来不想写这篇博文,但在网上找到的文章博客都没有完整配置信息,所以这里记录下. 不了解IdentityServer4的可以看看我之前写的入门博文 Swagger 官方演示地址 源码地址 配置Id ...

  9. COGS 513 八

    513. 八 http://www.cogs.pro/cogs/problem/problem.php?pid=513 ★☆   输入文件:eight.in   输出文件:eight.out   简单 ...

  10. codevs 3235 战争

    3235 战争 http://codevs.cn/problem/3235/  时间限制: 2 s  空间限制: 128000 KB   题目描述 Description 2050年,人类与外星人之间 ...