【C++ STL】算法 中各種算法解析


一,巡防算法

        for_each(容器起始地址,容器結束地址,要執行的方法)


[html] view plaincopy
  1. #include <iostream>  
  2. #include <algorithm>  
  3. #include <vector>  
  4.   
  5. using namespace std;  
  6.   
  7. template<class T>  
  8. struct plus2  
  9. {  
  10.     void operator()(T&x)const  
  11.     {  
  12.         x+=2;  
  13.     }  
  14.       
  15. };  
  16.   
  17. void printElem(int& elem)  
  18. {  
  19.   cout << elem << endl;  
  20. }  
  21.   
  22.   
  23. int main()  
  24. {  
  25.     int ia[]={0,1,2,3,4,5,6};  
  26.     for_each(ia,ia+7,printElem);//輸出  
  27.       
  28.     int ib[]={7,8,9,10,11,12,13};  
  29.     vector<int> iv(ib,ib+7);  
  30.     for_each(iv.begin(),iv.end(),plus2<int>());//更改元素  
  31.     for_each(iv.begin(),iv.end(),printElem);//輸出  
  32.       
  33.       
  34.     return 0;  
  35. }  


二,find算法

   int *find(int *begin,int *end,int  value)

   前閉後合的區間 begin,end中,查找value如果查找到了就返回第一個符合條件的元素,否則返回end指針

[html] view plaincopy
  1. #include <iostream>  
  2. #include <algorithm>  
  3.   
  4. using namespace std;  
  5.   
  6. void printElem(int& elem)  
  7. {  
  8.   cout << elem << endl;  
  9. }  
  10.   
  11.   
  12. int main()  
  13. {  
  14.     int ia[]={0,1,2,3,4,5,6};  
  15.       
  16.     int *ifind(ia,ia+7,9);//在整個數組中查找元素 9   
  17.     int *jfind(ia,ia+7,3);//在整個數組中查找元素 3  
  18.     int *end=ia+7;//數組最後位置   
  19.     if(i == end)   
  20.        cout<<"沒有找到元素 9"<<endl;  
  21.     else   
  22.        cout<<"找到元素9"<<endl;  
  23.          
  24.     if(j == end)   
  25.        cout<<"沒有找到元素 3"<<endl;  
  26.     else   
  27.        cout<<"找到元素"<<*j<<endl;  
  28.     return 0;  
  29. }  


三,數值算法

        包含在<numeric>頭文件中

[html] view plaincopy
  1. #include <iostream>  
  2. #include <numeric>  //數值算法   
  3. #include <vector>  
  4. #include <functional>   
  5. #include <iterator>   
  6.   
  7. #include <math.h>   
  8.  using namespace std;  
  9.    
  10.  int main()  
  11.  {  
  12.     int ia[]={1,2,3,4,5};  
  13.     vector<int> iv(ia,ia+5);  
  14.       
  15.     cout<<accumulate(iv.begin(),iv.end(),0)<<endl; //累加  初值爲0   
  16.     cout<<accumulate(iv.begin(),iv.end(),0,minus<int>())<<endl; //累加 符號位負  
  17.        
  18.     cout<<inner_product(iv.begin(),iv.end(),iv.begin(),10)<<endl;//兩個數組內積  初值爲10   
  19.     cout<<inner_product(iv.begin(),iv.end(),iv.begin(),10,minus<int>(),plus<int>())<<endl;//10-(1+1)-(2+2)  
  20.       
  21.     ostream_iterator<int> oite(cout," ");//迭代器綁定到cout上作爲輸出使用  
  22.     partial_sum(iv.begin(),iv.end(),oite);//依次輸出前n個數的和   
  23.       
  24.     cout<<endl;   
  25.     partial_sum(iv.begin(),iv.end(),oite,minus<int>());//依次輸出第一個數減去(除第一個數外到當前數的和)  
  26.       
  27.     cout<<endl;   
  28.     adjacent_difference(iv.begin(),iv.end(),oite); //輸出相鄰元素差值 前面-後面  
  29.       
  30.     cout<<endl;   
  31.     adjacent_difference(iv.begin(),iv.end(),oite,plus<int>()); //輸出相鄰元素差值 前面+後面  。前面更改影響後面元素   
  32.        
  33.        
  34.     cout<<endl;   
  35.     cout<<pow(10,3)<<endl; // 平方  
  36.       
  37.     /*  VC 不支持   只有安裝了才SGI STL支持   
  38.     int n=3;  
  39.     iota(iv.begin(),iv.end(),n);//在指定區間填入n  n+1 n+2  
  40.     for(int i=0;i<iv.size();++i)  
  41.         cout<<iv[i]<<" ";   
  42.           
  43.         */   
  44.     return 0;  
  45.  }  
  46.    


