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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:37:29+00:00 2026-05-30T22:37:29+00:00

I came across the following code snippet in C++ (I am not yet on

  • 0

I came across the following code snippet in C++ (I am not yet on C++11):

int test(std::map<int, size_t> &threshold, const int value) {
  std::map<int, size_t>::const_iterator itr = threshold.upper_bound(value);

  if (threshold.begin() == itr) {
    return -1;
  }
  return return (--itr)->second;
}

In particular, I do not like the use of --itr at the end nor the compare of itr to begin(), they both feel wrong to me.

I’m wondering if there is a way with STL to do some kind of lookup that would return end() (or rend()) if not found and otherwise to return the last element that is less than or equal to the value so the code would look more like this:

int test(std::map<int, size_t> &threshold, const int value) {
  std::map<int, size_t>::const_reverse_iterator itr = threshold.WhatGoesHere(value);

  if (threshold.rend() == itr) {
    return -1;
  }
  return return itr->second;
}

In a sense, I want a reverse_lower_bound() that returns a reverse iterator to the last element that is not greater than the value or if none can be found rend().

  • 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-30T22:37:31+00:00Added an answer on May 30, 2026 at 10:37 pm

    Based on Xeo’s comment, I think this is the answer:

    int test(std::map<int, size_t> &threshold, const int value) {
      std::map<int, size_t>::const_reverse_iterator
        last_element_not_greater_than(threshold.upper_bound(value));
    
      if (threshold.rend() == last_element_not_greater_than) {
        return -1;
      }
      return return last_element_not_greater_than->second;
    }
    

    I learned this new thing:

    When an iterator is reversed, the reversed version does not point to the same
    element in the range, but to the one preceding it.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across the following code : int i; for(; scanf(%s, &i);) printf(hello); As
Possible Duplicate: What does map(&:name) mean in Ruby? I came across a code snippet
I have a question regarding the following code snippet I came across in one
I came across the following code snippet and needed to predict the output. My
While investigating a crash, I came across the following code snippet and immediately recognized
I was studying the Twitter source code, and I came across the following snippet:
I came across the following code on msdn: unsafe static void SquarePtrParam (int* p)
I came across this code snippet 1 int return_printChar1() { // code // oops!
I came across the following code snippet on the Scala mailing list: scala> class
I came across the following code: int main() { char *A=(char *)malloc(20); char *B=(char

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.