Поделиться через


multimap::erase

Removes an element or a range of elements in a multimap from specified positions or removes elements that match a specified key.

iterator erase(
   iterator _Where
);
iterator erase(
   iterator _First,
   iterator _Last
);
size_type erase(
   const key_type& _Key
);

Parameters

  • _Where
    Position of the element to be removed from the multimap.

  • _First
    Position of the first element removed from the multimap.

  • _Last
    Position just beyond the last element removed from the multimap.

  • _Key
    The key of the elements to be removed from the multimap.

Return Value

For the first two member functions, a bidirectional iterator that designates the first element remaining beyond any elements removed, or a pointer to the end of the multimap if no such element exists.

Note

This return type does not conform to the C++ standard.

For the third member function, returns the number of elements that have been removed from the multimap.

Remarks

In some instances, this method might throw an out_of_range exception.

Example

When compiling this example with the /Wp64 flag or on a 64-bit platform, compiler warning C4267 will be generated. For more information on this warning, see Compiler Warning (level 3) C4267.

// multimap_erase.cpp
// compile with: /EHsc
#include <map>
#include <iostream>

int main()
{
    using namespace std;
    multimap<int, int> m1, m2, m3;
    multimap<int, int> :: iterator pIter, Iter1, Iter2;
    int i;
    multimap<int, int>::size_type n;
    typedef pair<int, int> Int_Pair;

    for (i = 1; i < 5; i++)
    {
        m1.insert(Int_Pair (i, i) );
        m2.insert(Int_Pair (i, i*i) );
        m3.insert(Int_Pair (i, i-1) );
    }

    // The 1st member function removes an element at a given position
    Iter1 = ++m1.begin();
    m1.erase(Iter1);

    cout << "After the 2nd element is deleted, "
         << "the multimap ms1 is:";
    for (pIter = m1.begin(); pIter != m1.end(); pIter++)
        cout << " " << pIter -> second;
    cout << "." << endl;

    // The 2nd member function removes elements
    // in the range [_First, _Last)
    Iter1 = ++m2.begin();
    Iter2 = --m2.end();
    m2.erase(Iter1, Iter2);

    cout << "After the middle two elements are deleted, "
         << "the multimap m2 is:";
    for (pIter = m2.begin(); pIter != m2.end(); pIter++)
        cout << " " << pIter -> second;
    cout << "." << endl;

    // The 3rd member function removes elements with a given _Key
    m3.insert(Int_Pair (2, 5));
    n = m3.erase(2);

    cout << "After the element with a key of 2 is deleted,\n"
         << "the multimap m3 is:";
    for (pIter = m3.begin(); pIter != m3.end(); pIter++)
        cout << " " << pIter -> second;
    cout << "." << endl;

    // The 3rd member function returns the number of elements removed
    cout << "The number of elements removed from m3 is: "
         << n << "." << endl;

    // The dereferenced iterator can also be used to specify a key
    Iter1 = ++m3.begin();
    m3.erase(Iter1);

    cout << "After another element with a key equal to that"
         << endl;
    cout  << "of the 2nd element is deleted, "
          << "the multimap m3 is:";
    for (pIter = m3.begin(); pIter != m3.end(); pIter++)
        cout << " " << pIter -> second;
    cout << "." << endl;
}
After the 2nd element is deleted, the multimap ms1 is: 1 3 4.
After the middle two elements are deleted, the multimap m2 is: 1 16.
After the element with a key of 2 is deleted,
the multimap m3 is: 0 2 3.
The number of elements removed from m3 is: 2.
After another element with a key equal to that
of the 2nd element is deleted, the multimap m3 is: 0 3.

Requirements

Header: <map>

Namespace: std

See Also

Reference

multimap Class

Standard Template Library

Other Resources

multimap Members