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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:45:11+00:00 2026-06-14T05:45:11+00:00

I have a struct that looks like this, struct Foo { int a; };

  • 0

I have a struct that looks like this,

struct Foo {
    int a;
};

I have a vector of these structs that look like this,

vector<Foo> foos;

All of the Foos are sorted by the integer a in ascending order using the STL sort() function. Now I want to get the Foo object that has the member field a less than or equal to a given number, like the STL lower_bound() function. The problem is that the STL lower_bound function declaration looks like this:

template <class ForwardIterator, class T, class Compare>
  ForwardIterator lower_bound ( ForwardIterator first, ForwardIterator last,
                                const T& value, Compare comp );

So while I want to do something like,

lower_bound(foos.begin(), foos.end(), 5, custom_comp);

I can’t because the int I’m looking for (5 in this case) is not of the type Foo. I’m having this issue with lower_bound(), upper_bound(), and binary_search(). custom_comp only defines the ordering and doesn’t define that an object with a = 5 actually equals the int 5.

Is there any elegant way of doing this with STL?

Edit:

I realized my example doesn’t completely represent my problem. What I actually have is that Foo contains two ints, a and b. When I call lower_bound, I don’t have access to b (because I don’t care about it). Now the issue with billz answer is that I’d have to define a constructor that takes only a as a parameter, which isn’t very elegant in my opinion (because b is left undefined or abitrary, and this constructor can be used anywhere in the code). But if this is the only option, I’ll take it.

  • 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-06-14T05:45:13+00:00Added an answer on June 14, 2026 at 5:45 am

    You could provide a constructor to your struct Foo

    struct Foo {
      Foo(int x):a(x){
      }
        int a;
    };
    

    you can now call:

    std::lower_bound(foos.begin(), foos.end(), 5, custom_comp);
    

    or

    std::lower_bound(foos.begin(), foos.end(), Foo(5), custom_comp);
    

    or

    Foo f(5);
    std::lower_bound(foos.begin(), foos.end(), f, custom_comp);
    

    The suggested way is:

    struct Foo {
      explicit Foo(int x):a(x){
      }
        int a;
    };
    
    std::lower_bound(foos.begin(), foos.end(), Foo(5), custom_comp);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a struct that looks like this (a POD): struct Foo {
I have a C++ struct that looks like this: struct unmanagedstruct { int flags;
I have a struct that looks like this: struct queue_item_t { int id; int
I have a struct that looks like this: typedef struct _my_struct { float first_vector[SOME_NUM][OTHER_NUM];
I have a struct that looks something like this: [StructLayout(LayoutKind.Sequential)] public struct in_addr {
I have a code that looks something like: struct Data { int value; };
I have a C structure that looks like this typedef struct event_queue{ Event* event;
I have a struct that looks something like: struct foo_t { template <std::size_t x,
I have an unmanaged struct I'd like to marshal to c sharp that looks
I have a struct that I initialize like this: typedef struct { word w;

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.