編程第五十六天

c++ algorithm頭文件巡防算法

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章