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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:24:55+00:00 2026-05-18T02:24:55+00:00

Please have a look at this example posted by Johannes Schaub to sort a

  • 0

Please have a look at this example posted by Johannes Schaub to sort a vector of pairs:

How do I sort a vector of pairs based on the second element of the pair?

std::sort(a.begin(), a.end(), 
          boost::bind(&std::pair<int, int>::second, _1) <
          boost::bind(&std::pair<int, int>::second, _2));

I thought I do understand boost::bind, but I have trouble with this one.

Question 1:

the sort algorithm is expecting a predicate function as a third parameter. What I see here, is a boolean expression. What am I missing?:

boost::bind(&std::pair<int, int>::second, _1) < boost::bind(&std::pair<int, int>::second, _2)

Does the boost::bind library overload operator< for those two binds, and is returning some kind of function pointer (like a lambda)?

Question 2:
This gets me confused:

boost::bind(&std::pair<int, int>::second, _1)

Usually there is some kind of function pointer as first parameter of a bind call, but here it is an address of a class member? What is the result of that particular bind?

Thanks for your time & help

  • 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-18T02:24:55+00:00Added an answer on May 18, 2026 at 2:24 am

    boost::bind overloads the operator ! and the relational and logical operators ==, !=, <, <=, >, >=, &&, ||, so this is why you “see” a boolean expression, but you’re really getting back a function predicate.

    From there you can see that you’re binding the second member of the pair for the 1st and 2nd arguments of the overloaded less than function.

    As for your second question:
    Boost bind will recognize when you have passed a pointer to a member and treat it as if you called

    bind<R>(mem_fun(&std::pair<int,int>::second), args);
    

    This is how the documentation describes this:

    Using bind with pointers to members

    Pointers to member functions and
    pointers to data members are not
    function objects, because they do not
    support operator(). For convenience,
    bind accepts member pointers as its
    first argument, and the behavior is as
    if boost::mem_fn has been used to
    convert the member pointer into a
    function object. In other words, the
    expression

    bind(&X::f, args)

    is equivalent to

    bind(mem_fn(&X::f), args)

    where R is the return type of X::f
    (for member functions) or the type of
    the member (for data members.)

    You can find this and more information here.

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

Sidebar

Related Questions

I have a problem with internet explorer 6 and 7, please look this example
Please have a look at this form that I am trying to design with
Please have a look at this page www.pixeli.ca/issue. I have begun making a page
Please have a look at this demo page that I cooked up a while
I have a bunch of php arrays that look like this (please don't ask
Please, take a look at this example: import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Test
Please take a look at this example. http://jsfiddle.net/LdeWK/2/ I want to know how to
Please look at this example and show me how to make the blue <div>
Please have a look at the following example, the first call to getMethod() produces
I have a question about the volatile statement in Java. Please look at this

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.