四,基本算法

[html] view plaincopy
  1. #include <iostream>  
  2. #include <algorithm>  
  3. #include <vector>  
  4.   
  5. using namespace std;  
  6.   
  7. template<typename T>  
  8. struct display  
  9. {  
  10.     void operator()(const T  &x)const  
  11.     {  
  12.         cout<<x<<" ";  
  13.     }  
  14.       
  15. };  
  16.   
  17.   
  18. int main()  
  19. {  
  20.     int ia[]={0,1,2,3,4,5,6,7,8};  
  21.     vector<int> iv1(ia,ia+5);  
  22.     vector<int> iv2(ia,ia+9);  
  23.       
  24.     pair<vector<int>::iterator,vector<int>::iterator> pa;  
  25.     pa=mismatch(iv1.begin(),iv1.end(),iv2.begin());  
  26.     cout<<"兩個數組不同點--第一個數組點:"<<*(pa.first)<<endl; //這樣寫很危險,應該判斷是否到達end   
  27.     cout<<"兩個數組不同點--第二個數組點:"<<*(pa.second)<<endl;  
  28.       
  29.     //更改之後  
  30.     if(pa.first == iv1.end())  
  31.         cout<<"第一個數組與第二個數組匹配"<<endl;   
  32.           
  33.     cout<<equal(iv1.begin(),iv1.end(),iv2.begin())<<endl;// 1 表示 相等,因爲只比較跟 iv1長度大小的數組   
  34.     cout<<equal(iv1.begin(),iv1.end(),&ia[3])<<endl;// 0 表示 不相等   
  35.     cout<<equal(iv1.begin(),iv1.end(),&ia[3],less<int>())<<endl;// 1 表示 前者小於後者  
  36.       
  37.     fill(iv1.begin(),iv1.end(),9);//將iv1區間內填滿 9  
  38.     for_each(iv1.begin(),iv1.end(),display<int>());  
  39.     cout<<endl;     
  40.       
  41.     fill_n(iv1.begin(),3,6);//從iv1區間開始填 3個6   
  42.     for_each(iv1.begin(),iv1.end(),display<int>());  
  43.     cout<<endl;  
  44.       
  45.       
  46.     vector<int>::iterator ite1=iv1.begin();  
  47.     vector<int>::iterator ite2=ite1;  
  48.     advance(ite2,3);//向前跳3個  
  49.       
  50.     iter_swap(ite1,ite2);//交換迭代器指向的元素  
  51.     for_each(iv1.begin(),iv1.end(),display<int>());  
  52.        
  53.     cout<<"\nmax:"<<max(*ite1,*ite2)<<endl;  
  54.     cout<<"min:"<<min(*ite1,*ite2)<<endl;  
  55.       
  56.     swap(*ite1,*ite2);  
  57.     for_each(iv1.begin(),iv1.end(),display<int>());  
  58.       
  59.       
  60.     cout<<endl;  
  61.     string stra1[]={"a","b","c"};  
  62.     string stra2[]={"d","e","f"};  
  63.       
  64.     cout<<lexicographical_compare(stra1,stra1+2,stra2,stra2+2)<<endl;//按照字典序 前者小於後者   
  65.     cout<<lexicographical_compare(stra1,stra1+2,stra2,stra2+2,greater<string>())<<endl;//按照字典序 前者不大於後者  
  66.       
  67.           
  68.     return 0;  
  69. }  

五,copy()對不同容器複製;關於輸出區間與輸入區間重疊的討論

