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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:54:10+00:00 2026-05-11T17:54:10+00:00

I want to find the first item in a sorted vector that has a

  • 0

I want to find the first item in a sorted vector that has a field less than some value x.
I need to supply a compare function that compares ‘x’ with the internal value in MyClass but I can’t work out the function declaration.
Can’t I simply overload ‘<‘ but how do I do this when the args are ‘&MyClass’ and ‘float’ ?

 float x;
 std::vector< MyClass >::iterator last = std::upper_bound(myClass.begin(),myClass.end(),x);
  • 1 1 Answer
  • 1 View
  • 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-11T17:54:10+00:00Added an answer on May 11, 2026 at 5:54 pm

    What function did you pass to the sort algorithm? You should be able to use the same one for upper_bound and lower_bound.

    The easiest way to make the comparison work is to create a dummy object with the key field set to your search value. Then the comparison will always be between like objects.

    Edit: If for some reason you can’t obtain a dummy object with the proper comparison value, then you can create a comparison functor. The functor can provide three overloads for operator() :

    struct MyClassLessThan
    {
        bool operator() (const MyClass & left, const MyClass & right)
        {
            return left.key < right.key;
        }
        bool operator() (const MyClass & left, float right)
        {
            return left.key < right;
        }
        bool operator() (float left, const MyClass & right)
        {
            return left < right.key;
        }
    };
    

    As you can see, that’s the long way to go about it.

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

Sidebar

Related Questions

I want to find the first element that has appeared in previous positions in
Suppose I want to find the longest subsequence such that first half of subsequence
I want to find a struct in a vector, but I'm having some troubles.
I want to find first 4 words and last 4 words after the particular
I want to find (if they exist) any hash tags and get the first
i want to find factorial a number which i take memory first.(intel 8085) edit:
At first I want to say: I couldn't find a support forum for this
First of all, I need to Find a parent node by a specific child
As in, if I have an XML Document <root a=value> <item name=first> x <foo
I want to find the index of the n'th occurrence of an item in

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.