在 for_each 上使用函子
为什么函子上的 for_each
调用最后没有更新 sum::total
?
struct sum
{
sum():total(0){};
int total;
void operator()(int element)
{
total+=element;
}
};
int main()
{
sum s;
int arr[] = {0, 1, 2, 3, 4, 5};
std::for_each(arr, arr+6, s);
cout << s.total << endl; // prints total = 0;
}
Why does the for_each
call on functor doesn't update sum::total
at the end?
struct sum
{
sum():total(0){};
int total;
void operator()(int element)
{
total+=element;
}
};
int main()
{
sum s;
int arr[] = {0, 1, 2, 3, 4, 5};
std::for_each(arr, arr+6, s);
cout << s.total << endl; // prints total = 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
for_each
按值获取函子 - 因此它被复制。例如,您可以使用一个用指向外部 int 的指针初始化的函子。或者您可以使用
for_each
的返回值for_each
takes the functor by value - so it is copied. You can e.g. use a functor which is initialized with a pointer to an external int.Or you can use the return value from
for_each
for_each
按值接收函子的副本。即使在那之后,它也可以自由复制它,但会返回一个副本。OTOH,您只是想重新发明
std::accumulate
,这会更轻松地完成这项工作:for_each
receives a copy of your functor by value. Even after that, it's free to copy it, but does return a copy.OTOH, you're simply trying to re-invent
std::accumulate
, which will do the job much more easily:因为您传递给
for_each
的s
是按值传递的。for_each
按值接受它!在 C++0x 中,您可以使用
for_each
解决此问题,输出:
IDEONE 上的演示:http ://ideone.com/s7OOn
或者您可以简单地在
std::cout
本身中编写:Run : http://ideone.com/7Hyla
请注意,这种不同的语法对于学习目的是可以的,例如
std::for_each
如何工作以及它返回什么,但是我不会在实际代码中推荐这种语法。 :-)在 C++ 中,您可以在仿函数中编写用户定义的转换函数,
它与 Erik 第二个解决方案的版本略有不同: http ://ideone.com/vKnmA
Because the
s
which you pass to thefor_each
is by value.for_each
accepts it by value!In C++0x, you can solve this problem with
for_each
as,Output:
Demo at ideone : http://ideone.com/s7OOn
Or you can simple write in the
std::cout
itself:Run : http://ideone.com/7Hyla
Note such different syntax is okay for learning purpose, as to how
std::for_each
works, and what it returns, but I would not recommend this syntax in real code. :-)In C++, you can write user-defined conversion function in the functor as,
It's slightly different version from Erik second solution : http://ideone.com/vKnmA
发生这种情况是因为 std::for_each 要求函子按值传递。
针对您的解决方案的解决方法:
This happens due to std::for_each requires the functor to be passed by value .
A workaround for your solution:
如果您希望对象状态在 std::for_each 之后更新,
std::ref()
也是另一个选择std::ref()
is also another option if you want your object state gets updated after std::for_each