背景:构造一个无重复的白名单,之后要在里面进行二分查找。故要求名单有序,且无重复,并且要进行二分查找,所以要采用有:随机访问迭代器类型的容器。这类容器有vector,array,deque。显然要vector和deque合适一点,但是deque并没有体现出其两端和中间插入时间为固定而非线性的优势,因为本例都在尾部插入,vector和deque同为固定时间。而deque的随机存储操作时间长,故采用vector。

一.利用STL算法unique

首先要将vector排序,排序后。利用erase配合unique算法。利用一个含有一百万整数,里面重复数字并不太多的情况测试。

  1. #include<fstream>
  2. #include<iostream>
  3. #include <vector>
  4. #include<algorithm>
  5. #include<ctime>
  6. using namespace std;
  7. void main()
  8. {
  9. ifstream fwhite;
  10. int number;
  11. vector<int> white_list;
  12. clock_t cost;
  13. fwhite.open("largeW.txt");
  14. if(!fwhite.is_open())
  15. {//or use .good .fail or directly use ! to judge if the file has been opened successfully
  16. cout<<"can't open file list"<<endl;
  17. exit(EXIT_FAILURE);
  18. }
  19. cost=clock();
  20. while(!fwhite.eof())
  21. {
  22. fwhite>>number;
  23. white_list.push_back(number);
  24. }
  25. cost=clock()-cost;
  26. cout<<"Time to load data : "<<cost<<endl;
  27. sort(white_list.begin(),white_list.end());
  28. white_list.erase(unique(white_list.begin(),white_list.end()),white_list.end());
  29. cost = clock()-cost;
  30. cout<<"Time to remove reduplicative data from vector : "<<cost<<endl;
  31. ofstream fout("sort_white.txt",ios::trunc);
  32. vector<int>::iterator iter=white_list.begin();
  33. while (iter!= white_list.end())
  34. {
  35. fout<<*iter<<endl;
  36. iter++;
  37. }
  38. cost = clock()-cost;
  39. cout<<"Time to write data into file : "<<cost<<endl;
  40. exit(EXIT_SUCCESS);
  41. };

二.利用set配合copy

读数据的时候就用set,然后直接拷贝到vector。但是拷贝的时候要用到insert_iterator来进行插入拷贝。(溢出问题)

  1. #include<fstream>
  2. #include<iostream>
  3. #include <vector>
  4. #include<set>
  5. #include<algorithm>
  6. #include<ctime>
  7. #include <iterator>
  8. using namespace std;
  9. void main()
  10. {
  11. ifstream fwhite;
  12. int number;
  13. vector<int> white_list;
  14. set<int> ori_list;
  15. clock_t cost;
  16. fwhite.open("largeW.txt");
  17. if(!fwhite.is_open())
  18. {//or use .good .fail or directly use ! to judge if the file has been opened successfully
  19. cout<<"can't open file list"<<endl;
  20. exit(EXIT_FAILURE);
  21. }
  22. cost=clock();
  23. while(!fwhite.eof())
  24. {
  25. fwhite>>number;
  26. ori_list.insert(number);
  27. }
  28. cost=clock()-cost;
  29. cout<<"Time to load data : "<<cost<<endl;
  30. insert_iterator<vector<int> > it(white_list,white_list.begin());
  31. copy(ori_list.begin(),ori_list.end(),it);
  32. cost = clock()-cost;
  33. cout<<"Time to copy data from set to vector : "<<cost<<endl;
  34. ofstream fout("sort_white.txt",ios::trunc);
  35. vector<int>::iterator iter=white_list.begin();
  36. while (iter!= white_list.end())
  37. {
  38. fout<<*iter<<endl;
  39. iter++;
  40. }
  41. cost = clock()-cost;
  42. cout<<"Time to write data into file : "<<cost<<endl;
  43. exit(EXIT_SUCCESS);
  44. };

三.时间开销从开始构造容器开始,利用clock计时

第一种耗时:8.477秒

第二种耗时:23.246秒

看出,还是直接用vector就好,然后配合unique好。原因:同样插入100万个整数,set用时过长,经测试用去了约18秒。为主要开销。

第一种:读取文件到vector开销5.852秒,排序并去除重复元素开销3.205秒,写文件开销15.624秒。总耗时约24秒左右。

第二种:读文件到set开销18.893秒,从set拷贝数据到vector开销4.884秒,写文件开销20秒。总耗时约44秒左右。

但是看出程序写文件很慢,本例中采用iterator迭代取值写文件,如果直接采用索引下标会不会更快?或者采用copy函数和stream_interator?

四.在一的基础上,最后写文件时采用下标而不是迭代器

发现并无明显改进。

五.采用统一复制,配合ostream_iterator使用,在此例中速度缩短近一半。

  1. #include<fstream>
  2. #include<iostream>
  3. #include <vector>
  4. #include<algorithm>
  5. #include<ctime>
  6. #include <iterator>
  7. using namespace std;
  8. void main()
  9. {
  10. ifstream fwhite;
  11. int number;
  12. vector<int> white_list;
  13. clock_t cost;
  14. fwhite.open("largeW.txt");
  15. if(!fwhite.is_open())
  16. {//or use .good .fail or directly use ! to judge if the file has been opened successfully
  17. cout<<"can't open file list"<<endl;
  18. exit(EXIT_FAILURE);
  19. }
  20. cost=clock();
  21. while(!fwhite.eof())
  22. {
  23. fwhite>>number;
  24. white_list.push_back(number);
  25. }
  26. cost=clock()-cost;
  27. cout<<"Time to load data : "<<cost<<endl;
  28. sort(white_list.begin(),white_list.end());
  29. white_list.erase(unique(white_list.begin(),white_list.end()),white_list.end());
  30. cost = clock()-cost;
  31. cout<<"Time to remove reduplicative data from vector : "<<cost<<endl;
  32. ofstream fout("sort_white.txt",ios::trunc);
  33. /*vector<int>::iterator iter=white_list.begin();
  34. while (iter!= white_list.end())
  35. {
  36. fout<<*iter<<endl;
  37. iter++;
  38. }*/
  39. //for(unsigned int index = 0;index< white_list.size();index++)
  40. //{
  41. //  fout<<white_list[index]<<endl;
  42. //}
  43. copy(white_list.begin(),white_list.end(),ostream_iterator<int,char>(fout,"\n"));
  44. cost = clock()-cost;
  45. cout<<"Time to write data into file : "<<cost<<endl;
  46. exit(EXIT_SUCCESS);
  47. };

