for_each 和函数对象问题!

yshuise 2006-10-10 05:37:22
for_each怎么能每个对象都能调用啊!
比如:
template<class iter, class fct)
fct for_each(iter b, iter c, fct f){
while (b != e) f(*b++);
return f;
}
f(*b++)是传递对象么?是怎么调用的啊?怎么传递对象的!?
...全文
185 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
sinall 2006-10-10
  • 打赏
  • 举报
回复
f(*b++)是传递对象么?是怎么调用的啊?怎么传递对象的!?
————————————————————————————————————————
所以,你最好看看一般情况下f函数的原型!!!
就我所知,f函数的参数肯定是对象引用(就是*iter对应的类型)。
例如:
void f(CObject& obj)
{
//
}
或者是一个重载了operator()的类(函数对象)
struct f
{
void operator()(CObject& obj) const
{
//
}
};

*iter会得到一个CObject对象吧。这个毫无疑问吧?
然后,传递该对象的引用给f。
……
chenhu_doc 2006-10-10
  • 打赏
  • 举报
回复
c++标准程序库中:
The following is a complete example. This is the function object version of a previous example (see page 119) that did the same with an ordinary function:


// stl/foreach2.cpp

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

//simple function object that prints the passed argument
class PrintInt {
public:
void operator() (int elem) const {
cout << elem << ' ';
}
};

int main()
{
vector<int> coll;
//insert elements from 1 to 9
for (int i=1; i<=9; ++i) {
coll.push_back(i);
}

//print all elements
for_each (coll.begin(), coll.end(), //range
PrintInt()); //operation
cout << endl;
}


The class PrintInt defines objects for which you can call operator () with an int argument. The expression


PrintInt()


in the statement


for_each (coll.begin(), coll.end(),
PrintInt());


creates a temporary object of this class, which is passed to the for_each() algorithm as an argument. The for_each() algorithm is written like this:


namespace std {
template <class Iterator, class Operation>
Operation for_each (Iterator act, Iterator end, Operation op)
{
while (act != end) { //as long as not reached the end
op (*act); // - call op() for actual element
act++; // - move iterator to the next element
}
return op; }
}
}


for_each() uses the temporary function object op to call op(*act) for each element act. If the third parameter is an ordinary function, it simply calls it with *act as an argument. If the third parameter is a function object, it calls operator () for the function object op with *act as an argument. Thus, in this example program for_each() calls:


PrintInt::operator()(*act)


You may be wondering what all this is good for. You might even think that function objects look strange, nasty, or nonsensical. It is true that they do complicate code. However, function objects are more than functions, and they have some advantages:
chenhu_doc 2006-10-10
  • 打赏
  • 举报
回复
关于functin object, 参考c++ primer。
chenhu_doc 2006-10-10
  • 打赏
  • 举报
回复
for_each怎么能每个对象都能调用啊! //模板机制, 传递的是迭带器,模板可以自动的推断出类型出来。 然后用得出的类型替换在模板参数类型。

template<class iter, class fct)
fct for_each(iter b, iter e, fct f){
while (b != e) f(*b++);
return f;
}
f(*b++)是传递对象么?是怎么调用的啊?怎么传递对象的!?

f是一个function object, 传递的是对象,不过用引用的形式接受他,(传引用的)

64,647

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