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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:42:42+00:00 2026-05-30T20:42:42+00:00

I have made a small example in order to understand how boost::bind () works

  • 0

I have made a small example in order to understand how boost::bind () works with collections. I have a class called Data:

class Data
{
public:
    void print ();
    const std::string& get () const;
    std::string& get () ;
};

I have created a std::vector of Data objects called samples and I am able to use bind in the same way as std::mem_fun_ref works.

std::for_each (samples.begin (),samples.end (),std::mem_fun_ref (&Data::print));
std::for_each (samples.begin (),samples.end (),boost::bind (&Data::print,_1));

The basic idea is that the bind returns a function object of type bind_t<RetType=void, ObjType=Data, ArgType=void>. The member function as the first parameter allows the compiler to deduce RetType, ObjType and ArgType. The placeholder _1 corresponds to the data object which must be provided by the algorithm.

Then std::for_each calls the function object “for each” element in the following way:

for ( ; first!=last; ++first ) f(*first);

bind_t::operator(ObjType& obj) is invoked and its definition should be something like this:

return (obj.*_method ());

I have crated a class called Filter that performs some processing over a data element.

class Filter
{
    void filter (Data& data);
    ...
};

If I want to apply the filter over the data elements in the vector I call bind in the following way:

std::for_each (samples.begin (),samples.end (),boost::bind (&Filter::filter,filter,_1));

for_each passes a Data object to bind_t::operator(). In this case the function object already has the object and just need the parameter so in this case placeholder _1 refers to the argument.

Here comes my question:

How can use bind if I have to iterate over a std::map rather than a vector?

(Sorry for all the explanation, I just want to make sure that I understand the way in which bind works)

  • 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-30T20:42:43+00:00Added an answer on May 30, 2026 at 8:42 pm
    #include <boost/bind.hpp>
    #include <algorithm>
    int main()
    {
      struct Sample
      {
        int i_;
        double d_;
      };
      typedef std::map<int, Sample> Samples;
      struct Filter
      {
        void filter(const Sample &s)
        {
        }
      };
      Filter filter;
      Samples samples;
      std::for_each(samples.begin(), samples.end(), boost::bind(&Filter::filter, filter, boost::bind(&Samples::value_type::second, _1))); 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've made a small example with a Person class, which contains a list of
I have made a small log service that i want to publish to a
I have made a small application in Java and I would like to make
Post-release, I have made one small change to one form in our development site
I have just started programming and have made a few small applications in C
I have made an update on Google App Engine with a small fix and
I made a small example HTML page to get JQuery's getJSON method working. It
I have developed a small server that recives connections (using basically the example here:
How do I distribute a small amount of data in a random order in
I have made a small function which should print some InfoPath files. It is

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.