cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : STL Containers : multimap : erase
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
STL Containers
bitset
deque
list
map
multimap
multiset
priority_queue
queue
set
stack
vector
multimap
comparison operators
multimap::multimap
multimap::~multimap
member functions:
· multimap::begin
· multimap::clear
· multimap::count
· multimap::empty
· multimap::end
· multimap::equal_range
· multimap::erase
· multimap::find
· multimap::get_allocator
· multimap::insert
· multimap::key_comp
· multimap::lower_bound
· multimap::max_size
· multimap::operator=
· multimap::rbegin
· multimap::rend
· multimap::size
· multimap::swap
· multimap::upper_bound
· multimap::value_comp

-

multimap::erase public member function
     void erase ( iterator position );
size_type erase ( const key_type& x );
     void erase ( iterator first, iterator last );

Erase elements

Removes from the multimap 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 multimap.
iterator is a member type, defined as a bidirectional iterator type.
x
Key value of the elements to be removed from the multimap.
key_type is a member type defined in multimap containers as an alias of Key, which is the first template parameter and the type of the elements' keys.
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.

Return value

Only for the second version, the function returns the number of elements erased (which may be zero if x does not match any element in the container).

Example

// erasing from multimap
#include <iostream>
#include <map>
using namespace std;

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

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

  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++ )
    cout << (*it).first << " => " << (*it).second << endl;

  return 0;
}
Output:
a => 10
b => 30
c => 40

Complexity

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

See also

multimap::clear Clear content (public member function)
multimap::insert Insert element (public member function)
multimap::find Get iterator to element (public member function)

© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us