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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:35:40+00:00 2026-05-12T11:35:40+00:00

This may seem to be an academic question, but still I would be very

  • 0

This may seem to be an academic question, but still I would be very interested in the answer:

I have a vector of strings s in which I would like to find a given string findme. This can be done using something like

find(s.begin(), s.end(), findme);

My question is: There must be a way doing the same using find_if and the compare method of the STL strings as predicate, but how? Something like

find_if(s.begin(), s.end(), bind2nd(mem_fun_ref(&string::compare), string("findme")) );

does not work, because the compare method has several overloads and the compiler does not know which one to choose.

As a second step: My motivation for using find_if instead of find is that I have a vector of objects derived from a class having a string property name and I want to find an object with a given name. Is this possible (without writing an extra function to be used as predicate)?


EDIT: As some (most 🙂 answers mentioned using Boost — I would prefer not to have to include Boost for this. (As far as I know, most of the Boost libraries are “only” templates, so there should be a way without using Boost.)

  • 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-12T11:35:40+00:00Added an answer on May 12, 2026 at 11:35 am

    One option is to cast the member function pointer to suitable type. Another thing you are forgetting is that std::string::compare returns 0 for equal strings, so you’ll also need to negate the functor. All in all:

    std::find_if(
        vec.begin(), vec.end(),
        std::not1(
            std::bind2nd(
                std::mem_fun_ref(static_cast<int (std::string::*)(const char*)const>(&std::string::compare)),
                "findme"
            )
        )
    );
    

    As to your rationale against boost: its templates are an order of a magnitude more flexible than what you can find in STL functional header. It’s either boost, you wait for C++0x lambdas (which I believe will be the preferable way in such situations) or you write some helpers yourself. Currently it can’t get simpler than:

    std::find_if(vec.begin(), vec.end(), boost::bind(&X::name, _1) == "findme");
    

    FYI, C++0x will add std::bind which is similar to boost::bind but it seems the convenience of overloaded operator== will not be there.

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

Sidebar

Related Questions

This may seem like a very simple question, but I have been struggling with
this may seem like a stupid question but I have a query string which
This may seem like a simple question, but I can't find the answer anywhere
this may seem like a very simple question, but I want to dump some
This may seem like a dumb question but I have IDLE and I'm trying
This may seem like a simple question, but I can't find the definite answer:
This may seem a rather silly question, but I have a header file for
This may seem like a very basic question, but its been in my head
This may seem like a very basic question but I did not seem to
This may seem like a stupid Question but i have two cubes being rendered

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.