【发布时间】:2011-03-12 22:37:17
【问题描述】:
为什么对仿函数的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;
}
【问题讨论】: