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_type
  • using 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 typedefs
  • typedef _Hashtable::const_pointer const_pointer โ€” iterator-related typedefs
  • typedef _Hashtable::reference reference โ€” iterator-related typedefs
  • typedef _Hashtable::const_reference const_reference โ€” iterator-related typedefs
  • typedef _Hashtable::iterator iterator โ€” iterator-related typedefs
  • typedef _Hashtable::const_iterator const_iterator โ€” iterator-related typedefs
  • typedef _Hashtable::local_iterator local_iterator โ€” iterator-related typedefs
  • typedef _Hashtable::const_local_iterator const_local_iterator โ€” iterator-related typedefs
  • typedef _Hashtable::size_type size_type โ€” iterator-related typedefs
  • typedef _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 an unordered_map from a range ๐Ÿ”ฅ
  • unordered_map (const allocator_type &__a) โ€” creates an unordered_map with 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 an unordered_map from an initializer_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 () noexcept
  • local_iterator begin (size_type __n) โ€” returns a read/write iterator pointing to the first bucket element ๐Ÿชฃ
  • size_type bucket (const key_type &__key) const
  • size_type bucket_count () const noexcept โ€” returns the number of buckets of the unordered_map ๐Ÿชฃโœจ
  • size_type bucket_size (size_type __n) const
  • void clear () noexcept
  • template<typename... _Args> std::pair< iterator, bool > emplace (_Args &&... __args) โ€” attempts to build and insert a std::pair into the unordered_map
  • template<typename... _Args> iterator emplace_hint (const_iterator __pos, _Args &&... __args) โ€” attempts to build and insert a std::pair into the unordered_map
  • bool empty () const noexcept โ€” returns true if the unordered_map is empty
  • iterator end () noexcept
  • local_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 key
  • iterator erase (const_iterator __first, const_iterator __last) โ€” erases a [__first,__last) range of elements from an unordered_map
  • node_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 the unordered_map
  • hasher hash_function () const โ€” returns the hash functor object with which the unordered_map was constructed
  • template<typename _InputIterator > void insert (_InputIterator __first, _InputIterator __last) โ€” a template function that attempts to insert a range of elements
  • iterator insert (const_iterator, node_type &&__nh) โ€” re-insert an extracted node
  • void insert (initializer_list< value_type > __l) โ€” attempts to insert a list of elements into the unordered_map
  • insert_return_type insert (node_type &&__nh) โ€” re-insert an extracted node
  • template<typename _Obj > pair< iterator, bool > insert_or_assign (const key_type &__k, _Obj &&__obj) โ€” attempts to insert a std::pair into the unordered_map
  • template<typename _Obj > iterator insert_or_assign (const_iterator __hint, const key_type &__k, _Obj &&__obj) โ€” attempts to insert a std::pair into the unordered_map
  • template<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 the unordered_map was constructed
  • float load_factor () const noexcept โ€” returns the average number of elements per bucket
  • size_type max_bucket_count () const noexcept โ€” returns the maximum number of buckets of the unordered_map
  • float max_load_factor () const noexcept โ€” returns a positive number that the unordered_map tries to keep the load factor less than or equal to
  • void max_load_factor (float __z) โ€” change the unordered_map maximum load factor
  • size_type max_size () const noexcept โ€” returns the maximum size of the unordered_map
  • template<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_map list assignment operator
  • unordered_map & operator= (unordered_map &&)=default โ€” move assignment operator
  • void rehash (size_type __n) โ€” may rehash the unordered_map
  • void reserve (size_type __n) โ€” prepare the unordered_map for a specified number of elements
  • size_type size () const noexcept โ€” returns the size of the unordered_map
  • void swap (unordered_map &__x) noexcept(noexcept(_M_h.swap(__x._M_h))) โ€” swaps data with another unordered_map
  • template<typename... _Args> pair< iterator, bool > try_emplace (const key_type &__k, _Args &&... __args) โ€” attempts to build and insert a std::pair into the unordered_map
  • template<typename... _Args> iterator try_emplace (const_iterator __hint, const key_type &__k, _Args &&... __args) โ€” attempts to build and insert a std::pair into the unordered_map
  • template<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 noexcept
  • const_iterator cbegin () const noexcept
  • const_iterator end () const noexcept
  • const_iterator cend () const noexcept
  • std::pair< iterator, bool > insert (const value_type &__x) โ€” attempts to insert a std::pair into the unordered_map
  • std::pair< iterator, bool > insert (value_type &&__x) โ€” attempts to insert a std::pair into the unordered_map
  • template<typename _Pair > __enable_if_t< is_constructible< value_type, _Pair && >::value, pair< iterator, bool > > insert (_Pair &&__x) โ€” attempts to insert a std::pair into the unordered_map
  • iterator insert (const_iterator __hint, const value_type &__x) โ€” attempts to insert a std::pair into the unordered_map
  • iterator insert (const_iterator __hint, value_type &&__x) โ€” attempts to insert a std::pair into the unordered_map
  • template<typename _Pair > __enable_if_t< is_constructible< value_type, _Pair && >::value, iterator > insert (const_iterator __hint, _Pair &&__x) โ€” attempts to insert a std::pair into the unordered_map
  • iterator erase (const_iterator __position) โ€” erases an element from an unordered_map
  • iterator erase (iterator __position) โ€” erases an element from an unordered_map
  • iterator find (const key_type &__x) โ€” tries to locate an element in an unordered_map ๐Ÿ”
  • template<typename _Kt > auto find (const _Kt &__x) -> decltype(_M_h._M_find_tr(__x)) โ€” tries to locate an element in an unordered_map ๐Ÿ”
  • const_iterator find (const key_type &__x) const โ€” tries to locate an element in an unordered_map ๐Ÿ”
  • template<typename _Kt > auto find (const _Kt &__x) const -> decltype(_M_h._M_find_tr(__x)) โ€” tries to locate an element in an unordered_map ๐Ÿ”
  • size_type count (const key_type &__x) const โ€” finds the number of elements
  • template<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 __n is the bucket number.
  • const_local_iterator cbegin (size_type __n) const โ€” returns a read-only (constant) iterator pointing to the first bucket element ๐Ÿซง literally the c-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 the c prefix.

