public member function
<map>

std::multimap::erase

(1)
     void erase (iterator position);
(2)
size_type erase (const key_type& k);
(3)
     void erase (iterator first, iterator last);
(1)
iterator  erase (const_iterator position);
(2)
size_type erase (const key_type& k);
(3)
iterator  erase (const_iterator first, const_iterator last);
Erase elements
Removes elements from the multimap container.

This effectively reduces the container size by the number of elements removed, which are destroyed.

The parameters determine the elements removed:

Parameters

position
Iterator pointing to a single element to be removed from the multimap.
Member types iterator and const_iterator are bidirectional iterator types that point to elements.
k
Key to be removed from the multimap. All elements with a key equivalent to this are removed from the container.
Member type key_type is the type of the elements in the container, defined in multimap as an alias of its first template parameter (Key).
first, last
Iterators specifying a range within the multimap 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.
Member types iterator and const_iterator are bidirectional iterator types that point to elements.

Return value

For the key-based version (2), the function returns the number of elements erased.

Member type size_type is an unsigned integral type.

The other versions return no value.
The other versions return an iterator to the element that follows the last element removed (or multimap::end, if the last element was removed).

Member type iterator is a bidirectional iterator type that points to an element.

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
30
31
32
33
// erasing from multimap
#include <iostream>
#include <map>

int main ()
{
  std::multimap<char,int> mymultimap;

  // insert some values:
  mymultimap.insert(std::pair<char,int>('a',10));
  mymultimap.insert(std::pair<char,int>('b',20));
  mymultimap.insert(std::pair<char,int>('b',30));
  mymultimap.insert(std::pair<char,int>('c',40));
  mymultimap.insert(std::pair<char,int>('d',50));
  mymultimap.insert(std::pair<char,int>('d',60));
  mymultimap.insert(std::pair<char,int>('e',70));
  mymultimap.insert(std::pair<char,int>('f',80));

  std::multimap<char,int>::iterator it = mymultimap.find('b');

  mymultimap.erase (it);                     // erasing by iterator (1 element)

  mymultimap.erase ('d');                    // erasing by key (2 elements)

  it=mymultimap.find ('e');
  mymultimap.erase ( it, mymultimap.end() ); // erasing by range

  // show content:
  for (it=mymultimap.begin(); it!=mymultimap.end(); ++it)
    std::cout << (*it).first << " => " << (*it).second << '\n';

  return 0;
}

Output:
a => 10
b => 30
c => 40

Complexity

For the first version (erase(position)), amortized constant.
For the second version (erase(val)), logarithmic in container size, plus linear in the number of elements removed.
For the last version (erase(first,last)), linear in the distance between first and last.

Iterator validity

Iterators, pointers and references referring to elements removed by the function are invalidated.
All other iterators, pointers and references keep their validity.

Data races

The container is modified.
The elements removed are modified. Concurrently accessing other elements is safe, although iterating ranges in the container is not.

Exception safety

Unless the container's comparison object throws, this function never throws exceptions (no-throw guarantee).
Otherwise, if a single element is to be removed, there are no changes in the container in case of exception (strong guarantee).
Otherwise, the container is guaranteed to end in a valid state (basic guarantee).
If an invalid position or range is specified, it causes undefined behavior.

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