[html] view plaincopy
  1. #include <iostream>  
  2. #include <algorithm>  
  3. #include <deque>  
  4.   
  5. using namespace std;  
  6. template<class T>  
  7. struct display  
  8. {  
  9.     void operator()(const T &x)const  
  10.     {  
  11.         cout<<x<<" ";  
  12.     }  
  13. };  
  14.   
  15. int main()  
  16. {  
  17.     //以下複製區間沒有問題   
  18.     int ia1[]={0,1,2,3,4,5,6,7,8};  
  19.     copy(ia1+2,ia1+7,ia1);//將下標2-6複製給 1-5  
  20.     for_each(ia1,ia1+9,display<int>()); //2,3,4,5,6,5,6,7,8  
  21.     cout<<endl;  
  22.        
  23.     //輸出區間的起點與輸入區間重疊,可能會有問題。但本例copy採用memmove()執行實際複製操作   
  24.     int ia2[]={0,1,2,3,4,5,6,7,8};  
  25.     copy(ia2+2,ia2+7,ia2+4);//將下標2-6複製給 4-8  
  26.     for_each(ia2,ia2+9,display<int>()); //0,1,2,3,2,3,4,5,6  
  27.     cout<<endl;  
  28.       
  29.     //以下複製區間沒有問題   
  30.     int ia3[]={0,1,2,3,4,5,6,7,8};  
  31.     deque<int> id(ia3,ia3+9);  
  32.     deque<int>::iterator first=id.begin();  
  33.     deque<int>::iterator last=id.end();  
  34.     deque<int>::iterator result=id.begin();  
  35.     ++++first;  
  36.     cout<<*first<<endl;  
  37.     ----last;  
  38.     cout<<*last<<endl;  
  39.     cout<<*result<<endl;  
  40.     copy(first,last,result);  
  41.     for_each(id.begin(),id.end(),display<int>());//2,3,4,5,6,5,6,7,8  
  42.     cout<<endl;  
  43.       
  44.     //以下複製區間存在問題,由於實際複製沒有采用memove(),結果錯誤   
  45.     int ia4[]={0,1,2,3,4,5,6,7,8};  
  46.     deque<int> ide(ia4,ia4+9);  
  47.     deque<int>::iterator first1=ide.begin();  
  48.     deque<int>::iterator last1=ide.end();  
  49.     deque<int>::iterator result1=ide.begin();  
  50.     advance(result1,4);//注意這裏跟上面不一樣   
  51.     ++++first1;  
  52.     cout<<*first1<<endl;  
  53.     ----last1;  
  54.     cout<<*last1<<endl;  
  55.     cout<<*result1<<endl;  
  56.     copy(first1,last1,result1);  
  57.     for_each(ide.begin(),ide.end(),display<int>());// 0,1,2,3,2,3,2,3,2不是預期的 0,1,2,3,2,3,4,5,6  
  58.     cout<<endl;  
  59.       
  60.       
  61.       
  62.     return 0;  
  63. }   

【注意】如果以vector 容器替代deque容器則每種情況都正確,因爲vector迭代器其實是個源生指針,調用的copy()算法以mommove()執行實際複製。

                copy_backward(first,last,result);  //逆向複製,將迭代器first - last位置的元素逆向複製到 從result-1開始的逆向區間


補充:

        原型:void *memmove( void  * dest, const   void  * src, size_t  count );

  用法:#include <string.h>或#include <memory.h>
  功能:由src所指內存區域複製count個字節到dest所指內存區域。
  說明:src和dest所指內存區域可以重疊,但複製後dest內容會被更改。函數返回指向dest的指針。採取先拷貝再複製的方式,有效解決了dest和src區域重疊問題
  相關函數:memset、memcpy、strcpy 參考博文http://blog.csdn.net/tianshuai11/article/details/7624419

實例

  
[html] view plaincopy
  1. #include <stdio.h>  
  2. #include <string.h>  
  3. int main()  
  4. {  
  5.   char s[]="Golden Global View";  
  6.    memmove(s,s+7,strlen(s)+1-7);  
  7.    printf("%s",s);  
  8.       
  9.   return 0;  
  10. }  

六,Set方法

[html] view plaincopy
  1. #include <iostream>  
  2. #include <set>  
  3. #include <algorithm>  
  4. #include <iterator>  
  5. using namespace std;  
  6.   
  7. template <class T>  
  8. struct display  
  9. {  
  10.     void operator()(const T &x)  
  11.     {  
  12.         cout<<x<<" ";  
  13.     }  
  14.       
  15. };  
  16. int main()  
  17. {  
  18.     int ia1[]={1,3,5,7,9,11};  
  19.     int ia2[]={1,1,2,3,5,8,13};  
  20.       
  21.     multiset<int> s1(ia1,ia1+6);  
  22.     multiset<int> s2(ia2,ia2+7);  
  23.     for_each(s1.begin(),s1.end(),display<int>());  
  24.     cout<<endl;  
  25.     for_each(s2.begin(),s2.end(),display<int>());  
  26.     cout<<endl;  
  27.       
  28.     multiset<int>::iterator first1 = s1.begin();  
  29.     multiset<int>::iterator last1 = s1.end();  
  30.     multiset<int>::iterator first2 = s2.begin();  
  31.     multiset<int>::iterator last2 = s2.end();  
  32.       
  33.     cout<<"union of s1 and s2: ";  
  34.     //兩個集合合併,相同元素個數取 max(m,n)。   
  35.     set_union(first1,last1,first2,last2,ostream_iterator<int>(cout," "));  
  36.     cout<<endl;  
  37.       
  38.     first1=s1.begin();  
  39.     first2=s2.begin();  
  40.     cout<<"Intersection of s1 and s2: ";  
  41.     //兩個集合交集,相同元素個數取 min(m,n).  
  42.     set_intersection(first1,last1,first2,last2,ostream_iterator<int>(cout," "));   
  43.     cout<<endl;  
  44.       
  45.     first1=s1.begin();  
  46.     first2=s2.begin();  
  47.     cout<<"Intersection of s1 and s2: ";  
  48.     //兩個集合差集 就是去掉S1中 的s2   
  49.     set_difference(first1,last1,first2,last2,ostream_iterator<int>(cout," "));   
  50.     cout<<endl;  
  51.        
  52.     first1=s1.begin();  
  53.     first2=s2.begin();  
  54.     cout<<"Intersection of s1 and s2: ";  
  55.     //兩個集合對稱差集:就是取兩個集合互相沒有的元素 。兩個排序區間,元素相等指針後移,不等輸出小的並前進   
  56.     //相同元素的個數 abs(m-n)   
  57.     set_symmetric_difference(first1,last1,first2,last2,ostream_iterator<int>(cout," "));   
  58.     cout<<endl;  
  59.       
  60.       
  61.     return 0;  
  62. }  

 

 七,其他算法(運算邏輯相對單純的算法)

