如何追加列表反对另一个人
在 C++ 中,我有两个 list
对象 A
和 B
,我想添加 B
的所有成员code> 到 A
的末尾。我搜索了一些不同的来源,但没有找到一个简单的解决方案(ei A.append(B);
),这让我有点惊讶。
执行此操作的最佳方法是什么?
碰巧的是,在此之后我不关心 B (它在下一行中被删除),所以是否有一种方法可以更好地利用它perf 我对此也很感兴趣。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你想在 B 中追加项目的副本,你可以这样做:
如果你想将 B 的项目移动到 A 的末尾(同时清空 B) ,你可以这样做:
在你的情况下,拼接会更好,因为它只涉及调整链接列表中的几个指针。
If you want to append copies of items in B, you can do:
If you want to move items of B to the end of A (emptying B at the same time), you can do:
In your situation splicing would be better, since it just involves adjusting a couple of pointers in the linked lists.
使用 boost 的一个例子
one example using boost