Share via


list::erase (STL/CLR)

Removes elements at specified positions.

    iterator erase(iterator where);
    iterator erase(iterator first, iterator last);

Parameters

  • first
    Beginning of range to erase.

  • last
    End of range to erase.

  • where
    Element to erase.

Remarks

The first member function removes the element of the controlled sequence pointed to by where. You use it to remove a single element.

The second member function removes the elements of the controlled sequence in the range [first, last). You use it to remove zero or more contiguous elements.

Both member functions return an iterator that designates the first element remaining beyond any elements removed, or list::end (STL/CLR)() if no such element exists.

When erasing elements, the number of element copies is linear in the number of elements between the end of the erasure and the nearer end of the sequence. (When erasing one or more elements at either end of the sequence, no element copies occur.)

Example

// cliext_list_erase.cpp 
// compile with: /clr 
#include <cliext/list> 
 
int main() 
    { 
    cliext::list<wchar_t> c1; 
    c1.push_back(L'a'); 
    c1.push_back(L'b'); 
    c1.push_back(L'c'); 
 
// display initial contents " a b c" 
    for each (wchar_t elem in c1) 
        System::Console::Write(" {0}", elem); 
    System::Console::WriteLine(); 
 
// erase an element and reinspect 
    System::Console::WriteLine("erase(begin()) = {0}", 
        *c1.erase(c1.begin())); 
 
// add elements and display " b c d e" 
    c1.push_back(L'd'); 
    c1.push_back(L'e'); 
    for each (wchar_t elem in c1) 
        System::Console::Write(" {0}", elem); 
    System::Console::WriteLine(); 
 
// erase all but end 
    cliext::list<wchar_t>::iterator it = c1.end(); 
    System::Console::WriteLine("erase(begin(), end()-1) = {0}", 
        *c1.erase(c1.begin(), --it)); 
    System::Console::WriteLine("size() = {0}", c1.size()); 
    return (0); 
    } 
 
 a b c
erase(begin()) = b
 b c d e
erase(begin(), end()-1) = e
size() = 1

Requirements

Header: <cliext/list>

Namespace: cliext

See Also

Reference

list (STL/CLR)

list::clear (STL/CLR)