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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:45:03+00:00 2026-05-23T18:45:03+00:00

I am trying find nearest RGB value in QMap (I know it probably should

  • 0

I am trying find nearest RGB value in QMap (I know it probably should be HSV, but that is not the problem). Here is what I got so far:

        it = images_map.find(current_rgb);

        if(it != images_map.begin()){
            mi = images_map.lowerBound(current_rgb).value();
        }
        else{
            mi = images_map.upperBound(current_rgb).value();
        }

My map looks like this has that indexes:

images_map[ 4283914078 ] 
images_map[ 4284046165 ] 
images_map[ 4284902241 ] 
images_map[ 4289239953 ] 
images_map[ 4282200377 ] 
images_map[ 4289440688 ] 

When my current_rgb is for example 4285046165 it is OK, but if there is some value greater than greatest index, program crashes. What am I doing wrong?

  • 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-23T18:45:04+00:00Added an answer on May 23, 2026 at 6:45 pm

    Possibly because .value() tries to de-reference a non-existing item?

    This looks like your own custom map implementation (or wrapper), but your logic appears to be incorrect

    1. You call lowerBound every time – except if the item you are looking for is the first in the map
    2. If it is the first in the map, you do a search again???
    3. If it’s not you search again (which if already found is repeating the operation again), else if not found, looks for nearest (which is okay), however do you handle the case where there is none (i.e. in lowerBound)?

    The logic should be something like:

    it = images_map.find(current_rgb);
    
    if(it == images_map.end())
    {
      it = images_map.lowerBound(current_rgb);
      if (it == images_map.begin())
      {
        it = images_map.upperBound(current_rgb);
        if (it == images_map.end()) 
          // throw error
      }
      // now you know you have a valid iterator - de-reference
      mi = *it.value();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to find a way to prove that my program is not running correctly
Trying to find some simple SQL Server PIVOT examples. Most of the examples that
Trying to find an example that has css rollover using sprites & sliding door
Trying to find examples of when decorators might be really beneficial, and when not
Im trying to find out a good JavaScript library that can create a nice
I am trying to extract numbers from a typical scoreboard that you would find
EDIT following Ben's answer I'm trying to make something that should be very easy
I'm trying find a way to have PHP to indicate to the browser that
Trying to find the links on a page. my regex is: /<a\s[^>]*href=(\\'??)([^\\' >]*?)[^>]*>(.*)<\/a>/ but
trying to find absolute value and i thought there was a simple way to

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.