Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7520197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:01:34+00:00 2026-05-30T02:01:34+00:00

The following code should store a key and a value. At the end, I

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T02:01:35+00:00Added an answer on May 30, 2026 at 2:01 am

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I thought the following line of code should work fine: $(.1).attr('href', '#Home'); Right? But
I have a variable called $result which should store all the results from my
The following code should find the appropriate project tag and remove it from the
The following code should show a certain track in iTunes: NSString* iTunesPath = [[NSWorkspace
I know that the following code should show and hide a tiny circular progress
In this thread some one commented that the following code should only be used
How to print multiply flowdocumets in a batch? Following code should print different documents
I have the following code which should put programs startable in Bash. if [
The following piece of code should read each line of the file and operate
According to my memory the following piece of code should compile fine on C++

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.