std::multimap::equal_range(3)

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

πŸ’… std::multimap::equal_range πŸ’…

okay bestie, welcome to the std::multimap::equal_range(3) page β€” it's from the C++ Standard Libary (that spelling is what the source says, iykyk), and we're about to make this range-finding function make sense. no cap, it's lowkey one of the most useful little moves in std::multimap. πŸ¦‹


πŸŽ€ NAME πŸŽ€

std::multimap::equal_range - std::multimap::equal_range


✨ Synopsis ✨

std::pair<iterator, iterator> equal_range( const Key& key );                    (1)
std::pair<const_iterator, const_iterator> equal_range( const Key& key ) const;  (2)

template< class K >
std::pair<iterator, iterator> equal_range( const K& x );                        (3) (since C++14)

template< class K >
std::pair<const_iterator, const_iterator> equal_range( const K& x ) const;      (4) (since C++14)

🌸 Description 🌸

okay so this function returns a range containing all the elements with the given key in the container. The range is defined by two iterators: one points to the first element that is not less than key, and the other points to the first element greater than key. Alternatively, bestie, the first iterator may be obtained with lower_bound(), and the second with upper_bound(). periodt. ✨

  • (1), (2) β€” compares the keys to key.
  • (3), (4) β€” compares the keys to the value x. This overload participates in overload resolution only if the qualified-id Compare::is_transparent is valid and denotes a type. It allows calling this function without constructing an instance of Key. πŸ‘‘

πŸŽ€ Parameters πŸŽ€

  • key β€” key value to compare the elements to.
  • x β€” alternative value that can be compared to Key.

🫢 Return value 🫢

std::pair containing a pair of iterators defining the wanted range: the first pointing to the first element that is not less than key, and the second pointing to the first element greater than key.

If there are no elements not less than key, past-the-end (see end()) iterator is returned as the first element. Similarly if there are no elements greater than key, past-the-end iterator is returned as the second element. 😭

since C++11: since emplace and unhinted insert always insert at the upper bound, the order of equivalent elements in the equal range is the order of insertion unless hinted insert or emplace_hint was used to insert an element at a different position. πŸ’€

⚑ Complexity ⚑

Logarithmic in the size of the container. 🧚


πŸ“ Notes πŸ“

Feature-test macro check, bestie:

  • Feature-test macro: __cpp_lib_generic_associative_lookup
  • Value: 201304L
  • Std: (C++14)
  • Feature: Heterogeneous comparison lookup in associative containers, for overloads (3,4)

That's the whole table energy, no cap. 🌈


πŸ’» Example πŸ’»

// Run this code

#include <iostream>
#include <map>

int main()
{
    std::multimap<int, char> dict
    {
        {1, 'A'},
        {2, 'B'},
        {2, 'C'},
        {2, 'D'},
        {4, 'E'},
        {3, 'F'}
    };

    auto range = dict.equal_range(2);

    for (auto i = range.first; i != range.second; ++i)
        std::cout << i->first << ": " << i->second << '\n';
}

🌈 Output: 🌈

2: B
2: C
2: D

πŸ‘€ See also πŸ‘€

  • find β€” finds element with specific key (public member function)
  • contains β€” checks if the container contains element with specific key (C++20) (public member function)
  • count β€” returns the number of elements matching specific key (public member function)
  • upper_bound β€” returns an iterator to the first element greater than the given key (public member function)
  • lower_bound β€” returns an iterator to the first element not less than the given key (public member function)
  • equal_range β€” returns range of elements matching a specific key (function template)

that's the whole page, bestie. shoutout to the source http://cppreference.com, dated 2024.06.10, for this std::multimap::equal_range(3) page. now go find your range and slay. πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from