STD::UNORDERED_MAP(3cxx)
๐ฆ std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc > โ the 3cxx manpage, bestie edition
okay so picture this: you open the <unordered_map> header and this absolute icon of a container is staring back at you. ๐
This is the 3cxx manpage for std::unordered_map, and honestly? It's giving "hash table but make it fashion." Let's get into it, bestie, because there's a lot here. ๐ซถ
๐ธ NAME
std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc > โ a standard container composed of unique keys (containing at most one of each key value) that associates values of another type with the keys.
no cap, that's the whole vibe: one key, one value, keys are unique, duplicates are not invited to the party. it maps stuff to other stuff. iconic. โจ
๐ SYNOPSIS
#include <unordered_map>
๐ Public Types
using insert_return_type = typename _Hashtable::insert_return_typeusing node_type = typename _Hashtable::node_type
Then we've got the public typedefs, and yes, they're all here, every single one โ don't skip, iykyk:
typedef _Hashtable::key_type key_typeโ public typedef, the key type itself ๐typedef _Hashtable::value_type value_typeโ public typedef ๐typedef _Hashtable::mapped_type mapped_typeโ public typedef ๐ธtypedef _Hashtable::hasher hasherโ public typedef, the one that hashes โจtypedef _Hashtable::key_equal key_equalโ public typedef, the equality checker ๐ฆtypedef _Hashtable::allocator_type allocator_typeโ public typedef, the memory hookup ๐
And now the iterator-related typedefs โ literally the whole squad:
typedef _Hashtable::pointer pointerโ iterator-related typedefstypedef _Hashtable::const_pointer const_pointerโ iterator-related typedefstypedef _Hashtable::reference referenceโ iterator-related typedefstypedef _Hashtable::const_reference const_referenceโ iterator-related typedefstypedef _Hashtable::iterator iteratorโ iterator-related typedefstypedef _Hashtable::const_iterator const_iteratorโ iterator-related typedefstypedef _Hashtable::local_iterator local_iteratorโ iterator-related typedefstypedef _Hashtable::const_local_iterator const_local_iteratorโ iterator-related typedefstypedef _Hashtable::size_type size_typeโ iterator-related typedefstypedef _Hashtable::difference_type difference_typeโ iterator-related typedefs
๐ Public Member Functions
okay this is the main character section. buckle up. โญ
unordered_map ()=defaultโ default constructor, does exactly nothing, and honestly sometimes that's the moment ๐template<typename _InputIterator > unordered_map (_InputIterator __first, _InputIterator __last, size_type __n, const allocator_type &__a)template<typename _InputIterator > unordered_map (_InputIterator __first, _InputIterator __last, size_type __n, const hasher &__hf, const allocator_type &__a)template<typename _InputIterator > unordered_map (_InputIterator __first, _InputIterator __last, size_type __n=0, const hasher &__hf=hasher(), const key_equal &__eql=key_equal(), const allocator_type &__a=allocator_type())โ builds anunordered_mapfrom a range ๐ฅunordered_map (const allocator_type &__a)โ creates anunordered_mapwith no elements (empty, but make it hers)unordered_map (const unordered_map &)=defaultโ copy constructor ๐ซถunordered_map (const unordered_map &__umap, const allocator_type &__a)unordered_map (initializer_list< value_type > __l, size_type __n, const allocator_type &__a)unordered_map (initializer_list< value_type > __l, size_type __n, const hasher &__hf, const allocator_type &__a)unordered_map (initializer_list< value_type > __l, size_type __n=0, const hasher &__hf=hasher(), const key_equal &__eql=key_equal(), const allocator_type &__a=allocator_type())โ builds anunordered_mapfrom aninitializer_list๐unordered_map (size_type __n, const allocator_type &__a)unordered_map (size_type __n, const hasher &__hf, const allocator_type &__a)unordered_map (size_type __n, const hasher &__hf=hasher(), const key_equal &__eql=key_equal(), const allocator_type &__a=allocator_type())โ default constructor creates no elements ๐unordered_map (unordered_map &&)=defaultโ move constructor, she's leaving the group chat but taking her stuff ๐งunordered_map (unordered_map &&__umap, const allocator_type &__a) noexcept(noexcept(_Hashtable(std::move(__umap._M_h), __a)))iterator begin () noexceptlocal_iterator begin (size_type __n)โ returns a read/write iterator pointing to the first bucket element ๐ชฃsize_type bucket (const key_type &__key) constsize_type bucket_count () const noexceptโ returns the number of buckets of theunordered_map๐ชฃโจsize_type bucket_size (size_type __n) constvoid clear () noexcepttemplate<typename... _Args> std::pair< iterator, bool > emplace (_Args &&... __args)โ attempts to build and insert astd::pairinto theunordered_maptemplate<typename... _Args> iterator emplace_hint (const_iterator __pos, _Args &&... __args)โ attempts to build and insert astd::pairinto theunordered_mapbool empty () const noexceptโ returnstrueif theunordered_mapis emptyiterator end () noexceptlocal_iterator end (size_type __n)โ returns a read/write iterator pointing to one past the last bucket elements ๐ชฃsize_type erase (const key_type &__x)โ erases elements according to the provided keyiterator erase (const_iterator __first, const_iterator __last)โ erases a[__first,__last)range of elements from anunordered_mapnode_type extract (const key_type &__key)โ extract a node ๐งnode_type extract (const_iterator __pos)โ extract a node ๐งallocator_type get_allocator () const noexceptโ returns the allocator object used by theunordered_maphasher hash_function () constโ returns the hash functor object with which theunordered_mapwas constructedtemplate<typename _InputIterator > void insert (_InputIterator __first, _InputIterator __last)โ a template function that attempts to insert a range of elementsiterator insert (const_iterator, node_type &&__nh)โ re-insert an extracted nodevoid insert (initializer_list< value_type > __l)โ attempts to insert a list of elements into theunordered_mapinsert_return_type insert (node_type &&__nh)โ re-insert an extracted nodetemplate<typename _Obj > pair< iterator, bool > insert_or_assign (const key_type &__k, _Obj &&__obj)โ attempts to insert astd::pairinto theunordered_maptemplate<typename _Obj > iterator insert_or_assign (const_iterator __hint, const key_type &__k, _Obj &&__obj)โ attempts to insert astd::pairinto theunordered_maptemplate<typename _Obj > iterator insert_or_assign (const_iterator __hint, key_type &&__k, _Obj &&__obj)template<typename _Obj > pair< iterator, bool > insert_or_assign (key_type &&__k, _Obj &&__obj)key_equal key_eq () constโ returns the key comparison object with which theunordered_mapwas constructedfloat load_factor () const noexceptโ returns the average number of elements per bucketsize_type max_bucket_count () const noexceptโ returns the maximum number of buckets of theunordered_mapfloat max_load_factor () const noexceptโ returns a positive number that theunordered_maptries to keep the load factor less than or equal tovoid max_load_factor (float __z)โ change theunordered_mapmaximum load factorsize_type max_size () const noexceptโ returns the maximum size of theunordered_maptemplate<typename _H2 , typename _P2 > void merge (unordered_map< _Key, _Tp, _H2, _P2, _Alloc > &&__source)template<typename _H2 , typename _P2 > void merge (unordered_map< _Key, _Tp, _H2, _P2, _Alloc > &__source)template<typename _H2 , typename _P2 > void merge (unordered_multimap< _Key, _Tp, _H2, _P2, _Alloc > &&__source)template<typename _H2 , typename _P2 > void merge (unordered_multimap< _Key, _Tp, _H2, _P2, _Alloc > &__source)unordered_map & operator= (const unordered_map &)=defaultโ copy assignment operator ๐ซถunordered_map & operator= (initializer_list< value_type > __l)โUnordered_maplist assignment operatorunordered_map & operator= (unordered_map &&)=defaultโ move assignment operatorvoid rehash (size_type __n)โ may rehash theunordered_mapvoid reserve (size_type __n)โ prepare theunordered_mapfor a specified number of elementssize_type size () const noexceptโ returns the size of theunordered_mapvoid swap (unordered_map &__x) noexcept(noexcept(_M_h.swap(__x._M_h)))โ swaps data with anotherunordered_maptemplate<typename... _Args> pair< iterator, bool > try_emplace (const key_type &__k, _Args &&... __args)โ attempts to build and insert astd::pairinto theunordered_maptemplate<typename... _Args> iterator try_emplace (const_iterator __hint, const key_type &__k, _Args &&... __args)โ attempts to build and insert astd::pairinto theunordered_maptemplate<typename... _Args> iterator try_emplace (const_iterator __hint, key_type &&__k, _Args &&... __args)template<typename... _Args> pair< iterator, bool > try_emplace (key_type &&__k, _Args &&... __args)
๐ and there's MORE (const overloads + friends, we love a bonus)
const_iterator begin () const noexceptconst_iterator cbegin () const noexceptconst_iterator end () const noexceptconst_iterator cend () const noexceptstd::pair< iterator, bool > insert (const value_type &__x)โ attempts to insert astd::pairinto theunordered_mapstd::pair< iterator, bool > insert (value_type &&__x)โ attempts to insert astd::pairinto theunordered_maptemplate<typename _Pair > __enable_if_t< is_constructible< value_type, _Pair && >::value, pair< iterator, bool > > insert (_Pair &&__x)โ attempts to insert astd::pairinto theunordered_mapiterator insert (const_iterator __hint, const value_type &__x)โ attempts to insert astd::pairinto theunordered_mapiterator insert (const_iterator __hint, value_type &&__x)โ attempts to insert astd::pairinto theunordered_maptemplate<typename _Pair > __enable_if_t< is_constructible< value_type, _Pair && >::value, iterator > insert (const_iterator __hint, _Pair &&__x)โ attempts to insert astd::pairinto theunordered_mapiterator erase (const_iterator __position)โ erases an element from anunordered_mapiterator erase (iterator __position)โ erases an element from anunordered_mapiterator find (const key_type &__x)โ tries to locate an element in anunordered_map๐template<typename _Kt > auto find (const _Kt &__x) -> decltype(_M_h._M_find_tr(__x))โ tries to locate an element in anunordered_map๐const_iterator find (const key_type &__x) constโ tries to locate an element in anunordered_map๐template<typename _Kt > auto find (const _Kt &__x) const -> decltype(_M_h._M_find_tr(__x))โ tries to locate an element in anunordered_map๐size_type count (const key_type &__x) constโ finds the number of elementstemplate<typename _Kt > auto count (const _Kt &__x) const -> decltype(_M_h._M_count_tr(__x))โ finds the number of elements
okay that's the synopsis and the member function lineup, and we're literally just getting started โ more of this page is coming, so don't go anywhere bestie ๐
bool contains (const key_type &__x) constโ the "bestie does this key even exist" check ๐ finds whether an element with the given key exists. lowkey the most useful little guy on the whole page.template<typename _Kt > auto contains (const _Kt &__x) const -> decltype(_M_h._M_find_tr(__x), void(), true)โ same vibe, template flavor ๐ฆ finds whether an element with the given key exists.std::pair< iterator, iterator > equal_range (const key_type &__x)โ finds a subsequence matching given key โจ yes, a subsequence โ it's giving range, not single element.template<typename _Kt > auto equal_range (const _Kt &__x) -> decltype(_M_h._M_equal_range_tr(__x))โ the templated twin ๐ finds a subsequence matching given key.std::pair< const_iterator, const_iterator > equal_range (const key_type &__x) constโ read-only edition ๐ธ finds a subsequence matching given key.template<typename _Kt > auto equal_range (const _Kt &__x) const -> decltype(_M_h._M_equal_range_tr(__x))โ read-only and templated, the full combo ๐ finds a subsequence matching given key.mapped_type & operator[] (const key_type &__k)โ subscript ([]) access to unordered_map data ๐ฅ the one you'll type a million times, no cap.mapped_type & operator[] (key_type &&__k)โ subscript ([]) access to unordered_map data, but moving the key in ๐ซ same energy.mapped_type & at (const key_type &__k)โ access to unordered_map data ๐const mapped_type & at (const key_type &__k) constโ access to unordered_map data, but const and read-only ๐งconst_local_iterator begin (size_type __n) constโ returns a read-only (constant) iterator pointing to the first bucket element ๐ชฃ yes bestie, bucket โ that__nis the bucket number.const_local_iterator cbegin (size_type __n) constโ returns a read-only (constant) iterator pointing to the first bucket element ๐ซง literally thec-prefixed version, same deal.const_local_iterator end (size_type __n) constโ returns a read-only (constant) iterator pointing to one past the last bucket elements ๐ note the plural, that's how it's written.const_local_iterator cend (size_type __n) constโ returns a read-only (constant) iterator pointing to one past the last bucket elements ๐ again with thecprefix.
๐ซถ Friends ๐ซถ
okay so these aren't yours, they're the container's friends โ they get to see the privates ๐ญ iconic behavior:
template<typename _Key1 , typename _Tp1 , typename _Hash1 , typename _Pred1 , typename _Alloc1 > bool operator== (const unordered_map< _Key1, _Tp1, _Hash1, _Pred1, _Alloc1 > &, const unordered_map< _Key1, _Tp1, _Hash1, _Pred1, _Alloc1 > &)โ the equality bestie ๐template<typename , typename , typename > class std::_Hash_merge_helperโ the merge helper gremlin ๐ง internal-looking name, do not touch, just know it's there.
๐ Detailed Description ๐
template<typename _Key, typename _Tp, typename _Hash = hash<_Key>, typename
_Pred = equal_to<_Key>, typename _Alloc = allocator<std::pair<const
_Key, _Tp>>>
class std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >
"A standard container composed of unique keys (containing at most one of each key value) that associates values of another type with the keys.
fr fr that's the whole personality: unique keys, max one of each, each key drags a value of another type along with it ๐ธ
Since: C++11 โญ
Template Parameters ๐
_Keyโ type of key objects._Tpโ type of mapped objects._Hashโ hashing function object type, defaults tohash<_Value>._Predโ predicate function object type, defaults toequal_to<_Value>._Allocโ allocator type, defaults tostd::allocator<std::pair<const _Key, _Tp>>.
Meets the requirements of a container, and unordered associative container โ
The resulting value type of the container is std::pair<const _Key, _Tp> ๐
Base is _Hashtable, dispatched at compile time via template alias __umap_hashtable ๐ง that's the lore, iykyk.
๐งฌ Member Typedef Documentation ๐งฌ
every single one of these lives inside this exact template header, so I'm putting it here once instead of sixteen times, periodt:
template<typename _Key , typename _Tp , typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>>
allocator_typeโ public typedefs ๐typedef _Hashtable::allocator_type std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::allocator_typeconst_iteratorโ iterator-related typedefs ๐ฆtypedef _Hashtable::const_iterator std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::const_iteratorconst_local_iteratorโ iterator-related typedefs ๐ชฃtypedef _Hashtable::const_local_iterator std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::const_local_iteratorconst_pointerโ iterator-related typedefs โจtypedef _Hashtable::const_pointer std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::const_pointerconst_referenceโ iterator-related typedefs ๐งtypedef _Hashtable::const_reference std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::const_referencedifference_typeโ iterator-related typedefs ๐typedef _Hashtable::difference_type std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::difference_typehasherโ public typedefs ๐typedef _Hashtable::hasher std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::hasheriteratorโ iterator-related typedefs ๐ฅtypedef _Hashtable::iterator std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::iteratorkey_equalโ public typedefs ๐typedef _Hashtable::key_equal std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::key_equalkey_typeโ public typedefs โญtypedef _Hashtable::key_type std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::key_typelocal_iteratorโ iterator-related typedefs ๐ซงtypedef _Hashtable::local_iterator std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::local_iteratormapped_typeโ public typedefs ๐typedef _Hashtable::mapped_type std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::mapped_typepointerโ iterator-related typedefs ๐ธtypedef _Hashtable::pointer std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::pointerreferenceโ iterator-related typedefs ๐ซtypedef _Hashtable::reference std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::referencesize_typeโ iterator-related typedefs ๐งtypedef _Hashtable::size_type std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::size_typevalue_typeโ public typedefs ๐typedef _Hashtable::value_type std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::value_type
that's the whole typedef family, all sixteen accounted for, nothing skipped ๐ซถ
๐ธ Constructor & Destructor Documentation ๐ธ
okay so first up, the constructors โ there are a lot of them, but they're all just different ways to birth an unordered_map, and honestly each one ate ๐
every single one of these lives inside the same big template:
template<typename _Key , typename _Tp , typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>>
๐ unordered_map () [default]
std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::unordered_map () [default]
The default constructor. no args, no drama, just vibes โจ
๐ unordered_map (size_type __n, const hasher & __hf = hasher(), const key_equal & __eql = key_equal(), const allocator_type & __a = allocator_type()) [inline], [explicit]
std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::unordered_map (size_type __n, const hasher &
__hf = hasher(), const key_equal & __eql = key_equal(), const
allocator_type & __a = allocator_type()) [inline], [explicit]
Default constructor creates no elements. (yes bestie, that's what it says โ no elements, we're starting empty and unbothered ๐ซถ)
Parameters
__nโ Minimal initial number of buckets.__hfโ A hash functor.__eqlโ A key equality functor.__aโ An allocator object.
๐ unordered_map (_InputIterator __first, _InputIterator __last, size_type __n = 0, const hasher & __hf = hasher(), const key_equal & __eql = key_equal(), const allocator_type & __a = allocator_type()) [inline]
template<typename _InputIterator>
std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::unordered_map
(_InputIterator __first, _InputIterator __last, size_type __n = 0, const
hasher & __hf = hasher(), const key_equal & __eql = key_equal(), const
allocator_type & __a = allocator_type()) [inline]
Builds an unordered_map from a range. give it a [__first,__last) and it just makes one, iconic.
Parameters
__firstโ An input iterator.__lastโ An input iterator.__nโ Minimal initial number of buckets.__hfโ A hash functor.__eqlโ A key equality functor.__aโ An allocator object.
Create an unordered_map consisting of copies of the elements from [__first,__last). This is linear in N (where N is distance(__first,__last)).
๐ unordered_map (const unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc > &) [default]
std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::unordered_map (const unordered_map< _Key, _Tp,
_Hash, _Pred, _Alloc > &) [default]
Copy constructor. twin behavior fr fr ๐ฏ
๐ unordered_map (unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc > &&) [default]
std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::unordered_map (unordered_map< _Key, _Tp, _Hash,
_Pred, _Alloc > &&) [default]
Move constructor. it just takes the other one's stuff and leaves it empty โ main character energy, no cap ๐ฅ
๐ unordered_map (const allocator_type & __a) [inline], [explicit]
std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::unordered_map (const allocator_type & __a)
[inline], [explicit]
Creates an unordered_map with no elements.
Parameters
__aโ An allocator object.
๐ unordered_map (initializer_list< value_type > __l, size_type __n = 0, const hasher & __hf = hasher(), const key_equal & __eql = key_equal(), const allocator_type & __a = allocator_type()) [inline]
std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::unordered_map (initializer_list< value_type >
__l, size_type __n = 0, const hasher & __hf = hasher(), const key_equal
& __eql = key_equal(), const allocator_type & __a = allocator_type())
[inline]
Builds an unordered_map from an initializer_list. curly braces bestie ๐
Parameters
__lโ An initializer_list.__nโ Minimal initial number of buckets.__hfโ A hash functor.__eqlโ A key equality functor.__aโ An allocator object.
Create an unordered_map consisting of copies of the elements in the list. This is linear in N (where N is __l.size()).
โจ Member Function Documentation โจ
๐ mapped_type & at (const key_type & __k) [inline]
mapped_type & std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::at (const key_type & __k) [inline]
Access to unordered_map data.
Parameters
__kโ The key for which data should be retrieved.
Returns
A reference to the data whose key is equal to __k, if such a data is present in the unordered_map.
Exceptions
std::out_of_range If no such data is present. ๐ญ (so like, use this one when you know the key's in there, otherwise she'll throw)
๐ const mapped_type & at (const key_type & __k) const [inline]
const mapped_type & std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::at (const key_type & __k) const [inline]
Access to unordered_map data.
Parameters
__kโ The key for which data should be retrieved.
Returns
A reference to the data whose key is equal to __k, if such a data is present in the unordered_map.
Exceptions
std::out_of_range If no such data is present. same deal, but read-only this time ๐ซถ
๐ const_iterator begin () const [inline], [noexcept]
const_iterator std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::begin () const [inline], [noexcept]
Returns a read-only (constant) iterator that points to the first element in the unordered_map.
๐ iterator begin () [inline], [noexcept]
iterator std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::begin () [inline], [noexcept]
Returns a read/write iterator that points to the first element in the unordered_map.
๐ local_iterator begin (size_type __n) [inline]
local_iterator std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::begin (size_type __n) [inline]
Returns a read/write iterator pointing to the first bucket element. this is the bucket-flavored begin, iykyk ๐ชฃ
Parameters
__nโ The bucket index.
Returns
A read/write local iterator.
๐ const_local_iterator begin (size_type __n) const [inline]
const_local_iterator std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::begin (size_type __n) const [inline]
Returns a read-only (constant) iterator pointing to the first bucket element.
Parameters
__nโ The bucket index.
Returns
A read-only local iterator.
๐ size_type bucket_count () const [inline], [noexcept]
size_type std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::bucket_count () const [inline], [noexcept]
Returns the number of buckets of the unordered_map. just a little headcount of the buckets ๐ชฃโจ
๐ const_iterator cbegin () const [inline], [noexcept]
const_iterator std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::cbegin () const [inline], [noexcept]
Returns a read-only (constant) iterator that points to the first element in the unordered_map. the c stands for "can't touch this" ๐
๐ const_local_iterator cbegin (size_type __n) const [inline]
const_local_iterator std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::cbegin (size_type __n) const [inline]
Returns a read-only (constant) iterator pointing to the first bucket element.
Parameters
__nโ The bucket index.
Returns
A read-only local iterator.
๐ const_iterator cend () const [inline], [noexcept]
const_iterator std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::cend () const [inline], [noexcept]
Returns a read-only (constant) iterator that points one past the last element in the unordered_map.
Referenced bystd::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::insert_or_assign(), andstd::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::try_emplace().
๐ const_local_iterator cend (size_type __n) const [inline]
const_local_iterator std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::cend (size_type __n) const [inline]
Returns a read-only (constant) iterator pointing to one past the last bucket elements.
Parameters
__nโ The bucket index.
Returns
A read-only local iterator.
๐ void clear () [inline], [noexcept]
void std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::clear () [inline], [noexcept]
Erases all elements in an unordered_map. Note that this function only erases the elements, and that if the elements themselves are pointers, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibility.
bestie please read that twice ๐ญ clear() wipes the map's elements, NOT whatever your pointers are pointing at. those are yours to manage. no take-backsies, no free()s.
๐ template<typename _Kt > auto contains (const _Kt & __x) const -> decltype(_M_h._M_find_tr(__x), void(), true) [inline]
template<typename _Kt > auto
std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::contains (const
_Kt & __x) const -> decltype(_M_h._M_find_tr(__x), void(), true)
[inline]
Finds whether an element with the given key exists.
Parameters
__xโ Key of elements to be located.
Returns
True if there is any element with the specified key.
๐ bool contains (const key_type & __x) const [inline]
bool std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::contains (const key_type & __x) const
[inline]
Finds whether an element with the given key exists.
Parameters
__xโ Key of elements to be located.
Returns
True if there is any element with the specified key.
๐ template<typename _Kt > auto count (const _Kt & __x) const -> decltype(_M_h._M_count_tr(__x)) [inline]
template<typename _Kt > auto
std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::count (const _Kt
& __x) const -> decltype(_M_h._M_count_tr(__x))
[inline]
Finds the number of elements.
Parameters
__xโ Key to count.
Returns
Number of elements with specified key.
This function only makes sense for unordered_multimap; for unordered_map the result will either be 0 (not present) or 1 (present).
so like yes it returns a number, but for plain unordered_map it's literally just 0 or 1 โ she's not doing anything fancy, don't overthink it ๐
๐ count โ how many of this key do we even have
template<typename _Key , typename _Tp , typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>> size_type std::unordered_map<
_Key, _Tp, _Hash, _Pred, _Alloc >::count (const key_type & __x) const
[inline]
Finds the number of elements. Yes bestie, it literally counts them for you ๐
Parameters
__xโ Key to count.
Returns
Number of elements with specified key.
ok so real talk: this function only makes sense forunordered_multimap; forunordered_mapthe result will either be0(not present) or1(present). So likeโฆ it's a very dramatic way of asking "is this here or not" ๐ญ
โจ emplace โ build it in place, no copy era
template<typename _Key , typename _Tp , typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>> template<typename... _Args>
std::pair< iterator, bool > std::unordered_map< _Key, _Tp, _Hash, _Pred,
_Alloc >::emplace (_Args &&... __args) [inline]
Attempts to build and insert a std::pair into the unordered_map. Like construction and insertion but they're holding hands ๐ซถ
Parameters
__argsโ Arguments used to generate a new pair instance (seestd::piecewise_contructfor passing arguments to each part of the pair constructor).
Returns
A pair, of which the first element is an iterator that points to the possibly inserted pair, and the second is a bool that is true if the pair was actually inserted.
So here's the tea: this function attempts to build and insert a (key, value) pair into the unordered_map. An unordered_map relies on unique keys and thus a pair is only inserted if its first element (the key) is not already present in the unordered_map. No duplicates, no exceptions, we don't do that here ๐ซ
Insertion requires amortized constant time. slay.
๐ฆ emplace_hint โ same thing but with a lil suggestion
template<typename _Key , typename _Tp , typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>> template<typename... _Args>
iterator std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc
>::emplace_hint (const_iterator __pos, _Args &&... __args) [inline]
Attempts to build and insert a std::pair into the unordered_map. Same energy, extra parameter ๐
Parameters
__posโ An iterator that serves as a hint as to where the pair should be inserted.__argsโ Arguments used to generate a new pair instance (seestd::piecewise_contructfor passing arguments to each part of the pair constructor).
Returns
An iterator that points to the element with key of the std::pair built from __args (may or may not be that std::pair).
This function is not concerned about whether the insertion took place, and thus does not return a boolean like the single-argument emplace() does. Very chill of it honestly. Note that the first parameter is only a hint and can potentially improve the performance of the insertion process. A bad hint would cause no gains in efficiency. ๐
See https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints for more on hinting.
Insertion requires amortized constant time.
๐ empty โ is it empty or is it serving
template<typename _Key , typename _Tp , typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>> bool std::unordered_map< _Key,
_Tp, _Hash, _Pred, _Alloc >::empty () const [inline], [noexcept]
Returns true if the unordered_map is empty. That's it. That's the whole function. [noexcept] and everything, an icon ๐
๐ธ end (the const girlie)
template<typename _Key , typename _Tp , typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>> const_iterator
std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::end () const
[inline], [noexcept]
Returns a read-only (constant) iterator that points one past the last element in the unordered_map. Classic past-the-end behaviour, no notes ๐
๐ฅ end (the read/write one)
template<typename _Key , typename _Tp , typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>> iterator std::unordered_map<
_Key, _Tp, _Hash, _Pred, _Alloc >::end () [inline], [noexcept]
Returns a read/write iterator that points one past the last element in the unordered_map.
Referenced by std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::extract(). iykyk ๐
๐ง end (bucket edition, read/write)
template<typename _Key , typename _Tp , typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>> local_iterator
std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::end (size_type
__n) [inline]
Returns a read/write iterator pointing to one past the last bucket elements. Buckets have ends too, bestie ๐ชฃ
Parameters
__nโ The bucket index.
Returns
A read/write local iterator.
๐ end (bucket edition, read-only)
template<typename _Key , typename _Tp , typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>> const_local_iterator
std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::end (size_type
__n) const [inline]
Returns a read-only (constant) iterator pointing to one past the last bucket elements.
Parameters
__nโ The bucket index.
Returns
A read-only local iterator.
๐ equal_range โ the fancy template one, non-const
template<typename _Key , typename _Tp , typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>> template<typename _Kt > auto
std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::equal_range
(const _Kt & __x) -> decltype(_M_h._M_equal_range_tr(__x))
[inline]"
Finds a subsequence matching given key. Look at that trailing return type โ she's doing transparent lookup things, we love a flexible queen โจ
Parameters
__xโ Key to be located.
Returns
Pair of iterators that possibly points to the subsequence matching given key.
lowkey this function probably only makes sense forunordered_multimap. For plainunordered_mapit's likeโฆ a very extra way to look up one thing ๐
๐ฆ equal_range โ the fancy template one, const
template<typename _Key , typename _Tp , typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>> template<typename _Kt > auto
std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::equal_range
(const _Kt & __x) const -> decltype(_M_h._M_equal_range_tr(__x))
[inline]"
Finds a subsequence matching given key. Same as above but she's const now โ read-only main character energy ๐
Parameters
__xโ Key to be located.
Returns
Pair of iterators that possibly points to the subsequence matching given key.
This function probably only makes sense for unordered_multimap. (saying it twice because it's true twice fr fr)
๐ equal_range โ plain key_type, non-const
template<typename _Key , typename _Tp , typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>> std::pair< iterator, iterator >
std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::equal_range
(const key_type & __x) [inline]
Finds a subsequence matching given key.
Parameters
__xโ Key to be located.
Returns
Pair of iterators that possibly points to the subsequence matching given key.
This function probably only makes sense for unordered_multimap. ๐ซถ
๐ equal_range โ plain key_type, const
template<typename _Key , typename _Tp , typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>> std::pair< const_iterator,
const_iterator > std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc
>::equal_range (const key_type & __x) const [inline]
Finds a subsequence matching given key.
Parameters
__xโ Key to be located.
Returns
Pair of iterators that possibly points to the subsequence matching given key.
This function probably only makes sense for unordered_multimap. (const pair of const iterators, she's very demure, very mindful ๐
)
๐ erase โ by key
template<typename _Key , typename _Tp , typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>> size_type std::unordered_map<
_Key, _Tp, _Hash, _Pred, _Alloc >::erase (const key_type & __x) [inline]
Erases elements according to the provided key. bye bestie ๐
Parameters
__xโ Key of element to be erased.
Returns
The number of elements erased.
This function erases all the elements located by the given key from an unordered_map. For an unordered_map the result of this function can only be 0 (not present) or 1 (present).
โ ๏ธ Note that this function only erases the element, and that if the element is itself a pointer, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibility. no cap, that's on you ๐
๐ธ erase โ by range
template<typename _Key , typename _Tp , typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>> iterator std::unordered_map<
_Key, _Tp, _Hash, _Pred, _Alloc >::erase (const_iterator __first,
const_iterator __last) [inline]
Erases a [__first,__last) range of elements from an unordered_map. Half-open range, as is tradition โจ
Parameters
__firstโ Iterator pointing to the start of the range to be erased.__lastโ Iterator pointing to the end of the range to be erased.
Returns
The iterator __last.
This function erases a sequence of elements from an unordered_map.
โ ๏ธ Note that this function only erases the elements, and that if the element is itself a pointer, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibility. ๐
๐ง erase โ by const_iterator position
template<typename _Key , typename _Tp , typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>> iterator std::unordered_map<
_Key, _Tp, _Hash, _Pred, _Alloc >::erase (const_iterator __position)
[inline]
Erases an element from an unordered_map. single-target violence ๐ฏ
Parameters
__positionโ An iterator pointing to the element to be erased.
Returns
An iterator pointing to the element immediately following __position prior to the element being erased. If no such element exists, end() is returned.
This function erases an element, pointed to by the given iterator, from an unordered_map.
โ ๏ธ Note that this function only erases the element, and that if the element is itself a pointer, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibility. ๐ซถ
๐ erase โ by iterator position
template<typename _Key , typename _Tp , typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>> iterator std::unordered_map<
_Key, _Tp, _Hash, _Pred, _Alloc >::erase (iterator __position) [inline]
Erases an element from an unordered_map. Same as the const_iterator one but she takes a plain iterator โ equal rights ๐
Parameters
__positionโ An iterator pointing to the element to be erased.
Returns
An iterator pointing to the element immediately following __position prior to the element being erased. If no such element exists, end() is returned.
This function erases an element, pointed to by the given iterator, from an unordered_map.
โ ๏ธ Note that this function only erases the element, and that if the element is itself a pointer, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibility. ๐ญ
๐ extract โ grab the whole node
template<typename _Key , typename _Tp , typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>> node_type std::unordered_map<
_Key, _Tp, _Hash, _Pred, _Alloc >::extract (const key_type & __key)
[inline]
Extract a node. ๐
๐
extract โ the "gimme that node back" move
template<typename _Key, typename _Tp, typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>>
node_type
std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::
extract(const_iterator __pos) [inline]
Extract a node. That's it, that's the whole vibe โ you hand it a spot and it yoinks the node out of the map, no cap. ๐ซณ
- References:
std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::end()
๐ find โ the search era
Okay bestie, there are four find overloads and they all do the same little dance, so stay with me. ๐ต๏ธโโ๏ธ
the two template ones (_Kt flavour)
template<typename _Key, typename _Tp, typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>>
template<typename _Kt>
auto
std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::
find(const _Kt& __x) -> decltype(_M_h._M_find_tr(__x)) [inline]
template<typename _Key, typename _Tp, typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>>
template<typename _Kt>
auto
std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::
find(const _Kt& __x) const -> decltype(_M_h._M_find_tr(__x)) [inline]
Tries to locate an element in an unordered_map. ๐ซ
- Parameters:
__xโ Key to be located.
- Returns: Iterator pointing to sought-after element, or
end()if not found.
This function takes a key and tries to locate the element with which the key matches. If successful the function returns an iterator pointing to the sought after element. If unsuccessful it returns the past-the-end (end()) iterator. Basically: found it โ slay, didn't find it โ you get end() and you gotta deal. ๐ญ
the two key_type ones
template<typename _Key, typename _Tp, typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>>
iterator
std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::
find(const key_type& __x) [inline]
template<typename _Key, typename _Tp, typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>>
const_iterator
std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::
find(const key_type& __x) const [inline]
Tries to locate an element in an unordered_map. Same energy, keyed on key_type this time. โจ
- Parameters:
__xโ Key to be located.
- Returns: Iterator pointing to sought-after element, or
end()if not found.
This function takes a key and tries to locate the element with which the key matches. If successful the function returns an iterator pointing to the sought after element. If unsuccessful it returns the past-the-end (end()) iterator. (Yes it's the same paragraph four times, the docs said it and I'm saying it, periodt.)
๐ get_allocator
template<typename _Key, typename _Tp, typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>>
allocator_type
std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::
get_allocator() const [inline], [noexcept]
Returns the allocator object used by the unordered_map. That's the whole thing, she's lowkey chill and also noexcept so she literally cannot ruin your day. ๐ง
๐ธ hash_function
template<typename _Key, typename _Tp, typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>>
hasher
std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::
hash_function() const [inline]
Returns the hash functor object with which the unordered_map was constructed. Not a new one, not a copy you can vibe with โ the one she was built with. ๐
๐ insert โ the main character of this whole section
Six overloads, one legend. Let's go. ๐ฅ
the range one
template<typename _Key, typename _Tp, typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>>
template<typename _InputIterator>
void
std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::
insert(_InputIterator __first, _InputIterator __last) [inline]
A template function that attempts to insert a range of elements. โจ
- Parameters:
__firstโ Iterator pointing to the start of the range to be inserted.__lastโ Iterator pointing to the end of the range.
Complexity similar to that of the range constructor.
the single-pair one โ perfect-forwarding edition
template<typename _Key, typename _Tp, typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>>
template<typename _Pair>
__enable_if_t< is_constructible< value_type, _Pair && >::value, pair<iterator, bool> >
std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::
insert(_Pair&& __x) [inline]
Attempts to insert a std::pair into the unordered_map. ๐
- Parameters:
__xโ Pair to be inserted (seestd::make_pairfor easy creation of pairs).
- Returns: A
pair, of which the first element is an iterator that points to the possibly inserted pair, and the second is aboolthat istrueif the pair was actually inserted.
This function attempts to insert a (key, value) pair into the unordered_map. An unordered_map relies on unique keys and thus a pair is only inserted if its first element (the key) is not already present in the unordered_map. Soz, no duplicates, she's monogamous about keys. ๐
Insertion requires amortized constant time.
the single-pair one โ const value_type& edition
template<typename _Key, typename _Tp, typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>>
std::pair<iterator, bool>
std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::
insert(const value_type& __x) [inline]
Attempts to insert a std::pair into the unordered_map. Same deal, different parameter type. ๐
- Parameters:
__xโ Pair to be inserted (seestd::make_pairfor easy creation of pairs).
- Returns: A
pair, of which the first element is an iterator that points to the possibly inserted pair, and the second is aboolthat istrueif the pair was actually inserted.
This function attempts to insert a (key, value) pair into the unordered_map. An unordered_map relies on unique keys and thus a pair is only inserted if its first element (the key) is not already present in the unordered_map.
Insertion requires amortized constant time.
the hint trio ๐ฏโโ๏ธ
Three overloads, all taking const_iterator __hint first. Same description, same rules, and I'm giving you all of them because every detail stays. ๐
template<typename _Key, typename _Tp, typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>>
template<typename _Pair>
__enable_if_t< is_constructible< value_type, _Pair && >::value, iterator >
std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::
insert(const_iterator __hint, _Pair&& __x) [inline]
template<typename _Key, typename _Tp, typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>>
iterator
std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::
insert(const_iterator __hint, const value_type& __x) [inline]
template<typename _Key, typename _Tp, typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>>
iterator
std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::
insert(const_iterator __hint, value_type&& __x) [inline]
Attempts to insert a std::pair into the unordered_map. ๐ธ
- Parameters:
__hintโ An iterator that serves as a hint as to where the pair should be inserted.__xโ Pair to be inserted (seestd::make_pairfor easy creation of pairs).
- Returns: An iterator that points to the element with key of
__x(may or may not be the pair passed in).
This function is not concerned about whether the insertion took place, and thus does not return a boolean like the single-argument insert() does. Note that the first parameter is only a hint and can potentially improve the performance of the insertion process. A bad hint would cause no gains in efficiency. So like, hint politely or don't bother โ a bad hint is giving nothing. ๐
See https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints for more on hinting.
Insertion requires amortized constant time.
The value_type&& one also references std::move().
the re-insert-an-extracted-node one ๐ฆ
template<typename _Key, typename _Tp, typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>>
iterator
std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::
insert(const_iterator, node_type&& __nh) [inline]
Re-insert an extracted node. Yes, she's the bestie of extract up above โ take a node out, put a node back in. Iconic circular behaviour. โจ
- References:
std::move()
the initializer-list one ๐
template<typename _Key, typename _Tp, typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>>
void
std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::
insert(initializer_list<value_type> __l) [inline]
Attempts to insert a list of elements into the unordered_map. ๐
- Parameters:
__lโ Astd::initializer_list<value_type>of elements to be inserted.
Complexity similar to that of the range constructor.
๐ฅ insert โ the node_type edition
template<typename _Key , typename _Tp , typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>> insert_return_type
std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::insert (node_type
&& __nh) [inline]
Bestie, this one re-inserts a node that you already extracted. That's it, that's the whole vibe โ put the node back. ๐
Also references std::move().
๐ธ insert โ the value_type&& edition
template<typename _Key , typename _Tp , typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>> std::pair< iterator, bool >
std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::insert
(value_type && __x) [inline]
Attempts to insert a std::pair into the unordered_map. So dramatic, so brave. ๐
Parameters
__xโ the pair you wanna insert (peepstd::make_pairif you need easy pair creation, iykyk).
Returns
A pair where element one is an iterator pointing to the possibly-inserted pair, and element two is a bool that's true if the pair was actually inserted.
Okay so the whole lore: this function attempts to insert a (key, value) pair into the unordered_map. An unordered_map relies on unique keys and thus a pair is only inserted if its first element (the key) is not already present in the unordered_map. No duplicates, periodt. ๐ซ
Insertion requires amortized constant time. โก
References std::move().
โจ insert_or_assign โ no hint, all vibes
template<typename _Key , typename _Tp , typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>> template<typename _Obj > pair<
iterator, bool > std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc
>::insert_or_assign (const key_type & __k, _Obj && __obj) [inline]
Attempts to insert a std::pair into the unordered_map. (Yes, same opening line, the libstdc++ docs are creatures of habit ๐ญ)
Parameters
__kโ the key used to go find a possibly-existing pair in the map.__objโ the argument used to generate the.secondfor a pair instance.
Returns
A pair, first element = iterator pointing to the possibly-inserted pair, second element = bool that's true if the pair was actually inserted.
The lore: this function attempts to insert a (key, value) pair into the unordered_map. An unordered_map relies on unique keys and thus a pair is only inserted if its first element (the key) is not already present in the unordered_map. But here's the plot twist ๐ซข โ if the pair was already in the unordered_map, the .second of the pair is assigned from __obj. Insert-or-assign, get it? Iconic behaviour.
Insertion requires amortized constant time.
References std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::cend().
๐ฆ insert_or_assign โ now with a hint
template<typename _Key , typename _Tp , typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>> template<typename _Obj > iterator
std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::insert_or_assign
(const_iterator __hint, const key_type & __k, _Obj && __obj) [inline]
Attempts to insert a std::pair into the unordered_map. (again ๐)
Parameters
__hintโ an iterator that serves as a hint as to where the pair should be inserted.__kโ the key used to go find a possibly-existing pair in the unordered_map.__objโ the argument used to generate the.secondfor a pair instance.
Returns
An iterator that points to the element with key of __x (may or may not be the pair passed in, no promises bestie ๐).
So this function is not concerned about whether the insertion took place, and thus does not return a boolean like the single-argument insert() does. It's chill like that.
If the pair was already in the unordered map, the .second of the pair is assigned from __obj. Note that the first parameter is only a hint and can potentially improve the performance of the insertion process. A bad hint would cause no gains in efficiency. So likeโฆ hint responsibly. ๐
See https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints for more on hinting.
Insertion requires amortized constant time.
๐ key_eq
template<typename _Key , typename _Tp , typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>> key_equal std::unordered_map<
_Key, _Tp, _Hash, _Pred, _Alloc >::key_eq () const [inline]
Returns the key comparison object with which the unordered_map was constructed. That's the _Pred bestie, the one who decides if two keys are "the same". ๐ซถ
๐ load_factor
template<typename _Key , typename _Tp , typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>> float std::unordered_map< _Key,
_Tp, _Hash, _Pred, _Alloc >::load_factor () const [inline], [noexcept]
Returns the average number of elements per bucket. Yes it's a float. Yes it's noexcept. Yes it's giving math girlie. ๐
โญ max_bucket_count
template<typename _Key , typename _Tp , typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>> size_type std::unordered_map<
_Key, _Tp, _Hash, _Pred, _Alloc >::max_bucket_count () const [inline],
[noexcept]
Returns the maximum number of buckets of the unordered_map. Like, the absolute ceiling. The limit. The final boss of buckets. ๐
๐ max_load_factor โ the getter
template<typename _Key , typename _Tp , typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>> float std::unordered_map< _Key,
_Tp, _Hash, _Pred, _Alloc >::max_load_factor () const [inline],
[noexcept]
Returns a positive number that the unordered_map tries to keep the load factor less than or equal to. It's a target, not a promise, ok? ๐
๐ max_load_factor โ the setter
template<typename _Key , typename _Tp , typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>> void std::unordered_map< _Key,
_Tp, _Hash, _Pred, _Alloc >::max_load_factor (float __z) [inline]
Change the unordered_map maximum load factor. You're in the driver's seat now. ๐
Parameters
__zโ the new maximum load factor.
๐ง max_size
template<typename _Key , typename _Tp , typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>> size_type std::unordered_map<
_Key, _Tp, _Hash, _Pred, _Alloc >::max_size () const [inline], [noexcept]
Returns the maximum size of the unordered_map. The biggest it could ever be. Dream big, map. ๐
๐
operator= โ copy assignment
template<typename _Key , typename _Tp , typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>> unordered_map &
std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::operator= (const
unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc > &) [default]
Copy assignment operator. Defaulted, no notes. She just works. โจ
๐ธ operator= โ initializer_list edition
template<typename _Key , typename _Tp , typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>> unordered_map &
std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::operator=
(initializer_list< value_type > __l) [inline]
Unordered_map list assignment operator. Brace-init girlies rise ๐
Parameters
__lโ aninitializer_list.
This function fills an unordered_map with copies of the elements in the initializer list __l.
Heads up bestie: note that the assignment completely changes the unordered_map and that the resulting unordered_map's size is the same as the number of elements assigned. Total glow-up, old contents are gone. ๐
๐ฆ operator= โ move assignment
template<typename _Key , typename _Tp , typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>> unordered_map &
std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::operator=
(unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc > &&) [default]
Move assignment operator. Defaulted. Steals your stuff, politely. ๐ซถ
๐ฅ operator[] โ const key_type& flavour
template<typename _Key , typename _Tp , typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>> mapped_type & std::unordered_map<
_Key, _Tp, _Hash, _Pred, _Alloc >::operator[] (const key_type & __k)
[inline]
Subscript ( [] ) access to unordered_map data. The classic. The main character. ๐
Parameters
__kโ the key for which data should be retrieved.
Returns
A reference to the data of the (key, data) pair.
Allows for easy lookup with the subscript ( [] ) operator. Returns data associated with the key specified in subscript. And here's the iconic part: if the key does not exist, a pair with that key is created using default values, which is then returned. Sneaky little auto-insert, no cap. ๐ญ
Lookup requires constant time.
๐ operator[] โ key_type&& flavour
template<typename _Key , typename _Tp , typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>> mapped_type & std::unordered_map<
_Key, _Tp, _Hash, _Pred, _Alloc >::operator[] (key_type && __k) [inline]
Subscript ( [] ) access to unordered_map data. Same energy, key gets moved now. ๐ซ
Parameters
__kโ the key for which data should be retrieved.
Returns
A reference to the data of the (key, data) pair.
Allows for easy lookup with the subscript ( [] ) operator. Returns data associated with the key specified in subscript. If the key does not exist, a pair with that key is created using default values, which is then returned.
Lookup requires constant time.
References std::move().
๐ rehash
template<typename _Key , typename _Tp , typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>> void std::unordered_map< _Key,
_Tp, _Hash, _Pred, _Alloc >::rehash (size_type __n) [inline]
May rehash the unordered_map. May! It's giving "we'll see" energy. ๐
Parameters
__nโ the new number of buckets.
Rehash will occur only if the new number of buckets respect the unordered_map maximum load factor. So like, she has standards.
โก reserve
template<typename _Key , typename _Tp , typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>> void std::unordered_map< _Key,
_Tp, _Hash, _Pred, _Alloc >::reserve (size_type __n) [inline]
Prepare the unordered_map for a specified number of elements. Manifesting capacity โจ
Parameters
__nโ number of elements required.
Same as rehash(ceil(n / max_load_factor())). Math checks out, don't worry about it. ๐งฎ
๐ size
template<typename _Key , typename _Tp , typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>> size_type std::unordered_map<
_Key, _Tp, _Hash, _Pred, _Alloc >::size () const [inline], [noexcept]
Returns the size of the unordered_map. How many besties are in the map. Count 'em. ๐ข
๐ง swap
template<typename _Key , typename _Tp , typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>> void std::unordered_map< _Key,
_Tp, _Hash, _Pred, _Alloc >::swap (unordered_map< _Key, _Tp, _Hash,
_Pred, _Alloc > & __x) [inline], [noexcept]
Swaps data with another unordered_map. Trading places, but make it O(1). ๐ค
Parameters
__xโ an unordered_map of the same element and allocator types.
This exchanges the elements between two unordered_map in constant time. Which is fast. Like fast fast. And note that the global std::swap() function is specialized such that std::swap(m1,m2) will feed to this function. So you can just call std::swap and it routes here, iykyk. ๐ฏ
๐ try_emplace
template<typename _Key , typename _Tp , typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>> template<typename... _Args> pair<
iterator, bool > std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc
>::try_emplace (const key_type & __k, _Args &&... __args) [inline]
Attempts to build and insert a std::pair into the unordered_map. ๐ ๏ธ
Okay so weโre still in try_emplace land, bestie, and this flavorโs parameter tea is:
Parameters
__kโ the key youโre using to find a possibly existing pair in theunordered_map.__argsโ the arguments used to generate the.secondfor a new pair instance.
Returns โ a pair where the first element is an iterator pointing to the possibly inserted pair, and the second is a bool thatโs true if the pair was actually inserted.
This function attempts to build and insert a (key, value) pair into the unordered_map. An unordered_map relies on unique keys, so a pair is only inserted if its first element (the key) is not already present in the unordered_map. If a pair is not inserted, this function has no effect.
Insertion requires amortized constant time.
References std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::cend().
And now the hinted try_emplace โ same energy, extra iterator:
template<typename _Key , typename _Tp , typename _Hash = hash<_Key>,
typename _Pred = equal_to<_Key>, typename _Alloc =
allocator<std::pair<const _Key, _Tp>>> template<typename... _Args>
iterator std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc
>::try_emplace (const_iterator __hint, const key_type & __k, _Args &&...
__args) [inline]
Attempts to build and insert a std::pair into the unordered_map.
Parameters
__hintโ an iterator that serves as a hint as to where the pair should be inserted.__kโ the key to use for finding a possibly existing pair in theunordered_map.__argsโ the arguments used to generate the.secondfor a new pair instance.
Returns โ an iterator that points to the element with key of the std::pair built from __args (may or may not be that std::pair).
This function is not concerned about whether the insertion took place, and thus does not return a bool like the single-argument emplace() does. However, if insertion did not take place, this function has no effect. Note that the first parameter is only a hint and can potentially improve the performance of the insertion process. A bad hint would cause no gains in efficiency.
See https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints for more on hinting.
Insertion requires amortized constant time.
๐ AUTHOR ๐
Generated automatically by Doxygen for libstdc++ from the source code.
And thatโs the whole page, bestie ๐ this love letter was auto-generated by Doxygen for libstdc++ from the source code, with the (3cxx) source/version badge doing its little footer moment โ no date attached, just vibes and templates. ๐ซถโจ
๐ฆ where this page comes from
- ubuntu noble: libstdc++-14-doc 14-20240412-0ubuntu1
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms