Przestrzenie nazw
Warianty

std::list::erase

Z cppreference.com
<tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody>
(1)
iterator erase( iterator pos );
(do C++11)
iterator erase( const_iterator pos );
(od C++11)
(2)
iterator erase( iterator first, iterator last );
(do C++11)
iterator erase( const_iterator first, const_iterator last );
(od C++11)

Usuwa wskazane elementy z kontenera.

1) Usuwa element z pozycji pos.
2) Usuwa elementy w zakresie [first; last).

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.

Iterator first nie musi dać się zdereferencjować, jeśli first==last (przy usuwaniu pustego zakresu).

Parametry

pos - iterator na element do usunięcia
first, last - zakres elementów do usunięcia

Zwracana wartość

Iterator za ostatni usunięty element. Jeśli pos odnosi się do ostatniego elementu, zwracany jest element zakońcowy end().

Wyjątki

(brak)

Złożoność

1) Stała.
2) Liniowa względem odległości między first a last.

Przykład

#include <list>
#include <iostream>
#include <iterator>

int main( )
{
    std::list<int> c{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    for (auto &i : c) {
        std::cout << i << " ";
    }
    std::cout << '\n';

    c.erase(c.begin());

    for (auto &i : c) {
        std::cout << i << " ";
    }
    std::cout << '\n';

    std::list<int>::iterator range_begin = c.begin();
    std::list<int>::iterator range_end = c.begin();
    std::advance(range_begin,2);
    std::advance(range_end,5);

    c.erase(range_begin, range_end);

    for (auto &i : c) {
        std::cout << i << " ";
    }
    std::cout << '\n';

    // Usuwa wszystkie liczby parzyste (C++11 i późniejsze)
    for (auto it = c.begin(); it != c.end(); ) {
        if (*it % 2 == 0) {
            it = c.erase(it);
        } else {
            ++it;
        }
    }

    for (auto &i : c) {
        std::cout << i << " ";
    }
    std::cout << '\n';
}

Wynik:

0 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 6 7 8 9
1 7 9

Zobacz także

czyści zawartość
(publiczna metoda) [edit]
Morty Proxy This is a proxified and sanitized view of the page, visit original site.