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

  • Home
  • SEARCH
  • 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 8023399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:33:14+00:00 2026-06-04T22:33:14+00:00

I have a container of pointers to objects. The pointers are a base class,

  • 0

I have a container of pointers to objects. The pointers are a base class, and the hierarchy implements a virtual function count(). I want to calculate a sum of count() in the container.

I currently do this with for_each and a lambda function:

size_t sum = 0;
std::for_each(ptrs_.begin(), ptrs_.end(), [&sum](ptr const *p) {
    expr += p->count();
});
return sum;

Can anyone help me reimplement this with boost::bind and std::accumulate or other std algorithms?

  • 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-06-04T22:33:14+00:00Added an answer on June 4, 2026 at 10:33 pm
    auto getcount = std::mem_fun(&Base::count); // nothing to bind, we just need a functor
    
    size_t sum = std::accumulate(
        boost::make_transform_iterator(ptrs_.begin(), getcount),
        boost::make_transform_iterator(ptrs_.end(), getcount),
        (size_t)0
    );
    

    If you don’t like auto, or more likely if your compiler doesn’t, then of course you can paste the thing twice, or go looking for the return type of mem_fun, or capture it using a function template:

    template <typename IT, typename FUNC, typename T>
    T transform_accumulate(IT first, IT last, T init, FUNC func) {
        return std::accumulate(
            boost::make_transform_iterator(first, func),
            boost::make_transform_iterator(last, func),
            init
        );
    }
    

    Then call it as:

    transform_accumulate(ptrs_.begin(), ptrs_.end(), size_t(), std::mem_fun(&Base::count));
    

    Alternately, use the form of std::accumulate that takes a binary functor:

    struct AddCount {
        size_t operator()(size_t result, Base *p) const {
            return result + p->count();
        }
    };
    
    size_t sum = std::accumulate(ptrs_.begin(), ptrs_.end(), size_t(), AddCount());
    

    Instead of writing AddCount, you could of course use a lambda expression. I expect you can construct it using the stuff in <functional> too, but I’m not going to.

    I haven’t tested any of this code, so let the error-spotting begin!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a container of smart pointers to mutable objects. I have to write
I have an Entity class, which contains 3 pointers: m_rigidBody, m_entity, and m_parent. Somewhere
I have a class TContainer that is an aggregate of several stl collections pointers
I have a container class which uses boost::optional to hold the value. Here is
I have a STL container full of billions of the following objects pair<SomeClass*, SomeClass*>
I have an STL map that contains shared pointers to objects that are manipulated
Bumped into another templates problem: The problem: I want to partially specialize a container-class
I am attempting to store objects derived from a templated base class in an
All objects in my program inherit from a Container class. The Container class has
Is it possible to store a bunch of objects by their base class 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.