Search code examples
c++stlmultimap

multimap upper_bound return confusion


I have a multimap filled with pairings. I want to iterate through a range. upper_bound does not return an iterator pointing to an element unless it find the first element whose key is greater than the value passed to upper_bound.

How can I tell if there was no value returned from upper_bound because there's nothing greater than the passed value?

Thanks!


Solution

  • See reference and msdn

    upper_bound returns the first element that is greater than your key value, if there is no element then it will return the same as end()

    So you can just compare this against the end of your multimap

    auto it = my_multi_map.upper_bound(some_val);
    if (it == my_multi_map.end())
    {
      // iterator is pointing past end so no value found
    }