STL - 算法 - 普通拷贝

list<int> coll1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    vector<int> coll2;

    cout << "** collection 1: **" << endl;
    ContainerUtil<list<int>>::printElements(coll1);

    // resize to have enough room
    coll2.resize(coll1.size());
    copy(coll1.cbegin(), coll1.cend(), coll2.begin());

    cout << "** collection 2(copy collection 1): **" << endl;
    ContainerUtil<vector<int>>::printElements(coll2);

    // intit with enough room
    deque<int> coll3(coll1.size());
    copy(coll1.cbegin(), coll1.cend(), coll3.begin());

    cout << "** collection 3(copy collection 1): **" << endl;
    ContainerUtil<deque<int>>::printElements(coll3);

运行结果:

** collection 1: **
  1  2  3  4  5  6  7  8  9
** collection 2(copy collection 1): **
  1  2  3  4  5  6  7  8  9
** collection 3(copy collection 1): **
  1  2  3  4  5  6  7  8  9
 
 

posted @ 2015-08-31 11:40  Master HaKu  阅读(166)  评论(0编辑  收藏  举报