[html] view plaincopy
  1. #include <iostream>  
  2. #include <algorithm>  
  3. #include <vector>  
  4. #include <functional>  
  5. #include <vector>   
  6.   
  7.   
  8. using namespace std;  
  9.   
  10. template <class T>  
  11. struct display  
  12. {  
  13.     void operator()(const T &x)const  
  14.     {  
  15.         cout<<x<<" ";   
  16.     }   
  17.       
  18. };   
  19.   
  20. struct even  
  21. {  
  22.     bool operator()(int x)const  
  23.     {  
  24.         return x%2?false:true;   
  25.     }   
  26. };  
  27.   
  28. class even_by_two  
  29. {  
  30. private:  
  31.     static int _x; //注意靜態變量   
  32. public:  
  33.     int operator()()const  
  34.     {  
  35.         return _x+=2;   
  36.     }      
  37.       
  38. };  
  39. int even_by_two::_x=0;   
  40.   
  41. int main()  
  42. {  
  43.     int ia[]={0,1,2,3,4,5,6,6,6,7,8};  
  44.     vector<int> iv(ia,ia+sizeof(ia)/sizeof(int));  
  45.       
  46.     //找出iv之中相鄰元素值相等的第一個元素   
  47.     cout<<*adjacent_find(iv.begin(),iv.end())<<endl;    
  48.     cout<<*adjacent_find(iv.begin(),iv.end(),equal_to<int>())<<endl; //仿函數  
  49.       
  50.     cout<<count(iv.begin(),iv.end(),6)<<endl;//統計6的個數   
  51.     cout<<count_if(iv.begin(),iv.end(),bind2nd(less<int>(),7))<<endl;//統計小於7的元素的個數 :9個  
  52.       
  53.     cout<<*find(iv.begin(),iv.end(),4)<<endl; //返回元素爲4的元素的下標位置  
  54.       
  55.     cout<<*find_if(iv.begin(),iv.end(),bind2nd(greater<int>(),2))<<endl; //返回大於2的第一個元素的位置:3  
  56.       
  57.     vector<int> iv2(ia+6,ia+8);//6 6  
  58.       
  59.     for(int i=0;i<iv2.size();++i)  
  60.       cout<<iv2[i]<<" ";   
  61.         
  62.     cout<<endl;   
  63.     //返回iv序列中 iv2序列 出現的最後一個位置(再往後三個位置的值):8   
  64.     cout<<"find_end:"<<*(find_end(iv.begin(),iv.end(),iv2.begin(),iv2.end())+3)<<endl;   
  65.      //返回iv序列中 iv2序列 出現的最後一個位置(再往後三個位置的值):7  
  66.     cout<<"find_first_of:"<<*(find_first_of(iv.begin(),iv.end(),iv2.begin(),iv2.end())+3)<<endl;  
  67.       
  68.        
  69.      for_each(iv.begin(),iv.end(),display<int>());   
  70.      cout<<endl;  
  71.        
  72.      //遍歷整個iv2區間並執行 even_by_two操作   
  73.      generate(iv2.begin(),iv2.end(),even_by_two());  
  74.      for_each(iv2.begin(),iv2.end(),display<int>());   
  75.      cout<<endl;  
  76.        
  77.      //遍歷區間(給出起點和長度),對每個遍歷元素執行even_by_two操作   
  78.      generate_n(iv.begin(),3,even_by_two());  
  79.      for_each(iv.begin(),iv.end(),display<int>()); //由於_X是static 所以接着 增長   
  80.      cout<<endl;  
  81.        
  82.      //刪除元素6 尾端可能有殘餘數據   
  83.      remove(iv.begin(),iv.end(),6);  
  84.      for_each(iv.begin(),iv.end(),display<int>()); //由於_X是static 所以接着 增長   
  85.      cout<<endl; //8 10 3 4 5 7 8 6 6 7 8 (最後四個是殘留數據)   
  86.         
  87.      //去除value 然後將一個容器的元素複製到另一個 容器。仍然可能有殘留元素   
  88.       vector<int> iv3(12);//重新申請空間  
  89.       remove_copy(iv.begin(),iv.end(),iv3.begin(),6);  
  90.       for_each(iv3.begin(),iv3.end(),display<int>()); //由於_X是static 所以接着 增長   
  91.       cout<<endl; //8 10 3 4 5 7 8 7 8 0 0 (最後兩個是殘留元素)   
  92.         
  93.       //將小於6的元素 "刪除" iv 此時爲 8 10 3 4 5 7 8 6 6 7 8   
  94.       remove_if(iv.begin(),iv.end(),bind2nd(less<int>,6));  
  95.       for_each(iv1.begin(),iv1.end(),display<int>()); //由於_X是static 所以接着 增長   
  96.       cout<<endl; //8 10 7 8 6 6 7 8 6 7 8 (最後三個是殘留元素)   
  97.         
  98.         
  99.       //將小於7的元素 "刪除"  iv3元素:8 10 3 4 5 7 8 7 8 0 0 (最後兩個是殘留元素)  
  100.       remove_copy_if(iv.begin(),iv.end(),iv3.begin(),bind2nd(less<int>,7));  
  101.       for_each(iv3.begin(),iv3.end(),display<int>()); //由於_X是static 所以接着 增長   
  102.       cout<<endl; //8 10 7 8 7 8 7 8 8 0 0(最後三個殘留元素)   
  103.         
  104.         
  105.      return 0;   
  106. }   

第二段算法示例:

