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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:32:25+00:00 2026-05-15T08:32:25+00:00

We are having a bit of trouble using find_if to search a vector of

  • 0

We are having a bit of trouble using find_if to search a vector of pairs for an entry in which the first element of the pair matches a particular value. To make this work, we have defined a trivial functor whose operator() takes a pair as input and compares the first entry against a string.

Unfortunately, when we actually add a call to find_if using an instance of our functor constructed using a temporary string value, the compiler produces a raft of error messages. Oddly (to me, anyway), if we replace the temporary with a string that we’ve created on the stack, things seem to work.

Here’s what the code (including both versions) looks like:

typedef std::pair<std::string, std::string> MyPair;
typedef std::vector<MyPair> MyVector;

struct MyFunctor: std::unary_function <const MyPair&, bool>
{
  explicit MyFunctor(const std::string& val)
    : m_val(val) {}

  bool operator() (const MyPair& p)
  {
    return p.first == m_val;
  }

  const std::string m_val;
};

bool f(const char* s)
{
  MyFunctor f(std::string(s));  // ERROR
  // std::string str(s);                                                                                                  
  // MyFunctor f(str);              // OK                                                                                                    
  MyVector vec;
  MyVector::const_iterator i = std::find_if(vec.begin(), vec.end(), f);
  return i != vec.end();
}

And here’s what the most interesting error message looks like:

/usr/include/c++/4.2.1/bits/stl_algo.h:260: error: conversion from ‘std::pair, std::allocator >, std::basic_string, std::allocator > >’ to non-scalar type ‘std::string’ requested

Because we have a workaround, we’re mostly curious as to why the first form causes problems. I’m sure we’re missing something, but we haven’t been able to figure out what it is.

  • 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-15T08:32:25+00:00Added an answer on May 15, 2026 at 8:32 am

    This is the most vexing parse.

    You can do:

    MyFunctor f(s);
    

    or

    MyFunctor f((std::string(s)));
    

    The original declares a function f. f takes a single argument, a pointer to a function taking an argument s and returning std::string.

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

Sidebar

Related Questions

Having a bit of trouble using the List.Find with a custom predicate i have
I'm having a bit of trouble with jQuery Deferred. I'm using a jQuery ajax
OK so i'm having a bit of trouble with .htaccess. I'm using Codeigniter and
I am having a bit of trouble using strtok with strcmp. //Handles the header
I am having a wee bit of trouble calling a variable in phpmyadmin using
I'm having a bit of trouble using variable generated php div classes and jQuery
I'm having a little bit of trouble using CSS's :nth-child(odd) selector. It seems to
I seem to be having a bit of trouble with using classes from other
Having a little bit of trouble figuring out using a ternary with Razor view
I'm having a bit of trouble changing the image source, using jQuery, in my

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.