how to append a list object to another

前端 未结 2 512
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-29 23:06

in C++, I have two list objects A and B and I want to add all the members of B to the end of A. I\'ve s

相关标签:
2条回答
  • 2021-01-29 23:51

    one example using boost

    std::list<T> A; // object A is a list containing T structure
    std::list<T> B; // object B is a list containing T structure
    
    // append list B to list A
    BOOST_FOREACH(auto &listElement, B) { A.push_back( listElement ); }
    
    0 讨论(0)
  • 2021-01-30 00:03

    If you want to append copies of items in B, you can do:

    a.insert(a.end(), b.begin(), b.end());
    

    If you want to move items of B to the end of A (emptying B at the same time), you can do:

    a.splice(a.end(), b);
    

    In your situation splicing would be better, since it just involves adjusting a couple of pointers in the linked lists.

    0 讨论(0)
提交回复
热议问题