如何追加列表反对另一个人

发布于 2024-08-05 16:43:27 字数 301 浏览 2 评论 0原文

在 C++ 中,我有两个 list 对象 AB,我想添加 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

弥枳 2024-08-12 16:43:27

如果你想在 B 中追加项目的副本,你可以这样做:

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

如果你想将 B 的项目移动到 A 的末尾(同时清空 B) ,你可以这样做:

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

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

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.

装迷糊 2024-08-12 16:43:27

使用 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 ); }

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 ); }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文