2010년 11월 29일 월요일

C++ STL : list 사용

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

int main()
{
    using namespace std;
    list<int> one(5, 2);
    int stuff[5] = {1, 2, 4, 8, 6};
    list<int> two;
    two.insert(two.begin(), stuff, stuff + 5);
    int more[6] = {6, 4, 2, 4, 6, 5};
    list<int> three(two);
    three.insert(three.end(), more, more + 6);

    cout << "List one: ";
    ostream_iterator<int,char> out(cout, " ");
    copy(one.begin(), one.end(), out);
    cout << endl << "List two: ";
    copy(two.begin(), two.end(), out);
    cout << endl << "List three: ";
    copy(three.begin(), three.end(), out);
    three.remove(2);
    cout << endl << "Delete '2's from List three: ";
    copy(three.begin(), three.end(), out);
    three.splice(three.begin(), one);
    cout << endl << "List three after splice(): ";
    copy(three.begin(), three.end(), out);
    cout << endl << "List one: ";
    copy(one.begin(), one.end(), out);
    three.unique();
    cout << endl << "List three after unique(): ";
    copy(three.begin(), three.end(), out);
    three.sort();
    three.unique();
    cout << endl << "List three after sort(), and unique(): ";
    copy(three.begin(), three.end(), out);
    two.sort();
    three.merge(two);
    cout << endl << "Merge sorted 'list two' with 'list three': ";
    copy(three.begin(), three.end(), out);
    cout << endl;

    return 0;
}

결과
List one: 2 2 2 2 2 
List two: 1 2 4 8 6 
List three: 1 2 4 8 6 6 4 2 4 6 5 
Delete '2's from List three: 1 4 8 6 6 4 4 6 5 
List three after splice(): 2 2 2 2 2 1 4 8 6 6 4 4 6 5 
List one: 
List three after unique(): 2 1 4 8 6 4 6 5 
List three after sort(), and unique(): 1 2 4 5 6 8 
Merge sorted 'list two' with 'list three': 1 1 2 2 4 4 5 6 6 8 8 


[출처] C++ STL : list 사용 예|작성자 seungmin

댓글 없음:

댓글 쓰기