1、remove_if

  1. /*  
  2.  *remove if example  
  3.  */  
  4. #include <iostream>   
  5. #include <algorithm>   
  6. #include <vector>   
  7.   
  8. using namespace std;   
  9.   
  10. bool isOdd(int i)   
  11. {   
  12.     return ((i%2)==1);   
  13. }   
  14.   
  15. int main(int argc,char **argv)   
  16. {   
  17.     int myints[] = {1,2,3,4,5,6,7,8,9}; //1 2 3 4 5 6 7 8 9   
  18.        
  19.     //bounds of range   
  20.     int *pbegin = myints;   
  21.     int *pend = myints + sizeof(myints)/sizeof(int);   
  22.        
  23.     pend = remove_if(pbegin,pend,isOdd);    // 2 4 6 8   
  24.   
  25.     for(int *p=pbegin;p!=pend;++p)   
  26.         cout << “ ” << *p ;   
  27.   
  28.     cout << endl;          
  29.   
  30.     return 0;   
  31. }  

2、sort:

  1. /*  
  2.  *unique example  
  3.  */    
  4. #include <iostream>   
  5. #include <algorithm>   
  6. #include <vector>   
  7.   
  8. using namespace std;   
  9.   
  10. bool myfunction(int i,int j)   
  11. {   
  12.     return (i<j);   
  13. }   
  14.   
  15. struct myclass{   
  16.     bool operator()(int i,int j)   
  17.     {   
  18.         return (i<j);   
  19.     }   
  20. }myobject;   
  21.   
  22. //print vector contains   
  23. void print(vector <int> v)   
  24. {   
  25.     vector<int>::iterator it;   
  26.     cout << “myvector contains: ”;   
  27.     for(it=v.begin();it!=v.end();it++)   
  28.         cout << *it << “ ”;   
  29.     cout << endl;          
  30. }   
  31.   
  32. int main(int argc,char **argv)   
  33. {   
  34.     int myints[] = {32,87,12,45,25,70,53,9};   
  35.     vector<int> myvector(myints,myints+8);   
  36.     vector<int>::iterator it;   
  37.   
  38.     print(myvector);                //32 87 12 45 25 70 53 9   
  39.   
  40.     //using default comparison(operator <)   
  41.     sort(myvector.begin(),myvector.begin()+4);   
  42.     print(myvector);                //12 32 45 87 25 70 53 9   
  43.        
  44.     //using function as comparison   
  45.     sort(myvector.begin()+4,myvector.end(),myfunction);   
  46.     print(myvector);                //12 32 45 87 9 25 53 70   
  47.        
  48.     //using object as comparison   
  49.     sort(myvector.begin(),myvector.end(),myobject);   
  50.     print(myvector);                //9 12 25 32 45 53 70 87   
  51.   
  52.     return 0;   
  53. }  

3、unique

  1. /*  
  2.  *unique algorithm example  
  3.  */  
  4. #include <iostream>   
  5. #include <algorithm>   
  6. #include <vector>   
  7.   
  8. using namespace std;   
  9.   
  10. bool myfunction(int i,int j)   
  11. {   
  12.     return (i==j);   
  13. }   
  14.   
  15. //print vector contains   
  16. void print(vector <int> v)   
  17. {   
  18.     vector<int>::iterator it;   
  19.     cout << “myvector contains: ”;   
  20.     for(it=v.begin();it!=v.end();it++)   
  21.         cout << *it << “ ”;   
  22.     cout << endl;          
  23. }   
  24.   
  25. int main(int argc,char **argv)   
  26. {   
  27.     int myints[] = {10,20,20,20,30,30,20,20,10};    // 10 20 20 20 30 30 20 20 10   
  28.     vector<int> myvector (myints,myints+9);   
  29.     vector<int>::iterator it;   
  30.     print(myvector);   
  31.     cout << myvector.size() << endl;        //9   
  32.   
  33.     // using default comparison:   
  34.     it = unique (myvector.begin(), myvector.end()); // 10 20 30 20 10 ?  ?  ?  ?                                                    
  35.     cout << myvector.size() << endl;        //9    
  36.     myvector.resize( it - myvector.begin() );       // 10 20 30 20 10   
  37.   
  38.     // using predicate comparison:   
  39.     unique (myvector.begin(), myvector.end(), myfunction);   // (no changes)   
  40.     print(myvector);   
  41.     cout << myvector.size() << endl;        //5   
  42.   
  43.     return 0;   
  44. }     
C++ Vector容器中remove_if、sort、unique的简单用法

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据