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 6220987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:03:23+00:00 2026-05-24T08:03:23+00:00

For a map like std::map, how do I accumulate it’s values’ sum? Actually, I

  • 0

For a map like std::map, how do I accumulate it’s values’ sum?
Actually, I made it with a functor and std::for_each algorithm. But I’d also like to make this using std::accumulate algorithm.
I have no idea how to apply it to std::map.
Is this even possible?

struct Accumurator
    : std::unary_function<std::pair<int, int>, void>
{
    Accumurator()
        : totalValue_(0)
    {
    } 

    void operator()(const std::pair<int, int>& p)
    {
        totalValue_ += p.second;
    }

    int result() const
    {
        return totalValue_;
    }

    int totalValue_; 
};

int _tmain(int argc, _TCHAR* argv[])
{
    std::map<int, int> m;
    m.insert(make_pair(1, 10));
    m.insert(make_pair(2, 10));
    m.insert(make_pair(3, 10));
    m.insert(make_pair(4, 10));
    m.insert(make_pair(5, 10));
    m.insert(make_pair(6, 10));

    int totalSum = std::for_each(m.begin(), m.end(), Accumurator()).result();

    // How can I apply accumulate algorithm for associative containers.
    // int totalSum = accumulate(m.begin(), m.end(), ???);

    return 0;
}
  • 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-24T08:03:23+00:00Added an answer on May 24, 2026 at 8:03 am

    Almost. The functor must be a binary operator taking the return value type as the first and the range type as the second argument:

    x = Functor(init, *it++);
    x = Functor(x, *it++);
    x = Functor(x, *it++);
    // ... until it == end
    

    So you don’t need a stateful functor at all, a simple function will do:

    int map_acc(int lhs, const std::pair<int, int> & rhs)
    {
      return lhs + rhs.second;
    }
    
    const int sum = std::accumulate(m.begin(), m.end(), 0, map_acc);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking for a container which provides std::map like interface but maintains the
I want something like an std::map , but I only want to see if
I have a map defined like this std::map<some_key_type, std::string::iterator> mIteratorMap; And a huge string
I'd like to create a std::map that contains a std::vector of iterators into itself,
For an std::map<std::string, std::string> variables , I'd like to do this: BOOST_CHECK_EQUAL(variables[a], b); The
Supposing to have something like this: #include <map> int main(){ std::map<int,int> m; m[1] =
I have a class like this: class MyClass{ public: MyClass(int Mode); private: std::map <
I'm looking for a STL container that works like std::multimap, but has constant access
I'm a little bit scared about something like this: std::map<DWORD, DWORD> tmap; tmap[0]+=1; tmap[0]+=1;
In my header file, I'm declaring a map like so: std::map<LPD3DXSPRITE, LPDIRECT3DTEXTURE9> sprites; In

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.