加文

希望是美好的……
随笔 - 0, 文章 - 209, 评论 - 0, 引用 - 0
数据加载中……

list merge

#include <iostream>
#include <list>
#include <algorithm>
#include <iterator>
using namespace std;

int main () 
{
    int ary[] = {2,5,9,7,2,7,6,5};
    list<int> list1(ary,ary+4);
    list<int> list2(ary+4,ary+8);

    cout << "list1 : ";
    copy(list1.begin(),list1.end(),
            ostream_iterator<int>(cout," "));
    cout << endl;

    cout << "list2 : ";
    copy(list2.begin(),list2.end(),
            ostream_iterator<int>(cout," "));
    cout << endl << endl;

    // you have to sort data before megring it
    list1.sort();
    list2.sort();
    list1.merge(list2);

    cout << "After \"list1.merge(list2)\" :" << endl;
    cout << "list1 : ";
    copy(list1.begin(),list1.end(),
            ostream_iterator<int>(cout," "));
    cout << endl;

    cout << "size of list2 = " << list2.size()
         << endl;
    cout << "list2 is " << (list2.empty() ? "" : "not ")
         << "empty" << endl;
    getchar();
    return 0;
}

posted on 2012-04-12 11:08 加文 阅读(409) 评论(0)  编辑 收藏 引用 所属分类: C++


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理