刪除數組中重複元素 (使用stl::set)

來源:互聯網
上載者:User

標籤:os   io   使用   ar   for   sp   log   html   c   

/*
 *程式作用刪除數中重複的元素,先使用set 遍曆一次數組,然後在使用兩個指標,以及set查重,
 *去重複之後使用0填補多餘空間
 *複雜度 O(NlogN)
 *空間複雜度 O(N)

 */

#include<iostream>#include<set>using namespace std;void delete_over_arry(int *a,int len);void print(int *a ,int len);int main(){   int p[]={1,1,2,5,3,7,3,4,8,2,1,3,9,1};   print(p,sizeof(p)/sizeof(int));   delete_over_arry( p,sizeof(p)/sizeof(int));   print(p,sizeof(p)/sizeof(int));   return 0;}void print(int *a ,int len){  for(int i=0;i<len;i++)      cout<<a[i];      cout<<endl;}void  delete_over_arry(int *a,int len){       set<int>  temp_set;       int *set_p=a+1;       int *new_p=a+1;       int count=0;       temp_set.insert(a[0]);       for(int i=1;i<len;i++)          {               if(temp_set.count(*set_p))              {                 set_p++;                 count++;              }              else              {                 temp_set.insert(*set_p);                 *new_p++=*set_p++;              }          }        for(int i=0;i<count;i++)              *new_p++=0;        }
以下是程式運行結果:

[[email protected] code_test]$ g++ -o delete_overarray delete_overarray.cpp[[email protected] code_test]$ ./delete_overarray 1125373482139112537489000000





刪除數組中重複元素 (使用stl::set)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.