编程第五十六天

来源:互联网 发布:宁波行知小学总课表 编辑:程序博客网 时间:2024/04/18 23:50

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. }