【C++】去除vector里重复元素的方法比较
背景:构造一个无重复的白名单,之后要在里面进行二分查找。故要求名单有序,且无重复,并且要进行二分查找,所以要采用有:随机访问迭代器类型的容器。这类容器有vector,array,deque。显然要vector和deque合适一点,但是deque并没有体现出其两端和中间插入时间为固定而非线性的优势,因为本例都在尾部插入,vector和deque同为固定时间。而deque的随机存储操作时间长,故采用vector。
一.利用STL算法unique
首先要将vector排序,排序后。利用erase配合unique算法。利用一个含有一百万整数,里面重复数字并不太多的情况测试。
- #include<fstream>
- #include<iostream>
- #include <vector>
- #include<algorithm>
- #include<ctime>
- using namespace std;
- void main()
- {
- ifstream fwhite;
- int number;
- vector<int> white_list;
- clock_t cost;
- fwhite.open("largeW.txt");
- if(!fwhite.is_open())
- {//or use .good .fail or directly use ! to judge if the file has been opened successfully
- cout<<"can't open file list"<<endl;
- exit(EXIT_FAILURE);
- }
- cost=clock();
- while(!fwhite.eof())
- {
- fwhite>>number;
- white_list.push_back(number);
- }
- cost=clock()-cost;
- cout<<"Time to load data : "<<cost<<endl;
- sort(white_list.begin(),white_list.end());
- white_list.erase(unique(white_list.begin(),white_list.end()),white_list.end());
- cost = clock()-cost;
- cout<<"Time to remove reduplicative data from vector : "<<cost<<endl;
- ofstream fout("sort_white.txt",ios::trunc);
- vector<int>::iterator iter=white_list.begin();
- while (iter!= white_list.end())
- {
- fout<<*iter<<endl;
- iter++;
- }
- cost = clock()-cost;
- cout<<"Time to write data into file : "<<cost<<endl;
- exit(EXIT_SUCCESS);
- };
二.利用set配合copy
读数据的时候就用set,然后直接拷贝到vector。但是拷贝的时候要用到insert_iterator来进行插入拷贝。(溢出问题)
- #include<fstream>
- #include<iostream>
- #include <vector>
- #include<set>
- #include<algorithm>
- #include<ctime>
- #include <iterator>
- using namespace std;
- void main()
- {
- ifstream fwhite;
- int number;
- vector<int> white_list;
- set<int> ori_list;
- clock_t cost;
- fwhite.open("largeW.txt");
- if(!fwhite.is_open())
- {//or use .good .fail or directly use ! to judge if the file has been opened successfully
- cout<<"can't open file list"<<endl;
- exit(EXIT_FAILURE);
- }
- cost=clock();
- while(!fwhite.eof())
- {
- fwhite>>number;
- ori_list.insert(number);
- }
- cost=clock()-cost;
- cout<<"Time to load data : "<<cost<<endl;
- insert_iterator<vector<int> > it(white_list,white_list.begin());
- copy(ori_list.begin(),ori_list.end(),it);
- cost = clock()-cost;
- cout<<"Time to copy data from set to vector : "<<cost<<endl;
- ofstream fout("sort_white.txt",ios::trunc);
- vector<int>::iterator iter=white_list.begin();
- while (iter!= white_list.end())
- {
- fout<<*iter<<endl;
- iter++;
- }
- cost = clock()-cost;
- cout<<"Time to write data into file : "<<cost<<endl;
- exit(EXIT_SUCCESS);
- };
三.时间开销从开始构造容器开始,利用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使用,在此例中速度缩短近一半。
- #include<fstream>
- #include<iostream>
- #include <vector>
- #include<algorithm>
- #include<ctime>
- #include <iterator>
- using namespace std;
- void main()
- {
- ifstream fwhite;
- int number;
- vector<int> white_list;
- clock_t cost;
- fwhite.open("largeW.txt");
- if(!fwhite.is_open())
- {//or use .good .fail or directly use ! to judge if the file has been opened successfully
- cout<<"can't open file list"<<endl;
- exit(EXIT_FAILURE);
- }
- cost=clock();
- while(!fwhite.eof())
- {
- fwhite>>number;
- white_list.push_back(number);
- }
- cost=clock()-cost;
- cout<<"Time to load data : "<<cost<<endl;
- sort(white_list.begin(),white_list.end());
- white_list.erase(unique(white_list.begin(),white_list.end()),white_list.end());
- cost = clock()-cost;
- cout<<"Time to remove reduplicative data from vector : "<<cost<<endl;
- ofstream fout("sort_white.txt",ios::trunc);
- /*vector<int>::iterator iter=white_list.begin();
- while (iter!= white_list.end())
- {
- fout<<*iter<<endl;
- iter++;
- }*/
- //for(unsigned int index = 0;index< white_list.size();index++)
- //{
- // fout<<white_list[index]<<endl;
- //}
- copy(white_list.begin(),white_list.end(),ostream_iterator<int,char>(fout,"\n"));
- cost = clock()-cost;
- cout<<"Time to write data into file : "<<cost<<endl;
- exit(EXIT_SUCCESS);
- };
另外:largeW文件是从《算法4》的网站得到的,或者可以采用rand函数先自己制造一个。每行一个int型整数,100万行即可。
【C++】去除vector里重复元素的方法比较的更多相关文章
- python去除列表中重复元素的方法
列表中元素位置的索引用的是L.index 本文实例讲述了Python去除列表中重复元素的方法.分享给大家供大家参考.具体如下: 比较容易记忆的是用内置的set 1 2 3 l1 = ['b','c', ...
- php 去除数组中重复元素
去除数组中重复元素, 找了下可以一下两个函数 php array_flip()与array_uniqure() $arr = array(…………) ;// 假设有数组包含一万个元素,里面有重复的元素 ...
- 引用vector里的元素被删除后,引用会怎么样?
引用的定义不多说,直接看做变量的别名就可以了.有一天写着写着代码,突然想到,如果对vector里某个元素设置引用后,将这个元素从vector里删除会怎么样?我思考了下,认为那个元素会被删除,但是引用还 ...
- java去除数组重复元素的方法
转载自:https://blog.csdn.net/Solar24/article/details/78672500 import java.util.ArrayList; import java.u ...
- js判断数组里是否有重复元素的方法
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/longzhoufeng/article/details/78840974 第一种方法:但是下面的这种 ...
- pyhon 去除列表中重复元素
Python set() 函数 描述 set() 函数创建一个无序不重复元素集,可进行关系测试,删除重复数据,还可以计算交集.差集.并集等. 语法 set 语法: class set([iterabl ...
- JS判断数组中是否有重复元素的方法
判断数组中是否有重复元素,最容易想到的方法是使用2重循环,逐个遍历,比较,但是这个是最慢,最笨的方法,百度得出了更好的方法. var ary = new Array("111",& ...
- 利用filter,巧妙地去除Array的重复元素
var r, arr = ['apple', 'strawberry', 'banana', 'pear', 'apple', 'orange', 'orange', 'strawberry']; r ...
- ArrayList中重复元素处理方法.[Java]
1.使用HashSet删除ArrayList中重复的元素 private static void sortByHashSet() { ArrayList<String> listWithD ...
随机推荐
- PHP中的符号 ->、=> 和 :: 的含义(用法)
php新手经常碰到的问题,->.=> 和 :: 这三个家伙是什么分别都是做什么的啊!看着就很晕. 没关系,下面我们做一下详细的解释,如果你有C++,Perl基础,你会发现这些家伙和他们里面 ...
- MATLAB---fopen、fprintf函数
1 概述 fopen()是个将数据按指定格式读入到matlab中的函数. fprintf()是个将数据按指定格式写入到文本文件中的函数. 2 用法 2.1 fopen函数 matlab中fopen函数 ...
- nodejs+express 初学(一)
以下都是windows环境 1.下载nodejs http://www.nodejs.org/download/ 然后安装 2.确认已经安装完成 . node -v 3.安装express 注意: 是 ...
- LINUX用户身份切换
Su 命令作用 su的作用是变更为其它使用者的身份,超级用户除外,需要键入该使用者的密码. 使用方式 su [-fmp] [-c command] [-s shell] [--help] [--ver ...
- PHP苹果推送实现(APNS)
以下资料网上收集整理得来 1.在ios dev center制作相关证书和文件用客户端实现(不再赘述,网上很多,) 网上教程: http://blog.csdn.net/lizhenning87/ar ...
- MySQL事务、锁机制、查询缓存
MySQL事务 何为事务? 事务(Transaction)是访问并可能更新数据库中各种数据项的一个程序执行单元(unit). 一个事务可以是一条SQL语句,一组SQL语句或整个程序. 事务的特性: 事 ...
- ASP.NET Core修改IOC为Autofac
如下是我为了了解如何更换ASP.NET Core中的IOC而查找的文章,如果大家英文OK的,可以直接前往阅读,同时也已经有简单的github例子供大家参考. 参考文章: ASP.NET Core文档: ...
- Redis源码解析:24sentinel(五)TLIT模式、执行脚本
十一:TILT模式 根据之前的介绍可知,哨兵的运行,非常依赖于系统时间,但是当系统时间被调整,或者哨兵中的流程因为某种原因(比如负载较高.IO发生阻塞.进程被信号停止等)而被阻塞时,哨兵的行为就会变得 ...
- ES6学习笔记之字符串的扩展
字符串的for of ES6 为字符串添加了遍历器接口,使得字符串可以被for...of循环遍历. const str='abcd'; for(let s of str){ console.log(s ...
- Composer项目安装依赖包
说明:一般用Composer的项目中,目录下都有一个composer.json文件,安装这些依赖,只需要输入命令即可 步骤: cmd进入项目目录,运行命令即可 composer install 文章参 ...