function_object 在 for_each 之后丢失了数据成员的值

发布于 2025-01-06 22:10:08 字数 1943 浏览 0 评论 0原文

以下代码应存储一个键和一个值。最后,我想要所有值的总和。但是我的函数对象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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹤仙姿 2025-01-13 22:10:08

std::for_each 通过值获取其函子参数,而不是通过引用。原版没有修改。你需要做这样的事情:

sum = for_each(table.begin(), table.end(), sum);

std::for_each takes its functor argument by value, not by reference. The original is not modified. You need to do something like this:

sum = for_each(table.begin(), table.end(), sum);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文