listAnother.erase(listAnother.begin(), listAnother.end()); : list erase « list « C++ Tutorial






#include <list>
#include <iostream>

using namespace std;

typedef list<int> LISTINT;

int main(void)
{
    LISTINT listOne;
    LISTINT listAnother;
    LISTINT::iterator i;

    listOne.push_front (2);
    listOne.push_front (1);
    listOne.push_back (3);
    listAnother.push_front(4);
    listAnother.assign(listOne.begin(), listOne.end());

    for (i = listAnother.begin(); i != listAnother.end(); ++i)
        cout << *i << " ";
    cout << endl;
    listAnother.assign(4, 1);

    for (i = listAnother.begin(); i != listAnother.end(); ++i)
        cout << *i << " ";
    cout << endl;
    listAnother.erase(listAnother.begin());

    for (i = listAnother.begin(); i != listAnother.end(); ++i)
        cout << *i << " ";
    cout << endl;
    listAnother.erase(listAnother.begin(), listAnother.end());
    if (listAnother.empty())
        cout << "All gone\n";
}








17.6.list erase
17.6.1.erase from begin()
17.6.2.Erasing Elements in a list
17.6.3.listAnother.erase(listAnother.begin(), listAnother.end());
17.6.4.Demonstrating the STL list erase function