std::unorde...:equal_range(3)
πΈ NAME πΈ
Okay so the page title is std::unorde...:equal_range(3), and it comes from the C++ Standard Libary manual β and yes, bestie, βLibaryβ is spelled exactly like that in the header, we are leaving it alone π
. The function itself is std::unordered_map::equal_range β literally std::unordered_map::equal_range, no notes, it's the range-grabbing bestie β¨.
β¨ Synopsis β¨
Here's the overload lineup, and I'm obsessed:
(1) β since C++11:
std::pair<iterator, iterator> equal_range( const Key& key );
(2) β since C++11:
std::pair<const_iterator, const_iterator> equal_range( const Key& key ) const;
(3) β since C++20:
template< class K >
std::pair<iterator, iterator> equal_range( const K& x );
(4) β since C++20:
template< class K >
std::pair<const_iterator, const_iterator> equal_range( const K& x ) const;
So like, 1,2) returns a range containing all elements with key key in the container. That range is defined by two iterators: the first points to the first element of the wanted range, and the second points past the last element of the range. Periodt π
.
And 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. No extra Key construction, highkey iconic π.
π 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. That's the whole vibe: no matches, both iterators are past-the-end π«Ά.
π₯ Complexity π₯
Average case linear in the number of elements with the key key, worst case linear in the size of the container. That's the tea, fr fr.
π§ Notes π§
Feature-test macro tea β β the receipts are:
- Feature-test macro:
__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:
// Run this code
#include <iostream>
#include <unordered_map>
int main()
{
std::unordered_map<int,char> map = {{1, 'a'},{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
π See also π
The whole friend group:
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.
Okay that's the whole page, go be a main character with your iterators π«Ά β source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::unorde...:equal_range(3)
π¦ 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