for_each [STL]

所需的标头

   <algorithm>


原型

   template<class InputIterator, class Function> inline

       Function for_each(InputIterator first,
                         InputIterator last,
                         Function F)

注意在原型中的类/参数名与在头文件版本不匹配。某些已被修改,以提高可读性。

说明
for_each 算法范围 [first, last) 中的每个元素调用函数 F,并返回输入的参数 f。此函数不会修改序列中的任何元素。

示例代码
注意在示例代码段的首行/GX 在等效于 /EHsc Visual c + +.net 中或在 Visual C + + 2005年和默认设置
 

Code:
  1. //////////////////////////////////////////////////////////////////////  
  2. //  
  3. // Compile options needed: /GX  
  4. //  
  5. // count.cpp : Illustrates how to use the for_each function.  
  6. //  
  7. // Functions:  
  8. //  
  9. //   for_each  - Calls function F for every element in a range.  
  10. //  
  11. //   begin     - Returns an iterator that points to the first element  
  12. //               in a sequence.  
  13. //  
  14. //   end       - Returns an iterator that points one past the end of  
  15. //               a sequence.  
  16. //  
  17. // Written by Kalindi Sanghrajka  
  18. // of Microsoft Product Support Services,  
  19. // Software Core Developer Support.  
  20. // Copyright (c) 1996 Microsoft Corporation. All rights reserved.  
  21. //////////////////////////////////////////////////////////////////////  
  22.   
  23. // disable warning C4786: symbol greater than 255 character,  
  24. // okay to ignore  
  25.   
  26. #pragma warning(disable: 4786)  
  27.   
  28. #include <iostream>  
  29. #include <vector>  
  30. #include <algorithm>  
  31.   
  32. #if _MSC_VER > 1020   // if VC++ version is > 4.2  
  33.    using namespace std;  // std c++ libs implemented in std  
  34.    #endif  
  35.   
  36. // prints the cube of integer n  
  37. void PrintCube(int n)  
  38.   
  39. {  
  40.   
  41.     cout << n * n * n << " " ;  
  42.   
  43. }  
  44.   
  45. void main()  
  46.   
  47. {  
  48.   
  49.     const int VECTOR_SIZE = 8 ;  
  50.   
  51.     // Define a template class vector of integers  
  52.     typedef vector<int, allocator<int> > IntVector ;  
  53.   
  54.     //Define an iterator for template class vector of integer  
  55.     typedef IntVector::iterator IntVectorIt ;  
  56.   
  57.     IntVector Numbers(VECTOR_SIZE) ;   //vector containing numbers  
  58.   
  59.     IntVectorIt start, end, it ;  
  60.   
  61.     int i ;  
  62.   
  63.     // Initialize vector Numbers  
  64.     for (i = 0; i < VECTOR_SIZE; i++)  
  65.         Numbers[i] = i + 1 ;  
  66.   
  67.     start = Numbers.begin() ;   // location of first  
  68.                                 // element of Numbers  
  69.   
  70.     end = Numbers.end() ;       // one past the location  
  71.                                 // last element of Numbers  
  72.   
  73.     // print content of Numbers  
  74.     cout << "Numbers { " ;  
  75.     for(it = start; it != end; it++)  
  76.         cout << *it << " " ;  
  77.     cout << " }/n" << endl ;  
  78.   
  79.     // for each element in the range [first, last)  
  80.     // print the cube of the element  
  81.     for_each(start, end, PrintCube) ;  
  82.     cout << "/n/n" ;  
  83.   
  84. }  



输出:

Numbers { 1 2 3 4 5 6 7 8  }

1 8 27 64 125 216 343 512

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值