std::multimap::upper_bound(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… NAME

std::multimap::upper_bound β€” no cap, that's literally the whole name, it's giving "I contain myself" energy πŸ’–

So bestie, you've got a std::multimap, right? And you wanna know where the "bigger than this key" zone starts. That's this girl. She's the boundary-finder, the first-one-past-the-line, the "everything from here on out is greater than your key" icon. Periodt. ✨


🌸 Synopsis

iterator upper_bound( const Key& key );             (1)
const_iterator upper_bound( const Key& key ) const; (2)
template< class K >                                 (3) (since C++14)
iterator upper_bound( const K& x );
template< class K >                                 (4) (since C++14)
const_iterator upper_bound( const K& x ) const;

Okay so there's four flavors, and they are not the same vibe, so pay attention 🫢

  1. Overloads 1 and 2 return an iterator pointing to the first element that is greater than key.
  2. Overloads 3 and 4 (both since C++14) return an iterator pointing to the first element that compares greater to the value x. πŸ¦‹ Here's the tea though: this overload only participates in overload resolution if the qualified-id Compare::is_transparent is valid and denotes a type. Why do we care? Because it lets you call this function without constructing an instance of Key. Chef's kiss. πŸ‘‘

πŸŽ€ Parameters

  • key β€” the key value you're comparing the elements against. Your reference point, your "greater than what?" πŸ’…
  • x β€” the alternative value that can be compared to Key. The flexible bestie. 🌈

⭐ Return value

An iterator pointing to the first element that is greater than key. Plain and simple, main character energy.

But listen β€” if no such element exists? You get the past-the-end iterator (see end()). So like, check before you dereference, bestie, I'm begging πŸ˜­πŸ’€


πŸ”₯ Complexity

Logarithmic in the size of the container. Fast. Efficient. Ate and left no crumbs. ⚑


🧚 Notes

Feature-test macro time, write these down, they will be on the quiz πŸ“πŸ’–

  • __cpp_lib_generic_associative_lookup β€” value 201304L, standard (C++14), feature: Heterogeneous comparison lookup in associative containers, for overloads (3,4)

That's the one that unlocks the fancy "compare without building a Key" behavior. Iykyk. ✨


πŸ™ˆ Example

This section is incomplete.

Reason: no example.

(Yeah, they really left us hanging. Tragic. Not me waiting for a snippet that never comes πŸ’€)


πŸ‘‘ See also

  • equal_range β€” returns range of elements matching a specific key (public member function)
  • lower_bound β€” returns an iterator to the first element not less than the given key (public member function)

Go check out lower_bound too, she's the other half of this whole boundary situation β€” the two of them together are giving duo goals 🫢


πŸ“ Category

  • Todo no example

So that's the whole page, bestie β€” sourced straight from http://cppreference.com, version 2024.06.10, and the page itself is std::multimap::upper_bound(3). Slay, go find your upper bound, and remember: past-the-end is a real place and you don't wanna live there πŸŒΈπŸ’œ

πŸ“¦ where this page comes from