- 论坛徽章:
- 0
|
原帖由 cuicp 于 2006-10-17 08:44 发表
非常感谢楼上的解答!
楼上的意思是不是,find_if函数的第三个参数必须重载()操作符?
另外,仿函数的定义是什么?也就是什么样的形式算是仿函数呢?
我在网上找了半天,可是还是不太明白,是不是模板从载了()操作符就 ...
第三个参数不一定非要重载(),比如:
- [b]#include <iostream>
- #include <boost/tokenizer.hpp>
- #include <string>
- #include <fstream>
- #include <list>
- using namespace std;
- void print(string str)
- {
- cout<<str<<endl;
- }
- int main()
- {
- ifstream in("12345",ios::in);
- string str;
- int i = 0;
- typedef boost::tokenizer<boost::char_separator<char> > Tok;
- boost::char_separator<char>sep("'\t'");
- list<string>mylist;
- while(getline(in,str,'\n'))
- {
- mylist.push_back(str);
- i++;
- }
- Tok tok(*(++(++mylist.begin())),sep);
- cout<<i<<endl;
- //cout<<*tok.begin()<<endl;
- //for(Tok::iterator iter = tok.begin();iter != tok.end();++iter)
- //{
- //cout<<*iter<<endl;
- //}
- size_t diff = distance(tok.begin(),tok.end());
- cout<<diff<<endl;
- for_each(tok.begin(),tok.end(),print);
- }[/b]
复制代码 早上写得一个很烂得测试代码。 你看for_each第三个参数。他是一个函数,没有重载(),
但 “仿函数”必须要重载()了。
找本STL得书看看 上面说得比较详细。
[ 本帖最后由 okmmno1 于 2006-10-17 13:18 编辑 ] |
|