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

  • Home
  • SEARCH
  • 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 3975736
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:43:15+00:00 2026-05-20T04:43:15+00:00

I have an array of structures, sorted on a member of a structure, something

  • 0

I have an array of structures, sorted on a member of a structure, something like:

struct foo
{
    int bar;
    double baz;
};

// An array of foo, sorted on .bar
foo foos[] = { ........ };
// foos[0] = {0, 0.245}
// foos[1] = {1, -943.2}
// foos[2] = {2, 304.222}
// etc...

I want to find the element with a specific .bar value. It might or might not be in the array, and I’d like to do it in O(log(n)) time, since the array is sorted.

std::lower_bound is what I’d normally go for, but I need to specify a comparison function. However, the type of the members of the array (struct foo) and the searched value (int) are not the same, thus, my comparator is:

bool comp(foo a, int b)
{
    // ...
}
// --- or ---
bool comp(int a, foo b)
{
    // ...
}

It looks like the first will work with gcc, but I was wondering if the order of the arguments to the comparison function was specified by the standard, or if I’m relying on compiler behavior.

I’d like to avoid constructing a foo to pass to std::lower_bound here, as a full foo isn’t required, and could be costly. My other option would be to wrap the foo * in a custom iterator that only exposed the .bar member.

  • 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-20T04:43:15+00:00Added an answer on May 20, 2026 at 4:43 am

    From the standard, 25.3.3.1/3, on std::lower_bound():

    Returns: The furthermost iterator i in the range [first, last] such
    that for any iterator j in the range
    [first, i) the following
    corresponding conditions hold: *j <
    value
    or comp(*j, value) != false.

    From that, you may use

    bool comp(foo a, int b)
    

    or you may compare two foo instances and then access bar in both of them.

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

Sidebar

Related Questions

I have the following array structure (linked list): struct str_pair { char ip [50]
I have a structure that contains an arrays of another structure, it looks something
I have an array of a few million numbers. double* const data = new
I have an array of integers: int[] number = new int[] { 2,3,6,7 };
i have an array like this Array ( [0] => Array ( [cat_name] =>
I have an array of a large structure that I am trying to output
I have an array(sorted on an attribute called node_id) of ruby objects which basically
I have a data structure like the following @colors = qw(red blond green); @numbers
I have an array in Perl: my @my_array = (one,two,three,two,three); How do I remove
I have an array of numbers that potentially have up to 8 decimal places

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.