【C++】去除vector裡重複元素的方法比較

來源:互聯網
上載者:User

標籤:

背景:構造一個無重複的白名單,之後要在裡面進行二分尋找。故要求名單有序,且無重複,並且要進行二分尋找,所以要採用有:隨機訪問迭代器類型的容器。這類容器有vector,array,deque。顯然要vector和deque合適一點,但是deque並沒有體現出其兩端和中間插入時間為固定而非線性優勢,因為本例都在尾部插入,vector和deque同為固定時間。而deque的隨機儲存操作時間長,故採用vector。

一.利用STL演算法unique

首先要將vector排序,排序後。利用erase配合unique演算法。利用一個含有一百萬整數,裡面重複數字並不太多的情況測試。

 

[cpp] view plaincopy 
  1. #include<fstream>  
  2. #include<iostream>  
  3. #include <vector>  
  4. #include<algorithm>  
  5. #include<ctime>  
  6.   
  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.   
  22.   
  23.     while(!fwhite.eof())  
  24.     {  
  25.         fwhite>>number;  
  26.         white_list.push_back(number);  
  27.     }  
  28.     cost=clock()-cost;  
  29.     cout<<"Time to load data : "<<cost<<endl;  
  30.       
  31.     sort(white_list.begin(),white_list.end());  
  32.     white_list.erase(unique(white_list.begin(),white_list.end()),white_list.end());  
  33.     cost = clock()-cost;  
  34.     cout<<"Time to remove reduplicative data from vector : "<<cost<<endl;  
  35.   
  36.     ofstream fout("sort_white.txt",ios::trunc);  
  37.   
  38.     vector<int>::iterator iter=white_list.begin();  
  39.     while (iter!= white_list.end())  
  40.     {  
  41.         fout<<*iter<<endl;  
  42.         iter++;  
  43.     }  
  44.     cost = clock()-cost;  
  45.     cout<<"Time to write data into file : "<<cost<<endl;  
  46.     exit(EXIT_SUCCESS);  
  47. };  



二.利用set配合copy

讀資料的時候就用set,然後直接拷貝到vector。但是拷貝的時候要用到insert_iterator來進行插入拷貝。(溢出問題)

 

[cpp] view plaincopy 
  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.       
  24.       
  25.     while(!fwhite.eof())  
  26.     {  
  27.         fwhite>>number;  
  28.         ori_list.insert(number);  
  29.     }  
  30.     cost=clock()-cost;  
  31.     cout<<"Time to load data : "<<cost<<endl;  
  32.   
  33.     insert_iterator<vector<int> > it(white_list,white_list.begin());  
  34.     copy(ori_list.begin(),ori_list.end(),it);  
  35.     cost = clock()-cost;  
  36.     cout<<"Time to copy data from set to vector : "<<cost<<endl;  
  37.   
  38.     ofstream fout("sort_white.txt",ios::trunc);  
  39.     vector<int>::iterator iter=white_list.begin();  
  40.     while (iter!= white_list.end())  
  41.     {  
  42.         fout<<*iter<<endl;  
  43.         iter++;  
  44.     }  
  45.     cost = clock()-cost;  
  46.     cout<<"Time to write data into file : "<<cost<<endl;  
  47.     exit(EXIT_SUCCESS);  
  48. };  

 

 

三.時間開銷從開始構造容器開始,利用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使用,在此例中速度縮短近一半。

 

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


另外:largeW檔案是從《演算法4》的網站得到的,或者可以採用rand函數先自己製造一個。每行一個int型整數,100萬行即可。

【C++】去除vector裡重複元素的方法比較

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.