std::unorde...equal_range(3)

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

Hey bestie πŸ’– you're reading std::unorde...equal_range(3) from the C++ Standard Library, and the main character is std::unordered_multimap::equal_range. Let's get into it ✨

🌸 NAME 🌸

std::unordered_multimap::equal_range β€” std::unordered_multimap::equal_range (yes bestie, the NAME section is literally the same name twice; iconic redundancy, no notes πŸ’…).

πŸ¦‹ Synopsis πŸ¦‹

std::pair<iterator, iterator> equal_range( const Key& key );
(1) (since C++11)
std::pair<const_iterator, const_iterator> equal_range( const Key&
(2) (since C++11)
key ) const;
template< class K >
(3) (since C++20)
std::pair<iterator, iterator> equal_range( const K& x );
template< class K >
std::pair<const_iterator, const_iterator> equal_range( const K& x
(4) (since C++20)
) const;
  • 1,2 β€” Returns a range containing all elements with key key in the container. The range is defined by two iterators, the first pointing to the first element of the wanted range and the second pointing past the last element of the range.
  • 3,4 β€” Returns a range containing all elements in the container with key equivalent to x. This overload participates in overload resolution only if Hash::is_transparent and KeyEqual::is_transparent are valid and each denotes a type. This assumes that such Hash is callable with both K and Key type, and that the KeyEqual is transparent, which, together, allows calling this function without constructing an instance of Key.

πŸŽ€ Parameters πŸŽ€

  • key β€” key value to compare the elements to.
  • x β€” a value of any type that can be transparently compared with a key.

πŸ’œ Return value πŸ’œ

A std::pair containing a pair of iterators defining the wanted range. If there are no such elements, past-the-end (see end()) iterators are returned as both elements of the pair. So yeah, empty range = both sides are the past-the-end vibe 🫢.

πŸ”₯ Complexity πŸ”₯

Average case linear in the number of elements with the key key, worst case linear in the size of the container. Lowkey a range query, highkey a container-sized worst case πŸ’€.

✨ Notes ✨

Feature-test macro tea β˜•:

  • __cpp_lib_generic_unordered_lookup β€” Value: 201811L; Std: (C++20); Feature: Heterogeneous comparison lookup in unordered associative containers, overloads (3,4).

πŸ“ Example πŸ“

Run this code, bestie β€” it's giving "let's print every 1 key in an unordered_multimap" energy:

// Run this code

#include <iostream>
#include <unordered_map>

int main()
{
    std::unordered_multimap<int,char> map = {{1, 'a'},{1, 'b'},{1,
'd'},{2, 'b'}};
    auto range = map.equal_range(1);
    for (auto it = range.first; it != range.second; ++it)
        std::cout << it->first << ' ' << it->second << '\n';
}

✨ Output ✨

1 a
1 b
1 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)
  • equal_range β€” returns range of elements matching a specific key (function template)

That's the whole page, bestie πŸ’– Footer vibes: source/version http://cppreference.com, date 2024.06.10, page std::unorde...equal_range(3). Periodt ✨

πŸ“¦ where this page comes from