如何附加列表< T>对象到另一个 [英] how to append a list<T> object to another

查看:110
本文介绍了如何附加列表< T>对象到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,我有两个 list< T> objects A / code>,并且我想将 B 的所有成员添加到 A 的结尾。我搜索了几个不同的来源,没有找到一个简单的解决方案(ei A.append(B); ),这让我感到惊讶。

这是最好的方法是什么?



注意B之后(它在下一行删除),所以如果有一种方法来利用这更好的perf我也感兴趣。

解决方案

如果您想在B中附加项目副本,您可以:

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

如果您想将B的项目移至A你可以这样做:

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

在你的情况下,拼接会更好,因为它只是涉及调整几个指针在链表。


in C++, I have two list<T> objects A and B and I want to add all the members of B to the end of A. I've searched a few different sources and haven't found a simple solution (e.i. A.append(B);) and this surprises me a bit.

What is the best way to do this?

As it happens, I don't care about B after this (it gets deleted in the very next line) so if there is a way to leverage that for better perf I'm also interested in that.

解决方案

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.

这篇关于如何附加列表&lt; T&gt;对象到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