C++中vector和set删除一亿个数字中的奇数

原文地址:http://blog.csdn.net/corcplusplusorjava/article/details/45115057  向原作者致敬

一、vector

先贴代码再解释:
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <iostream>  
  2. #include <algorithm>  
  3. #include <vector>  
  4. #include <set>  
  5.   
  6. using namespace std;  
  7.   
  8. const unsigned int NUM = 100000000;  
  9.   
  10. void removeOdd1(vector<int>& a)  
  11. {  
  12.     for(vector<int>::iterator it = a.begin();it!=a.end();)  
  13.     {  
  14.         if((*it)%2==1)  
  15.         {  
  16.             it = a.erase(it);//注意这里和set的区别,erase当前元素后面的元素会自动不过了所以不需要++了  
  17.         }  
  18.         else  
  19.         {  
  20.             it++;  
  21.         }  
  22.     }  
  23. }  
  24.   
  25. bool isOdd(unsigned int x)  
  26. {  
  27.     if(x%2==1)  
  28.     {  
  29.         return true;  
  30.     }  
  31.     else  
  32.     {  
  33.         return false;  
  34.     }  
  35. }  
  36.   
  37. void removeOdd2(vector<int>& a)  
  38. {  
  39.     a.erase(remove_if(a.begin(), a.end(), isOdd),a.end());  
  40. }  
  41.   
  42.   
  43. int main()  
  44. {  
  45.     vector<int> a(NUM, 0);  
  46.     for(unsigned int i=0;i<NUM;++i)  
  47.     {  
  48.         a[i] = i;  
  49.     }  
  50.       
  51. //  removeOdd1(a);  
  52.     removeOdd2(a);  
  53.       
  54.     return 0;  
  55. }  
一般情况下我们最直观的方法就是用方法一,对于小数据量这些都没有问题,但是数据量大时运行速度就不行了。这是因为vector其实是对数组的封装你每找到一个奇数就移除那么其后面的数据是要向前移动的也即是每次删除一个元素后面的每个元素就要移动一次。对于一亿的数据量我在我的笔记本上没有等到运行结果!对于方法二remove_if()函数的原理是把vector中的偶数拷贝到另一个个vector中(其实remove_if()是在同一个vector上操作的),也即a中前一段存的是偶数而后一段存的还是原理的值,remove_if()返回的是所有符合条件的数据的下一个位置,因此使用a.erase()擦除后面的数据。以下代码是对其进行的验证:
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <iostream>  
  2. #include <algorithm>  
  3. #include <vector>  
  4. #include <set>  
  5.   
  6. using namespace std;  
  7.   
  8. const unsigned int NUM = 10;  
  9.   
  10. bool isOdd(unsigned int x)  
  11. {  
  12.     if(x%2==1)  
  13.     {  
  14.         return true;  
  15.     }  
  16.     else  
  17.     {  
  18.         return false;  
  19.     }  
  20. }  
  21.   
  22. int main()  
  23. {  
  24.     vector<int> a(NUM, 0);  
  25.     for(unsigned int i=0;i<NUM;++i)  
  26.     {  
  27.         a[i] = i;  
  28.     }  
  29.       
  30.     vector<int>::iterator t = remove_if(a.begin(), a.end(), isOdd);  
  31.     a.erase(t, a.end());  
  32.     for(vector<int>::iterator it = a.begin(); it<t; ++it)  
  33.     {  
  34.         cout << *it << endl;  
  35.     }  
  36.     cout << endl;  
  37.     for(vector<int>::iterator it=t; it<a.end(); ++it)  
  38.     {  
  39.         cout << *it << endl;  
  40.     }  
  41.       
  42.     return 0;  
  43. }  

二、set

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <iostream>  
  2. #include <algorithm>  
  3. #include <set>  
  4.   
  5. using namespace std;  
  6.   
  7. //const unsigned int NUM = 100000000;  
  8. const unsigned int NUM = 100;//因这里建树比较慢所以取值小了点  
  9.   
  10. void removeOdd1(set<int>& s)  
  11. {     
  12.     for(set<int>::iterator it = s.begin(); it!=s.end();++it)//注意这里和vector的不同,即使erase当前元素迭代器还是要++  
  13.     {  
  14.         if((*it)%2==1)  
  15.         {  
  16.             s.erase(it);  
  17.         }  
  18.     }  
  19. }  
  20.   
  21. int main()  
  22. {  
  23.     set<int> s;  
  24.     for(unsigned int i=0; i<NUM; ++i)  
  25.     {  
  26.         s.insert(i);  
  27.     }  
  28.     removeOdd1(s);  
  29.   
  30.     return 0;  
  31. }  

set是使用红黑树实现的因此插入和删除操作都在lg(n)时间内完成,因此直接删除就可以了。set是一个集合适合查找元素,而vector是一个连续的内存空间可以随机存取。但从查找一个元素来看set的效率要比vector高。

1、如果你需要高效的随即存取,而不在乎插入和删除的效率,使用vector

2、如果你要查找一个元素是否在某集合内存中,则使用set存储这个集合比较好

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值