function_object 在 for_each 之后丢失了数据成员的值
以下代码应存储一个键和一个值。最后,我想要所有值的总和。但是我的函数对象sum中的变量*sum_all*每次都包含“0”。到底是怎么回事?
// map
#include <iostream>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
struct Item {
int count;
double value;
};
class Sum {
public:
Sum() {
sum_all = 0.0;
}
// keys are stored as const in a map
void operator()(pair<const string, Item>& pair) {
cout << pair.first << "\n";
cout << "Sum: " << pair.second.value << "\n";
cout << "Middle: " << pair.second.value/pair.second.count << "\n";
sum_all += pair.second.value;
}
double get_sum_all() {
return sum_all;
}
private:
double sum_all;
};
int main() {
map<string, Item> table;
for (int i = 0; i < 3; i++) {
string key;
double value;
cin >> key;
cin >> value;
// new item
if (table.find(key) == table.end()) {
Item item;
item.count = 1;
item.value = value;
table[key] = item;
} else {
Item& item = table[key];
item.count++;
item.value += value;
}
}
Sum sum;
for_each(table.begin(), table.end(), sum);
cout << "table.size() " << table.size() << "\n";
cout << "sum.get_sum_all() " << sum.get_sum_all() << "\n";
cout << "sum.get_sum_all()/table.size()" << sum.get_sum_all()/table.size() << "\n";
return 0;
}
输入/输出示例:
[peter@donut chap_6]$ ./u3_map
foo 1
bar 2
foo 1
bar
Sum: 2
Middle: 2
foo
Sum: 2
Middle: 1
table.size() 2
sum.get_sum_all() 0
sum.get_sum_all()/table.size()0
调试显示,sum_all 已像在 for_each 期间那样进行了修改。但我无法设置观察点,或者 gdb 会忽略观察点。我以为构造函数被调用了两次,但这似乎没有发生。我做错了什么? 谢谢。
The following code should store a key and a value. At the end, I want a total sum of all values. But the variable *sum_all* in the my function object sum contains every time "0". What is going on?
// map
#include <iostream>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
struct Item {
int count;
double value;
};
class Sum {
public:
Sum() {
sum_all = 0.0;
}
// keys are stored as const in a map
void operator()(pair<const string, Item>& pair) {
cout << pair.first << "\n";
cout << "Sum: " << pair.second.value << "\n";
cout << "Middle: " << pair.second.value/pair.second.count << "\n";
sum_all += pair.second.value;
}
double get_sum_all() {
return sum_all;
}
private:
double sum_all;
};
int main() {
map<string, Item> table;
for (int i = 0; i < 3; i++) {
string key;
double value;
cin >> key;
cin >> value;
// new item
if (table.find(key) == table.end()) {
Item item;
item.count = 1;
item.value = value;
table[key] = item;
} else {
Item& item = table[key];
item.count++;
item.value += value;
}
}
Sum sum;
for_each(table.begin(), table.end(), sum);
cout << "table.size() " << table.size() << "\n";
cout << "sum.get_sum_all() " << sum.get_sum_all() << "\n";
cout << "sum.get_sum_all()/table.size()" << sum.get_sum_all()/table.size() << "\n";
return 0;
}
Example input/output:
[peter@donut chap_6]$ ./u3_map
foo 1
bar 2
foo 1
bar
Sum: 2
Middle: 2
foo
Sum: 2
Middle: 1
table.size() 2
sum.get_sum_all() 0
sum.get_sum_all()/table.size()0
Debugging shows, that sum_all is modified like it should during for_each. But I'm not able to set a watchpoint, or gdb ignores the watchpoint. I thought the constructor is called twice, but this seem not to happen. What I'm doing wrong?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
std::for_each
通过值获取其函子参数,而不是通过引用。原版没有修改。你需要做这样的事情:std::for_each
takes its functor argument by value, not by reference. The original is not modified. You need to do something like this: