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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:00:52+00:00 2026-05-16T06:00:52+00:00

I need some help understanding how stdext::hash_multimap’s lower_bound, upper_bound and equal_range work (at least

  • 0

I need some help understanding how stdext::hash_multimap’s lower_bound, upper_bound and equal_range work (at least the VS2005 version of it).

I have the following code (summarized for the question)

#include <hash_map>

using stdext::hash_multimap;
using std::greater;
using stdext::hash_compare;
using std::pair;
using std::cout;

typedef hash_multimap < double, CComBSTR, hash_compare< double, greater<double> > > HMM;
HMM hm1;
HMM :: const_iterator it1, it2;
pair<HMM::const_iterator, HMM::const_iterator> pairHMM;

typedef pair <double, CComBSTR> PairDblStr;

// inserting only two values for sample
hm1.insert ( PairDblStr ( 0.224015748, L"#1-64" ) );
hm1.insert ( PairDblStr ( 0.215354331, L"#1-72" ) );

// Using a double value in between the inserted key values to find one of the elements in the map
it1 = hm1.lower_bound( 0.2175 );

if( it1 == hm1.end() )
{
    cout << "lower_bound failed\n";
}

it1 = hm1.upper_bound( 0.2175 );

if( it1 == hm1.end() )
{
    cout << "upper_bound failed\n";
}

pairHMM = hm1.equal_range( 0.2175 );
if( ( pairHMM.first == hm1.end() ) && ( pairHMM.second == hm1.end() ) )
{
    cout << "equal_range failed\n";
}

As mentioned in the comment I am passing in a value (0.2175) that is in between the two inserted key values (0.224015748, 0.215354331). But the output of the code is:

lower_bound failed
upper_bound failed
equal_range failed

Did I misunderstand how the lower_bound, upper_bound and equal_range can be used in maps? Can we not find a “closest match” key using these methods? If these methods are not suitable, would you have any suggestion on what I could use for my requirement?

Thanks in advance for any help.

Thanks to @billy-oneal @dauphic for their comments and edits. I have updated the code above to make it compilable and runnable (once you include the correct headers of course).

  • 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-16T06:00:53+00:00Added an answer on May 16, 2026 at 6:00 am

    Can we not find a “closest match” key using these methods?

    No. hash_multimap is implemented using a hashtable. Two keys that are very close to each other (0.2153 and 0.2175, for example) will likely map to totally different bins in the hashtable.

    A hashtable does not maintain its elements in a sorted order, so you cannot find the closest match to a given key without a linear search.

    The lower_bound, upper_bound, and equal_range functions in hash_multimap have a somewhat odd implementation in the Visual C++ standard library extensions.

    Consider the documentation for lower_bound:

    The member function determines the first element X in the controlled sequence that hashes to the same bucket as key and has equivalent ordering to key. If no such element exists, it returns hash_map::end; otherwise it returns an iterator that designates X. You use it to locate the beginning of a sequence of elements currently in the controlled sequence that match a specified key.

    And the documentation for upper_bound:

    The member function determines the last element X in the controlled sequence that hashes to the same bucket as key and has equivalent ordering to key. If no such element exists, or if X is the last element in the controlled sequence, it returns hash_map::end; otherwise it returns an iterator that designates the first element beyond X. You use it to locate the end of a sequence of elements currently in the controlled sequence that match a specified key.

    Essentially, these functions allow you to identify the range of elements that have a given key. Their behavior is not the same as the behavior of std::lower_bound or std::map::lower_bound (theirs is the behavior that you were expecting).

    For what it’s worth, the C++0x unordered associative containers do not provide lower_bound, upper_bound, or equal_range functions.

    Would you have any suggestion on what I could use for my requirement?

    Yes: if you need the behavior of lower_bound and upper_bound, use an ordered associative container like std::multimap.

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

Sidebar

Related Questions

I have been trying out Cassandra and need some help in understanding a few
I need some help at understanding how foreign keys and cascades work. I understood
I need some help understanding how jQuery stores elements. Please have a look at
I think I need some help understanding the Dispatcher Queue . When new work
I need some help understanding how pip and yolk work I ran pip install
I need some help understanding this bit of code pre { white-space: pre; white-space:
OK, I need some help understanding the process behind Facebook's website integration process and
Oracle's PL/SQL is fairly new to me, so I need some help understanding if
I need some help in understanding when and why to use Remote Service instead
I need some help in understanding what is happening here .I am getting a

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.