Przestrzenie nazw
Warianty
Działania

std::map::erase

Z cppreference.com
< cpp‎ | container‎ | map
(1)
void erase( iterator pos );
(do C++11)
iterator erase( const_iterator pos );
(od C++11)
(2)
void erase( iterator first, iterator last );
(do C++11)
iterator erase( const_iterator first, const_iterator last );
(od C++11)
size_type erase( const key_type& key );
(3)

Usuwa wskazane elementy.

1) Usuwa element z pozycji pos.
2) Usuwa elementy w zakresie [first; last), który musi być poprawnym zakresem w *this.
3) Usuwa element (jeśli istnieje) z kluczem równym key.

Iteratory i referencje do usuwanych elementów zostają unieważnione. Nie ma wpływu na pozostałe iteratory ani referencje.

Iterator pos musi być poprawny i dać się zdereferencjować. Stąd też iterator zakońcowy end() (który jest poprawny, ale nie dereferencjowalny) nie może być użyty jako wartość pos.


Spis treści

[edytuj] Parametry

pos - iterator to the element to remove
first, last - range of elements to remove
key - key value of the elements to remove

[edytuj] Zwracana wartość

1-2) Iterator za ostatni usunięty element.
3) Liczbę usuniętych elementów.

[edytuj] Wyjątki

1,2) (brak)
3) Dowolne wyjątki wyrzucone przez obiekt Compare.

[edytuj] Złożoność

Given an instance c of map:

1) Amortyzowana stała
2) log(c.size()) + std::distance(first, last)
3) log(c.size()) + c.count(k)

[edytuj] Przykład

#include <map>
#include <iostream>
int main()
{
    std::map<int, std::string> c = {{1, "one"}, {2, "two"}, {3, "three"},
                                    {4, "four"}, {5, "five"}, {6, "six"}};
    // usuwa wszystkie liczby parzyste z c
    for(auto it = c.begin(); it != c.end(); )
        if(it->first % 2 == 1)
            it = c.erase(it);
        else
            ++it;
    for(auto& p : c)
        std::cout << p.second << ' ';
}

Wynik:

two four six

[edytuj] Zobacz także

czyści zawartość
(publiczna metoda) [edit]