for_each 函数 in STL

//####################################################################### ios

  1. //# Created Time: 2011-3-7 18:02:35
  2. //# File Name: for_each.cpp
  3. //# Description: 
  4. //#######################################################################
  5. #include <iostream>
  6. #include <string>
  7. #include <vector>
  8. #include <algorithm>
  9. using namespace std;  
  10. typedef std::vector<std::pair<std::string, std::string> > pv;  
  11. typedef pv::iterator pitr;  
  12. struct p{  
  13. void operator()(const pitr &iter) const{  
  14.         cout<<iter->first<<"/t"<<iter->second<<endl;  
  15.     }  
  16. };  
  17. int main(){  
  18.     pv v;  
  19.     v.push_back(make_pair("Hello, ", "the world!"));  
  20.     for_each(v.begin(), v.end(), p());  
  21. return 0;  

编译的时候提示: 函数

for_each(v.begin(), v.end(), p()); 这一句中类p中运算()不匹配。 网站

而有人给出了一个版本: spa

  1. struct p  
  2. {  
  3. void operator()(const pair<string,string>& itr) const{  
  4.         cout<<itr.first<<"/t"<<itr.second<<endl;  
  5.     }  
  6. void operator()(int& a){  
  7.         ++a;  
  8.     }  
  9. }; 

问题解决,编译也经过。可是就是不明白为何作样作就能够。 指针


刚开始不明白他的意思。后来才明白做为for_each的函数的形参类型有特别的要求。 code

总结以下: for_each的形参的函数或者类对应的运算符()的形参的有两点要求: 对象

第一:必须为单参数; 接口

第二:必须跟*iter的类型一致或者兼容。既它的形参不能是迭代器或者指针。只能是对象或者它的引用。 ip

为了考证这一点,我到cpluplus网站去看了for_each的接口定义。get

  1. template<class InputIterator, class Function> 
  2.   Function for_each(InputIterator first, InputIterator last, Function f)   //这里f至关于一个函数指针,根据其在函数体中的用法 要求其原型为 FunPtr (*)(容器元素或其引用).
  3.   { 
  4. //这就是标准库与本身写的程序对正确性要求的区别,你使用标准库,并非只要给出的参数类型表面上符合就好了,它可能会有一些隐含的条件.好比这里.
  5.     for ( ; first!=last; ++first ) f(*first); 
  6.     return f; 
  7.   } 
连接:http://www.cplusplus.com/reference/algorithm/for_each/ 1  
最后附上一个程序验证这一点:

  1. //####################################################################### 
  2. //# Created Time: 2011-3-7 18:02:35 
  3. //# File Name: for_each.cpp 
  4. //# Description:  
  5. //####################################################################### 
  6. #include <iostream> 
  7. #include <string> 
  8. #include <vector> 
  9. #include <map> 
  10. #include <algorithm> 
  11. using namespace std; 
  12. typedef std::vector<std::pair<std::string, std::string> > pv; 
  13. typedef pv::iterator pitr; 
  14. struct p{ 
  15.     void operator()(const pair<string, string> &v) const{ 
  16.         cout<<v.first<<"/t"<<v.second<<endl; 
  17.     } 
  18.     void operator()(int  a){ 
  19.         ++a; 
  20.     } 
  21. }; 
  22. void print(int n)  
  23.     cout << n << " "; 
  24. int main(){ 
  25.     pv v; 
  26.     vector<int>   ivec; 
  27.     for (int i=0; i < 10; i++) 
  28.         ivec.push_back(i); 
  29.      
  30.     v.push_back(make_pair("Hello, ", "the world!")); 
  31.     for_each(v.begin(), v.end(), p());  //这里P是对象,而这里应该给的是函数指针,因此调用对象的()运算符重载函数,至关于一个伪函数. 这里的括号不能省略.
  32.     for_each(ivec.begin(), ivec.end(), p()); 
  33.     for_each(ivec.begin(), ivec.end(), print);  //这里的print直接就是函数,使用时不用加括号.
  34.          
  35.     return 0; 
  36.   
编译经过:
运行结构以下:
Hello,  the world!
0 1 2 3 4 5 6 7 8 9
参考:
  1. cplusplus网站:http://www.cplusplus.com/reference/algorithm/for_each/
相关文章
相关标签/搜索