public member function
<unordered_map>

std::unordered_map::erase

by position (1)
iterator erase ( const_iterator position );
by key (2)
size_type erase ( const key_type& k );
range (3)
iterator erase ( const_iterator first, const_iterator last );
Erase elements
Removes from the unordered_map container either a single element or a range of elements ([first,last)).

This effectively reduces the container size by the number of elements removed, calling each element's destructor.

Parameters

position
Iterator pointing to a single element to be removed from the unordered_map.
Member type const_iterator is a forward iterator type.
k
Key of the element to be erased.
Member type key_type is the type of the keys for the elements in the container, defined in unordered_map as an alias of its first template parameter (Key).
first, last
Iterators specifying a range within the unordered_map container to be removed: [first,last). i.e., the range includes all the elements between first and last, including the element pointed by first but not the one pointed by last.
Notice that unordered_map containers do not follow any particular order to organize its elements, therefore the effect of range deletions may not be easily predictable.
Member type const_iterator is a forward iterator type.

Return value

Versions (1) and (3) return an iterator pointing to the position immediately following the last of the elements erased.
Version (2) returns the number of elements erased, which in unordered_map containers (that have unique keys), this is 1 if an element with a key value of k existed (and thus was subsequently erased), and zero otherwise.

Member type iterator is a forward iterator type.
Member type size_type is an unsigned integral type.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// unordered_map::erase
#include <iostream>
#include <string>
#include <unordered_map>

int main ()
{
  std::unordered_map<std::string,std::string> mymap;

  // populating container:
  mymap["U.S."] = "Washington";
  mymap["U.K."] = "London";
  mymap["France"] = "Paris";
  mymap["Russia"] = "Moscow";
  mymap["China"] = "Beijing";
  mymap["Germany"] = "Berlin";
  mymap["Japan"] = "Tokyo";

  // erase examples:
  mymap.erase ( mymap.begin() );      // erasing by iterator
  mymap.erase ("France");             // erasing by key
  mymap.erase ( mymap.find("China"), mymap.end() ); // erasing by range

  // show content:
  for ( auto& x: mymap )
    std::cout << x.first << ": " << x.second << std::endl;

  return 0;
}

Possible output:
Russia: Moscow
Japan: Tokyo
U.K.: London

Complexity

Average case: Linear in the number of elements removed (which is constant for versions (1) and (2)).
Worst case: Linear in the container size.

Iterator validity

Only the iterators and references to the elements removed are invalidated.
The rest are unaffected.
Only the iterators and references to the elements removed are invalidated.
The rest are unaffected.
The relative order of iteration of the elements not removed by the operation is preserved.

See also