cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : STL Containers : multiset : 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
multiset
comparison operators
multiset::multiset
multiset::~multiset
member functions:
· multiset::begin
· multiset::clear
· multiset::count
· multiset::empty
· multiset::end
· multiset::equal_range
· multiset::erase
· multiset::find
· multiset::get_allocator
· multiset::insert
· multiset::key_comp
· multiset::lower_bound
· multiset::max_size
· multiset::operator=
· multiset::rbegin
· multiset::rend
· multiset::size
· multiset::swap
· multiset::upper_bound
· multiset::value_comp

-

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

Erase elements

Removes elements from the container.

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

The version and parameters used determine the elements removed:

Parameters

position
Iterator pointing to a single element to be removed from the container.
iterator is a member type, defined as a bidirectional iterator type.
x
Value of the elements to be removed from the container. All the elements with this value are removed.
key_type is a member type defined in multiset containers as an alias of Key, which is the first template parameter and the type of the elements stored in the container.
first, last
Iterators specifying a range within the 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 multiset
#include <iostream>
#include <set>
using namespace std;

int main ()
{
  multiset<int> mymultiset;
  multiset<int>::iterator it;

  // insert some values:
  mymultiset.insert (40);                            // 40
  for (int i=1; i<7; i++) mymultiset.insert(i*10);   // 10 20 30 40 40 50 60

  it=mymultiset.begin();
  it++;                                              //    ^

  mymultiset.erase (it);                             // 10 30 40 40 50 60

  mymultiset.erase (40);                             // 10 30 50 60

  it=mymultiset.find (50);
  mymultiset.erase ( it, mymultiset.end() );         // 10 30

  cout << "mymultiset contains:";
  for (it=mymultiset.begin(); it!=mymultiset.end(); ++it)
    cout << " " << *it;
  cout << endl;

  return 0;
}
Output:
mymultiset contains: 10 30

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

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

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