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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:57:57+00:00 2026-05-13T20:57:57+00:00

std::find_if takes a predicate in one of it’s overloaded function. Binders make it possible

  • 0

std::find_if takes a predicate in one of it’s overloaded function. Binders make it possible to write EqualityComparators for user-defined types and use them either for dynamic comparison or static comparison.

In contrast the binary search functions of the standard library take a comparator and a const T& to the value that should be used for comparison. This feels inconsistent to me and could possibly more inefficient as the comparator has to be called with both arguments every time instead of having the constant argument bound to it. While it could be possible to implement std::binary_search in a way to use std::bind this would require all comparators to inherit from std::binary_function. Most code I’ve seen doesn’t do that.

Is there a possible benefit from letting comparators inherit from std::binary_function when using it with algorithms that take a const T& as a value instead of letting me use the binders? Is there a reason for not providing predicate overloads in those functions?

  • 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-13T20:57:58+00:00Added an answer on May 13, 2026 at 8:57 pm

    A single-argument predicate version of std::binary_search wouldn’t be able to complete in O(log n) time.

    Consider the old game “guess the letter I’m thinking of”. You could ask: “Is it A?” “Is it B?”.. and so on until you reached the letter. That’s a linear, or O(n), algorithm. But smarter would be to ask “Is it before M?” “Is it before G?” “Is it before I?” and so on until you get to the letter in question. That’s a logarithmic, or O(log n), algorithm.

    This is what std::binary_search does, and to do this in needs to be able to distinguish three conditions:

    • Candidate C is the searched-for item X
    • Candidate C is greater than X
    • Candidate C is less than X

    A one-argument predicate P(x) says only “x has property P” or “x doesn’t have property P”. You can’t get three results from this boolean function.

    A comparator (say, <) lets you get three results by calculating C < X and also X < C. Then you have three possibilities:

    • !(C < X) && !(X < C) C is equal to X
    • C < X && !(X < C) C is less than X
    • !(C < X) && X < C C is greater than X

    Note that both X and C get bound to both parameters of < at different times, which is why you can’t just bind X to one argument of < and use that.

    Edit: thanks to jpalecek for reminding me binary_search uses <, not <=.
    Edit edit: thanks to Rob Kennedy for clarification.

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

Sidebar

Related Questions

I have one std::list<> container and these threads: One writer thread which adds elements
std::auto_ptr is broken in VC++ 8 (which is what we use at work). My
std::next_permutation (and std::prev_permutation) permute all values in the range [first, last) given for a
For std::map, how will insert behave if it has to resize the container and
Is std::string size() a O(1) operation? The implementation of STL I'm using is the
If I have a std::vector or std::map variable, and I want to see the
What is std::pair for, why would I use it, and what benefits does boost::compressed_pair
For an std::map<std::string, std::string> variables , I'd like to do this: BOOST_CHECK_EQUAL(variables[a], b); The
I have several std::vector , all of the same length. I want to sort
I have a std::multimap where key is a custom class. Something like this: Class

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.