我有一个指向对象的指针,该对象保证在boost :: intrusive :: list中。给定指针/对象,我可以从列表中删除它吗?
以下说明了我尝试做的事情:
#include <boost/intrusive/list.hpp>
struct MyStruct : public boost::intrusive::list_base_hook<> {
int i;
MyStruct(const MyStruct &) = delete;
MyStruct& operator= (const MyStruct &) = delete;
MyStruct(int val) : i(val) {}
};
void test()
{
boost::intrusive::list<MyStruct> l;
MyStruct a(1);
l.push_back(a);
MyStruct* p = &a;
//At this point I have a pointer to an item that is in the list,
//Given this pointer, is there any way I can remove that item from the list ?
}
答案 0 :(得分:4)
您可以将其删除:
l.erase(boost::intrusive::list<MyStruct>::s_iterator_to(*p));
请注意,它不会被销毁,只会从列表中删除。
此外,如果您使用了自动取消关联选项的挂钩,那么您可以通过以下方式删除它:
p->unlink();