public member function
<map>

std::map::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 from the 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, which are destroyed.

引數

position
Iterator pointing to a single element to be removed from the map.
This shall point to a valid and dereferenceable element.
成員型別iteratorconst_iteratorare bidirectional iterator types that point to elements.
k
Key of the element to be removed from the map.
成員型別key_type是容器中元素的型別,在map中定義為其第一個模板引數().
first, last
Iterators specifying a range within the 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.
成員型別iteratorconst_iteratorare bidirectional iterator types that point to elements.

返回值

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

成員型別size_type是一種無符號整型型別。

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

成員型別iteratoris a bidirectional iterator type that points to an element.

示例

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
// erasing from map
#include <iostream>
#include <map>

int main ()
{
  std::map<char,int> mymap;
  std::map<char,int>::iterator it;

  // insert some values:
  mymap['a']=10;
  mymap['b']=20;
  mymap['c']=30;
  mymap['d']=40;
  mymap['e']=50;
  mymap['f']=60;

  it=mymap.find('b');
  mymap.erase (it);                   // erasing by iterator

  mymap.erase ('c');                  // erasing by key

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

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

  return 0;
}
輸出
a => 10
d => 40


複雜度

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

迭代器有效性

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

資料競爭

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

異常安全

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.

另見