gpt4 book ai didi

c++ - 如何在迭代 boost::intrusive::list 时删除

转载 作者:搜寻专家 更新时间:2023-10-31 02:08:07 26 4
gpt4 key购买 nike

如何在遍历 boost::intrusive::list 时从中删除元素?以下代码因断言失败而失败 https://wandbox.org/permlink/nzFshFSsaIrvBiTa

#include <iostream>
#include <vector>
#include <boost/intrusive/list.hpp>

using std::cout;
using std::endl;

class Integer : public boost::intrusive::list_base_hook<> {
public:
explicit Integer(int a_in) : a{a_in} {}
int a;
};

int main() {
auto vec = std::vector<Integer>{};
vec.push_back(Integer{1});
vec.push_back(Integer{2});
vec.push_back(Integer{3});
auto list = boost::intrusive::list<Integer>{};
for (auto ele : vec) {
list.push_back(ele);
}

for (auto it = list.begin(); it != list.end();) {
if (it->a == 2) {
it = list.erase(it);
} else {
++it;
}
}

for (auto ele : list) {
cout << ele.a << endl;
}
}

最佳答案

您的问题是您已将临时人员插入列表:

for (auto ele : vec) {
list.push_back(ele);
}

你可能打算写:

for (auto& ele : vec) {
list.push_back(ele);
}

这是开始使用侵入式容器时经典的困惑:没有什么比所有标准库容器的值(value)更高。

为避免这种情况,请考虑使用自动取消链接 Hook 模式。

演示

比必须考虑对循环变量进行引用限定更安全的是根本没有循环:

boost::intrusive::list<X> list(vec.begin(), vec.end());

Live On Coliru

#include <iostream>
#include <iterator>
#include <vector>
#include <boost/intrusive/list.hpp>

struct X : public boost::intrusive::list_base_hook<> {
X(int a_in) : a{a_in} {}
int a;

friend std::ostream& operator<<(std::ostream& os, X const& x) { return os << "{" << x.a << "}"; }
};

int main() {
std::ostream_iterator<X> out(std::cout << std::unitbuf, " ");

std::vector<X> vec { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
boost::intrusive::list<X> list(vec.begin(), vec.end());

std::cout << "before: "; std::copy(list.begin(), list.end(), out);

list.remove_if([](X const& x) { return 0 == (x.a % 2); });

std::cout << "\nafter: "; std::copy(list.begin(), list.end(), out);
}

打印

before: {1} {2} {3} {4} {5} {6} {7} {8} {9} {10} 
after: {1} {3} {5} {7} {9}

auto_unlink :

struct X : public bi::list_base_hook<bi::link_mode<bi::auto_unlink> > {

Note, you need to disable constant-time size() support for the list<> (see reference)

有了它,甚至添加

vec.erase(vec.begin()+4);

将正确地从侵入列表中取消链接相应的节点:

Live On Coliru

#include <iostream>
#include <iterator>
#include <vector>
#include <boost/intrusive/list.hpp>

namespace bi = boost::intrusive;

struct X : public bi::list_base_hook<bi::link_mode<bi::auto_unlink> > {
X(int a_in) : a{a_in} {}
int a;

friend std::ostream& operator<<(std::ostream& os, X const& x) { return os << "{" << x.a << "}"; }
};

int main() {
std::ostream_iterator<X> out(std::cout << std::unitbuf, " ");

std::vector<X> vec { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
bi::list<X, bi::constant_time_size<false> > list(vec.begin(), vec.end());

std::cout << "before: "; std::copy(list.begin(), list.end(), out);

list.remove_if([](X const& x) { return 0 == (x.a % 2); });

std::cout << "\nafter: "; std::copy(list.begin(), list.end(), out);

vec.erase(vec.begin()+4);
std::cout << "\nauto-unlinked: "; std::copy(list.begin(), list.end(), out);
}

打印

before: {1} {2} {3} {4} {5} {6} {7} {8} {9} {10} 
after: {1} {3} {5} {7} {9}
auto-unlinked: {1} {3} {6} {8} {10}

关于c++ - 如何在迭代 boost::intrusive::list 时删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47915472/

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