std::unorde...equal_range(3)
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 keykeyin 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 tox. This overload participates in overload resolution only ifHash::is_transparentandKeyEqual::is_transparentare valid and each denotes a type. This assumes that suchHashis callable with bothKandKeytype, and that theKeyEqualis transparent, which, together, allows calling this function without constructing an instance ofKey.
π 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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms