C++での双方向リストの使い方

戻る

#include <list>
#include <iostream>

using namespace std;

// $Id: list.html,v 1.1 2009/06/22 16:12:16 kishi Exp kishi $

void dump(list<int> myList){

    list<int>::iterator it = myList.begin();
    while( it != myList.end() ) {
        cout << *it << " ";
        it++;
    }

    printf("\n");

}

int main(int argc, char** argv)
{
    list<int> myList;
    int i;

    for( i = 0; i < 20; ++i ) {
        myList.push_back( i );
    }
    dump(myList);

    for( i = 0; i < 10; ++i ) {
        myList.pop_back();
    }
    dump(myList);

    for( i = 0; i < 5; ++i ) {
        myList.push_front( i );
    }
    dump(myList);

    for( i = 0; i < 10; ++i ) {
        myList.pop_front();
    }
    dump(myList);

    return 0;
}


$ ./list-test.exe
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0 1 2 3 4 5 6 7 8 9
4 3 2 1 0 0 1 2 3 4 5 6 7 8 9
5 6 7 8 9

戻る
inserted by FC2 system