[html] view plaincopy
  1. #include <iostream>  
  2. #include <algorithm>  
  3. #include <vector>  
  4. #include <functional>  
  5.   
  6. using namespace std;  
  7.   
  8. template <class T>  
  9. struct display  
  10. {  
  11.     void operator()(const T &x)const  
  12.     {  
  13.         cout<<x<<" ";   
  14.     }   
  15.       
  16. };   
  17.   
  18.    
  19. int main()  
  20. {  
  21.     int ia[]={8,10,7,8,6,6,7,8,6,7,8};  
  22.     vector<int> iv(ia,ia+sizeof(ia)/sizeof(int));  
  23.       
  24.     //將容器中6 替換爲 3   
  25.     replace(iv.begin(),iv.end(),6,3);  
  26.     for_each(iv.begin(),iv.end(),display<int>()); //由於_X是static 所以接着 增長   
  27.     cout<<endl; //iv:8 10 7 8 3 3 7 8 3 7 8   
  28.       
  29.     vector<int> iv2(12);   
  30.     //將容器中3 替換爲 5 放入另一個容器   
  31.     replace_copy(iv.begin(),iv.end(),iv2.begin(),3,5);  
  32.     for_each(iv2.begin(),iv2.end(),display<int>()); //由於_X是static 所以接着 增長   
  33.     cout<<endl; //iv2:8 10 7 8 5 5 7 8 5 7 8 0(最後y一個殘留元素)   
  34.         
  35.     //將容器中小於 5 替換爲 2   
  36.     replace_if(iv.begin(),iv.end(),bind2nd(less<int>(),5),2);  
  37.     for_each(iv.begin(),iv.end(),display<int>()); //由於_X是static 所以接着 增長   
  38.     cout<<endl; //iv:8 10 7 8 2 5 7 8 2 7 8   
  39.   
  40.     //將容器中小於 5 替換爲 2   
  41.     replace_copy_if(iv.begin(),iv.end(),iv2.begin(),bind2nd(equal_to<int>(),8),9);  
  42.     for_each(iv2.begin(),iv2.end(),display<int>()); //由於_X是static 所以接着 增長   
  43.     cout<<endl; //iv2:9 10 7 8 2 5 7 9 2 7 8 0(最後一個殘留元素)   
  44.       
  45.     //逆向重排每一個元素 (倒置)   
  46.     reverse(iv.begin(),iv.end());   
  47.     for_each(iv.begin(),iv.end(),display<int>());  
  48.     cout<<endl; //iv:8 7 2 8 7 5 2 8 7 10 8  
  49.       
  50.     //逆向重排每一個元素 (倒置)   
  51.     reverse_copy(iv.begin(),iv.end(),iv2.begin());   
  52.     for_each(iv2.begin(),iv2.end(),display<int>());  
  53.     cout<<endl; //iv2:8 10 7 8 2 5 7 8 2 7 8 0 (最後一個殘留元素)    
  54.      
  55.     // 互換元素  [bigin,middle)  [middle,end)   
  56.     rotate(iv.begin(),iv.begin()+4,iv.end());  
  57.     for_each(iv.begin(),iv.end(),display<int>());  
  58.     cout<<endl;//iv:7 2 2 8 7 10 8 8 7 2 8   
  59.          
  60.     // 互換元素  [bigin,middle)  [middle,end)   
  61.     rotate_copy(iv.begin(),iv.begin()+5,iv.end(),iv2.begin());  
  62.     for_each(iv2.begin(),iv2.end(),display<int>());  
  63.     cout<<endl;//iv2:10 8 8 7 2 8 7 2 2 8 7 0 (最後一個是殘留元素)   
  64.       
  65.       
  66.     //在iv中查找 子序列 2 8 第一次出現的位置的元素   
  67.     int ia2[3]={2,8};  
  68.     vector<int> iv3(ia2,ia2+2);  
  69.     cout<<*search(iv.begin(),iv.end(),iv3.begin(),iv3.end())<<endl; //2   
  70.       
  71.     //在iv中查找 2個8 出現的第一個位置的元素   
  72.     cout<<*search_n(iv.begin(),iv.end(),2,8)<<endl; //8   
  73.       
  74.     //在iv中查找 3個小於8 出現的第一個位置的元素   
  75.     cout<<*search_n(iv.begin(),iv.end(),3,8,less<int>())<<endl; //7  
  76.       
  77.     swap_ranges(iv3.begin(),iv3.end(),iv.begin());  
  78.     cout<<"iv:";   
  79.     for_each(iv.begin(),iv.end(),display<int>());//iv:2 8 2 8 7 10 8 8 7 2 8   
  80.     cout<<endl;  
  81.     cout<<"iv3:";   
  82.     for_each(iv3.begin(),iv3.end(),display<int>()); //iv3: 7 2   
  83.     cout<<endl;  
  84.         
  85.     //全部減2   
  86.     transform(iv.begin(),iv.end(),iv.begin(),bind2nd(minus<int>(),2));  
  87.     for_each(iv.begin(),iv.end(),display<int>());//0 6 0 6 5 8 6 6 5 0 6   
  88.     cout<<endl;   
  89.       
  90.      //兩個區間元素相加然後放到 iv上   
  91.     transform(iv.begin(),iv.end(),iv.begin(),iv.begin(),plus<int>());  
  92.     for_each(iv.begin(),iv.end(),display<int>());  
  93.     cout<<endl; //0 12 0 12 10 16 12 12 10 0 12  
  94.       
  95.        
  96.      return 0;   
  97. }   