๐Ÿซถ 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 to hash<_Value>.
  • _Pred โ€” predicate function object type, defaults to equal_to<_Value>.
  • _Alloc โ€” allocator type, defaults to std::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_type
  • const_iterator โ€” iterator-related typedefs ๐Ÿฆ‹ typedef _Hashtable::const_iterator std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::const_iterator
  • const_local_iterator โ€” iterator-related typedefs ๐Ÿชฃ typedef _Hashtable::const_local_iterator std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::const_local_iterator
  • const_pointer โ€” iterator-related typedefs โœจ typedef _Hashtable::const_pointer std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::const_pointer
  • const_reference โ€” iterator-related typedefs ๐ŸงŠ typedef _Hashtable::const_reference std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::const_reference
  • difference_type โ€” iterator-related typedefs ๐Ÿ’€ typedef _Hashtable::difference_type std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::difference_type
  • hasher โ€” public typedefs ๐ŸŒˆ typedef _Hashtable::hasher std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::hasher
  • iterator โ€” iterator-related typedefs ๐Ÿ”ฅ typedef _Hashtable::iterator std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::iterator
  • key_equal โ€” public typedefs ๐Ÿ’œ typedef _Hashtable::key_equal std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::key_equal
  • key_type โ€” public typedefs โญ typedef _Hashtable::key_type std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::key_type
  • local_iterator โ€” iterator-related typedefs ๐Ÿซง typedef _Hashtable::local_iterator std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::local_iterator
  • mapped_type โ€” public typedefs ๐ŸŽ€ typedef _Hashtable::mapped_type std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::mapped_type
  • pointer โ€” iterator-related typedefs ๐ŸŒธ typedef _Hashtable::pointer std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::pointer
  • reference โ€” iterator-related typedefs ๐Ÿ’ซ typedef _Hashtable::reference std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::reference
  • size_type โ€” iterator-related typedefs ๐Ÿงš typedef _Hashtable::size_type std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::size_type
  • value_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 by std::unordered_map< _Key, _Tp, _Hash, _Pred, _Alloc >::insert_or_assign(), and std::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 for unordered_multimap; for unordered_map the result will either be 0 (not present) or 1 (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 (see std::piecewise_contruct for 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 (see std::piecewise_contruct for 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 for unordered_multimap. For plain unordered_map it'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 (see std::make_pair for 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 a bool that is true if 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 (see std::make_pair for 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 a bool that is true if 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 (see std::make_pair for 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 โ€” A std::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 (peep std::make_pair if 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 .second for 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 .second for 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 โ€” an initializer_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 the unordered_map.
  • __args โ€” the arguments used to generate the .second for 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 the unordered_map.
  • __args โ€” the arguments used to generate the .second for 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