stl算法中for_each的调用函数要注意的问题:
2011-03-08 10:43
666 查看
昨天我得同事TY遇到一个问题,问题抽象如下:
编译的时候提示:
for_each(v.begin(), v.end(), p()); 这一句中类p中运算()不匹配。
而后qq群中的海东青给出了一个版本:
问题解决,编译也通过。但是就是不明白为什么做样做就可以。
后来同时qq群众的tary发来一段文字,现在附录在下:
for_each
template<class InIt, class Fun>
Fun for_each(InIt first, InIt last, Fun f);
The template function evaluates f(*(first + N)) once for each N in the range [0, last - first). It then returns f. The call f(*(first + N)) must not alter *(first + N).
See the related sample program.
刚开始不明白他的意思。后来想想了,才明白作为for_each的函数的形参类型有特别的要求。
总结如下: for_each的形参的函数或者类对应的运算符()的形参的有两点要求:
第一:必须为单参数;
第二:必须跟*iter的类型一致或者兼容。既它的形参不能是迭代器或者指针。只能是类型或者它的引用。
为了考证这一点,我到cpluplus网站去看了for_each的接口定义。
cplusplus网站:http://www.cplusplus.com/reference/algorithm/for_each/
//####################################################################### //# Created Time: 2011-3-7 18:02:35 //# File Name: for_each.cpp //# Description: //####################################################################### #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; typedef std::vector<std::pair<std::string, std::string> > pv; typedef pv::iterator pitr; struct p{ void operator()(const pitr &iter) const{ cout<<iter->first<<"/t"<<iter->second<<endl; } }; int main(){ pv v; v.push_back(make_pair("Hello, ", "the world!")); for_each(v.begin(), v.end(), p()); return 0; }
编译的时候提示:
for_each(v.begin(), v.end(), p()); 这一句中类p中运算()不匹配。
而后qq群中的海东青给出了一个版本:
struct p { void operator()(const pair<string,string>& itr) const{ cout<<itr.first<<"/t"<<itr.second<<endl; } void operator()(int& a){ ++a; } };
问题解决,编译也通过。但是就是不明白为什么做样做就可以。
后来同时qq群众的tary发来一段文字,现在附录在下:
for_each
template<class InIt, class Fun>
Fun for_each(InIt first, InIt last, Fun f);
The template function evaluates f(*(first + N)) once for each N in the range [0, last - first). It then returns f. The call f(*(first + N)) must not alter *(first + N).
See the related sample program.
刚开始不明白他的意思。后来想想了,才明白作为for_each的函数的形参类型有特别的要求。
总结如下: for_each的形参的函数或者类对应的运算符()的形参的有两点要求:
第一:必须为单参数;
第二:必须跟*iter的类型一致或者兼容。既它的形参不能是迭代器或者指针。只能是类型或者它的引用。
为了考证这一点,我到cpluplus网站去看了for_each的接口定义。
template<class InputIterator, class Function> Function for_each(InputIterator first, InputIterator last, Function f) { for ( ; first!=last; ++first ) f(*first); return f; }[/code]
链接:http://www.cplusplus.com/reference/algorithm/for_each/ 1
最后附上一个程序验证这一点:
//####################################################################### //# Created Time: 2011-3-7 18:02:35 //# File Name: for_each.cpp //# Description: //####################################################################### #include <iostream> #include <string> #include <vector> #include <map> #include <algorithm> using namespace std; typedef std::vector<std::pair<std::string, std::string> > pv; typedef pv::iterator pitr; struct p{ void operator()(const pair<string, string> &v) const{ cout<<v.first<<"/t"<<v.second<<endl; } void operator()(int a){ ++a; } }; void print(int n) { cout << n << " "; } int main(){ pv v; vector<int> ivec; for (int i=0; i < 10; i++) ivec.push_back(i); v.push_back(make_pair("Hello, ", "the world!")); for_each(v.begin(), v.end(), p()); for_each(ivec.begin(), ivec.end(), p()); for_each(ivec.begin(), ivec.end(), print); return 0; }[/code]
编译通过:
运行结构如下:
Hello, the world! 0 1 2 3 4 5 6 7 8 9
参考:
cplusplus网站:http://www.cplusplus.com/reference/algorithm/for_each/
鸣谢:
海东青,tary,还有我的同事TY。
相关文章推荐
- STL算法find,find_if,find_if_not,sort,fill,for_each,count,adjacent_find,random_shuffle,prev_permutation
- for-each循环应该注意的问题
- STL fill()填充算法以及for_each()算法 取容器中的最大最小值min_element,max_element
- 注意实现each时dom对象在for过程中被删除时的问题
- STL通用算法: for_each,c++11标准范围for,transform.
- STL运算算法-for_each()
- 【C++ STL学习之六】STL算法之for_each
- STL算法 ------- transform() 与 for_each()比较
- STL算法之for_each
- STL_算法 for_each 和 transform 比较
- 【C++ STL学习之六】STL算法之for_each
- c++ 标准模板库 STL 算法之 for_each 函数的使用用法详解
- STL学习笔记----9.STL算法之 for_each()
- STL与泛型编程<十七>:STL算法简介及for_each()算法
- STL常用遍历算法for_each和transform的比较
- C++ STL 中有关于for_each() 和 transform() 算法
- STL常用遍历算法for_each和transform的比较
- STL中的两个算法for_each与transform
- STL学习之十四:常用算法 for_each 和 transform以及adjacent_find的示例代码
- c++ 标准模板库 STL 算法之 for_each 函数的使用用法详解