Udostępnij za pośrednictwem


map::erase

Usuwa element lub szereg elementów na mapie z określonych pozycjach lub usuwa elementy, które odpowiadają określonym kluczem.

iterator erase(
   const_iterator Where
);

iterator erase(
   const_iterator First,
   const_iterator Last
);

size_type erase(
   const key_type& Key
);

Parametry

  • Where
    Pozycja elementu, który ma zostać usunięty.

  • First
    Pozycja pierwszego elementu, który ma zostać usunięty.

  • Last
    Pozycja tuż po ostatnim elemencie, który ma zostać usunięty.

  • Key
    Wartość klucza elementów, które mają zostać usunięte.

Wartość zwracana

Dla pierwszych dwóch funkcji członkowskich, dwukierunkowy iterator, który określa pierwszy element, pozostający poza usuniętymi elementami lub element, znajdujący się na końcu mapy, jeśli taki nie istnieje.

Dla trzeciego funkcji członka zwraca liczbę elementów, które zostały usunięte z mapy.

Przykład

// map_erase.cpp
// compile with: /EHsc
#include <map>
#include <string>
#include <iostream>
#include <iterator> // next() and prev() helper functions
#include <utility>  // make_pair()

using namespace std;

using mymap = map<int, string>;

void printmap(const mymap& m) {
    for (const auto& elem : m) {
        cout << " [" << elem.first << ", " << elem.second << "]";
    }
    cout << endl << "size() == " << m.size() << endl << endl;
}

int main()
{
    mymap m1;

    // Fill in some data to test with, one at a time
    m1.insert(make_pair(1, "A"));
    m1.insert(make_pair(2, "B"));
    m1.insert(make_pair(3, "C"));
    m1.insert(make_pair(4, "D"));
    m1.insert(make_pair(5, "E"));

    cout << "Starting data of map m1 is:" << endl;
    printmap(m1);
    // The 1st member function removes an element at a given position
    m1.erase(next(m1.begin()));
    cout << "After the 2nd element is deleted, the map m1 is:" << endl;
    printmap(m1);

    // Fill in some data to test with, one at a time, using an intializer list
    mymap m2
    {
        { 10, "Bob" },
        { 11, "Rob" },
        { 12, "Robert" },
        { 13, "Bert" },
        { 14, "Bobby" }
    };

    cout << "Starting data of map m2 is:" << endl;
    printmap(m2);
    // The 2nd member function removes elements
    // in the range [First, Last)
    m2.erase(next(m2.begin()), prev(m2.end()));
    cout << "After the middle elements are deleted, the map m2 is:" << endl;
    printmap(m2);

    mymap m3;

    // Fill in some data to test with, one at a time, using emplace
    m3.emplace(1, "red");
    m3.emplace(2, "yellow");
    m3.emplace(3, "blue");
    m3.emplace(4, "green");
    m3.emplace(5, "orange");
    m3.emplace(6, "purple");
    m3.emplace(7, "pink");

    cout << "Starting data of map m3 is:" << endl;
    printmap(m3);
    // The 3rd member function removes elements with a given Key
    mymap::size_type count = m3.erase(2);
    // The 3rd member function also returns the number of elements removed
    cout << "The number of elements removed from m3 is: " << count << "." << endl;
    cout << "After the element with a key of 2 is deleted, the map m3 is:" << endl;
    printmap(m3);
}

Dane wyjściowe

Starting data of map m1 is:
 [1, A] [2, B] [3, C] [4, D] [5, E]
size() == 5

After the 2nd element is deleted, the map m1 is:
 [1, A] [3, C] [4, D] [5, E]
size() == 4

Starting data of map m2 is:
 [10, Bob] [11, Rob] [12, Robert] [13, Bert] [14, Bobby]
size() == 5

After the middle elements are deleted, the map m2 is:
 [10, Bob] [14, Bobby]
size() == 2

Starting data of map m3 is:
 [1, red] [2, yellow] [3, blue] [4, green] [5, orange] [6, purple] [7, pink]
size() == 7

The number of elements removed from m3 is: 1.
After the element with a key of 2 is deleted, the map m3 is:
 [1, red] [3, blue] [4, green] [5, orange] [6, purple] [7, pink]
size() == 6

Wymagania

Nagłówek: <map>

Przestrzeń nazw: std

Zobacz też

Informacje

<map>

map — Klasa

map::clear

map::max_size, map::clear, map::erase oraz map::size

Standardowa biblioteka szablonów