另外:largeW文件是从《算法4》的网站得到的,或者可以采用rand函数先自己制造一个。每行一个int型整数,100万行即可。

【C++】去除vector里重复元素的方法比较的更多相关文章

  1. python去除列表中重复元素的方法

    列表中元素位置的索引用的是L.index 本文实例讲述了Python去除列表中重复元素的方法.分享给大家供大家参考.具体如下: 比较容易记忆的是用内置的set 1 2 3 l1 = ['b','c', ...

  2. php 去除数组中重复元素

    去除数组中重复元素, 找了下可以一下两个函数 php array_flip()与array_uniqure() $arr = array(…………) ;// 假设有数组包含一万个元素,里面有重复的元素 ...

  3. 引用vector里的元素被删除后,引用会怎么样?

    引用的定义不多说,直接看做变量的别名就可以了.有一天写着写着代码,突然想到,如果对vector里某个元素设置引用后,将这个元素从vector里删除会怎么样?我思考了下,认为那个元素会被删除,但是引用还 ...

  4. java去除数组重复元素的方法

    转载自:https://blog.csdn.net/Solar24/article/details/78672500 import java.util.ArrayList; import java.u ...

  5. js判断数组里是否有重复元素的方法

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/longzhoufeng/article/details/78840974 第一种方法:但是下面的这种 ...

  6. pyhon 去除列表中重复元素

    Python set() 函数 描述 set() 函数创建一个无序不重复元素集,可进行关系测试,删除重复数据,还可以计算交集.差集.并集等. 语法 set 语法: class set([iterabl ...

  7. JS判断数组中是否有重复元素的方法

    判断数组中是否有重复元素,最容易想到的方法是使用2重循环,逐个遍历,比较,但是这个是最慢,最笨的方法,百度得出了更好的方法. var ary = new Array("111",& ...

  8. 利用filter,巧妙地去除Array的重复元素

    var r, arr = ['apple', 'strawberry', 'banana', 'pear', 'apple', 'orange', 'orange', 'strawberry']; r ...

  9. ArrayList中重复元素处理方法.[Java]

    1.使用HashSet删除ArrayList中重复的元素 private static void sortByHashSet() { ArrayList<String> listWithD ...

随机推荐

  1. WhaleCTF之web密码泄露

    WhaleCTF之密码泄露 前往题目 没有思路,习惯看一下源码,拉到最后,发现有惊喜 直接把index.php 换成password.txt,访问 这是要让我密码爆破吗?直接把密码保存成passwor ...

  2. pytest框架-介绍、Mark(打标签)、命令运行用例、用例执行顺序、

    1.pytest介绍:基于unittest 之上的单元测试框架 1.1.自动发现测试模块和测试用例: unitest 需要添加用例,(泰斯特楼贷)加载器加载测试用例 pytest 只需要一条代码就可以 ...

  3. mongodb集群搭建过程记录

    mongodb集群搭建花费比较长的时间,在此记录下过程,方便以后使用 一 软件环境 系统:ubuntu 18.04,mongodb 社区版4.2 https://docs.mongodb.com/ma ...

  4. iview 小问题笔记

    总结一下用 iview 组件期间遇到的小问题.一边做项目一边做笔记. 官方文档:iview API 按需引入: 1,iview 分页组件 <Page :total="aboutPage ...

  5. Cors之带凭据的请求

    带凭据的请求 默认情况下,跨源请求不提供凭据.通过将withCredentials属性设置为true,可以制定某个请求应该发送凭据.

  6. 2019.10.20 csp-s模拟测试 lrd试题 反思总结

    赶进度赶进度,丢个代码两三句备注一下完事了. day1: 前面两道题没实际写代码怕印象不深所以描述一下大意. T1: 题目大意:给出两个数&.|.^的结果(可能只给出其中某一项或者某两项),求 ...

  7. 2019.9.27 csp-s模拟测试53 反思总结

    这个起名方式居然还有后续?! 为什么起名不是连续的?! T1想了半天,搞出来了,结果数组开小[其实是没注意范围].T2概率期望直接跳,后来翻回来写发现自己整个理解错了期望的含义[何].T3错误想到赛道 ...

  8. java-多态-object

    概要图 一 多态 1.1 多态的产生 下面的 红色部分降低了代码的可扩展性 Dog d = new Dog(); method(d); Cat c = new Cat(); method(c); } ...

  9. Leetcode590N-ary Tree Postorder TraversalN叉树的后序遍历

    给定一个 N 叉树,返回其节点值的后序遍历. class Node { public: int val; vector<Node*> children; Node() {} Node(in ...

  10. Leetcode617.Merge Two Binary Trees合并二叉树

    给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠. 你需要将他们合并为一个新的二叉树.合并的规则是如果两个节点重叠,那么将他们的值相加作为节点合并后的新值,否则不为 ...