第三段算法示例:


[html] view plaincopy
  1. #include <iostream>  
  2. #include <algorithm>  
  3. #include <vector>  
  4. #include <functional>  
  5.   
  6. using namespace std;  
  7.   
  8. template <class T>  
  9. struct display  
  10. {  
  11.     void operator()(const T &x)const  
  12.     {  
  13.         cout<<x<<" ";   
  14.     }   
  15.       
  16. };   
  17. struct even  
  18. {  
  19.     bool operator()(int x)const  
  20.     {  
  21.         return x%2?false:true;   
  22.     }   
  23. };  
  24.    
  25. int main()  
  26. {  
  27.     int ia[]={0,1,2,3,4,5,6,6,6,7,8};  
  28.     vector<int> iv(ia,ia+sizeof(ia)/sizeof(int));  
  29.     vector<int> iv2(ia+4,ia+8);//4 5 6 6  
  30.     vector<int> iv3(15);  
  31.       
  32.     cout<<*max_element(iv.begin(),iv.end())<<endl;  
  33.     cout<<*min_element(iv.begin(),iv.end())<<endl;   
  34.   
  35.     //判斷iv2中元素是否都出現在 iv中   
  36.     cout<<includes(iv.begin(),iv.end(),iv2.begin(),iv2.end())<<endl;   
  37.        
  38.     //iv 和iv2合併到iv3中   
  39.     merge(iv.begin(),iv.end(),iv2.begin(),iv2.end(),iv3.begin());   
  40.     for_each(iv3.begin(),iv3.end(),display<int>());  
  41.     cout<<endl;   
  42.       
  43.     //符合條件的 放到前面,不符合條件的放到後面   
  44.     partition(iv3.begin(),iv3.end(),even());  
  45.     for_each(iv3.begin(),iv3.end(),display<int>());  
  46.     cout<<endl;   
  47.       
  48.     //去除連續並且重複的元素   
  49.     unique(iv.begin(),iv.end());   
  50.     for_each(iv.begin(),iv.end(),display<int>());  
  51.     cout<<endl;   
  52.       
  53.       
  54.     //去除連續並且重複的元素   
  55.     unique_copy(iv.begin(),iv.end(),iv3.begin());   
  56.     for_each(iv3.begin(),iv3.end(),display<int>());  
  57.     cout<<endl;   
  58.       
  59.       
  60.      return 0;   
  61. }   



 

八,複雜算法示例(解釋在源碼中)

