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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:12:12+00:00 2026-05-31T18:12:12+00:00

I have a list of tuple, the list was sorted based on the first

  • 0

I have a list of tuple, the list was sorted based on the first element of the tuple but the second and last elements are in random order. Now I want to find all tuples with the first element within a range, i.e. return all tuples for (tuple.first>-X and tuple.first<X). Among all these returning tuples, I need to find the maximum and minimum value in the second element of the tuples. How can a STL algorithm implement this?

  • 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-31T18:12:13+00:00Added an answer on May 31, 2026 at 6:12 pm

    Since it is already sorted, you can use equal_range to get a pair of iterators that delimit the range of “interesting” tuples:

    It const begin = std::lower_bound(list.begin(), list.end(),
                                      [X](Tuple const& t) {
                                          return t.first > -X;
                                      });
    
    It const end = std::upper_bound(begin, list.end(),
                                    [X](Tuple const& t) {
                                        return t.first < X;
                                    });
    
    std::pair<It,It> const range = std::make_range(begin, end);
    

    Then, you can simply iterate over this range, and register the minimum and maximum values that you see:

    int min = INT_MAX, max = INT_MIN;
    
    for (Tuple const& t: range) {
      if (t.second < min) { min = t.second; }
      if (t.second > max) { max = t.second; }
    }
    
    // min and max are correctly set
    

    So… it’s not a single STL algorithm.

    Note: std::min_element and std::max_element do exist, but that would mean looping twice over the range, it’s certainly feasible though.

    Tuple const& min = *std::min_element(range.first, range.second,
                           [](Tuple const& left, Tuple const& right) {
                               return left.second < right.second;
                           });
    
    Tuple const& max = *std::max_element(range.first, range.second,
                           [](Tuple const& left, Tuple const& right) {
                               return left.second < right.second;
                           });
    
    // Or as noted by Vitali, slightly more efficient:
    
    auto const minmax = std::minmax_element(range.first, range.second, 
                           [](Tuple const& left, Tuple const& right) {
                               return left.second < right.second;
                           });
    
    Tuple const& min = *minmax.first;
    Tuple const& max = *minmax.second;
    

    Note that it gives a tuple, and not the .second member.

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

Sidebar

Related Questions

I have trouble sorting two related but separate lists of tuple lists. One list
Let's suppose that I have a list of elements, and I want to select
I have a list of tuples. Every tuple has 5 elements (corresponding to 5
Good morning, Let's imagine I have a list of Tuple elements, and a function
how to write a function from list to a tuple i have taken the
How can i have a tuple that has a list as a value. F.x.
How could I unpack a tuple of unknown to, say, a list? I have
If I have a list: my_list = [3,2,2,3,4,1,3,4] and a tuple my_tuple = (3,5)
So I have a list of tuples such as this: [(1,juca),(22,james),(53,xuxa),(44,delicia)] I want this
I have a List of tuple <Tuple<int, int>> x in that I am getting

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.