Peter
Peter

Reputation: 2320

function_object lost value of data member after for_each

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.

Upvotes: 2

Views: 172

Answers (1)

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272467

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);

Upvotes: 3

Related Questions