[html] view plaincopy
  1. #include <iostream>  
  2. #include <vector>  
  3. #include <functional>  
  4. #include <algorithm>  
  5.   
  6. using namespace std;  
  7.   
  8. struct even //是否是奇數   
  9. {  
  10.     bool operator()(int x)const  
  11.     {  
  12.         return x%2?false:true;   
  13.     }   
  14.       
  15. };   
  16. template<class T>   
  17. struct display  
  18. {  
  19.     void operator()(T &x)const  
  20.     {  
  21.         cout<<x<<" ";   
  22.     }   
  23.       
  24. };   
  25.   
  26. int main()  
  27. {  
  28.     int ia[] = {12,17,20,22,23,30,33,40};  
  29.     vector<int> iv(ia,ia+sizeof(ia)/sizeof(int));  
  30.       
  31.     //返回可以插入的第一個位置   
  32.     cout<<*lower_bound(iv.begin(),iv.end(),21)<<endl; //22   
  33.     cout<<*upper_bound(iv.begin(),iv.end(),21)<<endl; //22   
  34.     //返回可以插入的最後一個位置   
  35.     cout<<*lower_bound(iv.begin(),iv.end(),22)<<endl; //22   
  36.     cout<<*upper_bound(iv.begin(),iv.end(),22)<<endl; //23  
  37.       
  38.     //二分查找某個元素,返回是否找到   
  39.     cout<<binary_search(iv.begin(),iv.end(),33)<<endl; //1  
  40.     cout<<binary_search(iv.begin(),iv.end(),34)<<endl; //0   
  41.        
  42.     //生成下一個排列組合(字典序)   
  43.     next_permutation(iv.begin(),iv.end());   
  44.     for_each(iv.begin(),iv.end(),display<int>());  
  45.     cout<<endl;   
  46.        
  47.     prev_permutation(iv.begin(),iv.end());   
  48.     for_each(iv.begin(),iv.end(),display<int>());  
  49.     cout<<endl;   
  50.       
  51.     //打亂順序   
  52.     random_shuffle(iv.begin(),iv.end());   
  53.     for_each(iv.begin(),iv.end(),display<int>());  
  54.     cout<<endl;  
  55.       
  56.     //找出最小的4個元素 放在前四個 後面順序不一定有序   
  57.     partial_sort(iv.begin(),iv.begin()+4,iv.end());   
  58.     for_each(iv.begin(),iv.end(),display<int>());  
  59.     cout<<endl;  
  60.       
  61.     //排序(缺省爲遞增排序)   
  62.     sort(iv.begin(),iv.end());  
  63.     for_each(iv.begin(),iv.end(),display<int>());  
  64.     cout<<endl;  
  65.       
  66.      //排序(設置爲遞減)   
  67.     sort(iv.begin(),iv.end(),greater<int>());  
  68.     for_each(iv.begin(),iv.end(),display<int>());  
  69.     cout<<endl;  
  70.       
  71.     iv.push_back(22);  
  72.     iv.push_back(30);  
  73.     iv.push_back(17);  
  74.       
  75.       
  76.     //排序並保持原相對位置   
  77.     stable_sort(iv.begin(),iv.end());   
  78.     for_each(iv.begin(),iv.end(),display<int>());  
  79.     cout<<endl;//12 17 17 20 22 22 23 30 30 33 40   
  80.       
  81.     pair<vector<int>::iterator,vector<int>::iterator> pairIte;  
  82.     //返回等於22的一個小區間   
  83.     pairIte = equal_range(iv.begin(),iv.end(),22);   
  84.     cout<<*(pairIte.first)<<endl;//lowerbound 22   
  85.     cout<<*(pairIte.second)<<endl; //upperbound 23  
  86.       
  87.       
  88.     //這裏返回一個空區間   
  89.     pairIte = equal_range(iv.begin(),iv.end(),25);   
  90.     cout<<*(pairIte.first)<<endl;//lowerbound 30   
  91.     cout<<*(pairIte.second)<<endl; //upperbound 30  
  92.        
  93.      //打亂順序   
  94.     random_shuffle(iv.begin(),iv.end());   
  95.     for_each(iv.begin(),iv.end(),display<int>());  
  96.     cout<<endl;  
  97.       
  98.     //將小於iv.begin+5的放到左邊   
  99.     nth_element(iv.begin(),iv.begin()+5,iv.end());   
  100.     for_each(iv.begin(),iv.end(),display<int>());  
  101.     cout<<endl;  
  102.       
  103.     //將小於iv.begin+5的放到右邊   
  104.     nth_element(iv.begin(),iv.begin()+5,iv.end(),greater<int>());   
  105.     for_each(iv.begin(),iv.end(),display<int>());  
  106.     cout<<endl;  
  107.       
  108.     //排序   
  109.     stable_sort(iv.begin(),iv.end(),even());  
  110.     for_each(iv.begin(),iv.end(),display<int>());  
  111.     cout<<endl;  
  112.       
  113.       
  114.     return 0;   
  115. }  








原文地址:http://blog.csdn.net/tianshuai1111/article/details/7674327
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章