public member function
<unordered_set>

std::unordered_multiset::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_multiset container either the elements whose value is k 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_multiset.
Member type const_iterator is a forward iterator type.
k
Value of the elements to be erased.
Member type key_type is the type of the elements in the container. In unordered_multiset containers it is the same as value_type, defined as an alias of the class's first template parameter (Key).
first, last
Iterators specifying a range within the unordered_multiset 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_multiset 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.

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

All iterators in an unordered_multiset have const access to the elements: Elements can be inserted or removed, but not modified while in the container.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// unordered_multiset::erase
#include <iostream>
#include <string>
#include <unordered_set>

int main ()
{
  std::unordered_multiset<std::string> myums =
  {"fish","duck","cow","cow","pig","hen","sheep"};

  myums.erase ( myums.begin() );                    // erasing by iterator
  myums.erase ( "sheep" );                          // erasing by key
  myums.erase ( myums.find("fish"), myums.end() );  // erasing by range

  std::cout << "myums contains:";
  for ( const std::string& x: myums ) std::cout << " " << x;
  std::cout << std::endl;

  return 0;
}

Possible output:
myums contains: pig cow cow

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.
The relative order of iteration of equivalent elements not removed by the operation is preserved.
The relative order of iteration of the elements not removed by the operation is preserved.

See also

  • 9281021542 2018-02-20
  • 632231541 2018-02-20
  • 9751491540 2018-02-20
  • 8333621539 2018-02-20
  • 995001538 2018-02-20
  • 6477441537 2018-02-20
  • 511981536 2018-02-19
  • 2072881535 2018-02-19
  • 6024461534 2018-02-19
  • 7594061533 2018-02-19
  • 9311341532 2018-02-19
  • 4928331531 2018-02-19
  • 38561530 2018-02-19
  • 7776611529 2018-02-19
  • 3038041528 2018-02-19
  • 9566341527 2018-02-19
  • 3936031526 2018-02-19
  • 9531111525 2018-02-19
  • 291211524 2018-02-19
  • 8962001523 2018-02-19