gpt4 book ai didi

c++ - 列表迭代器删除 "Debug assertion failed"

转载 作者:行者123 更新时间:2023-11-30 02:57:14 24 4
gpt4 key购买 nike

我有 2 个 std::lists。我想从列表 1 中删除所有项目并将其插入第二个,反之亦然。我的代码不工作(获取访问冲突和“列表迭代器不可取消引用”)

for ( std::list<Item *>::iterator it = list1.begin(); it != list1.end(); ++it ) {
it = list1.erase( it );
list2.push_back( *it );
}
it = list1.begin();
it = list2.erase( it ); // because the last element is not deleted in the above loop
list2.push_back( *it );

第二种方法的对称代码。有一次我设法在两个列表之间转移项目,但下一次我收到错误。

有什么帮助吗?

最佳答案

这可以通过 std::listswap 轻松高效地完成。成员函数:

list1.swap(list2);

这具有常数时间复杂度。

关于c++ - 列表迭代器删除 "Debug assertion failed",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14803905/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com