当我知道我插入的指针时,如何从boost::ptr_set中删除?

How do I delete from a boost::ptr_set when I know the pointer I inserted?

本文关键字:ptr boost set 删除 插入 我知道 指针      更新时间:2023-10-16

当我知道我插入的指针时,我如何从boost::ptr_set中删除?(我有一个this指针指向插入的类对象)。

下面是一个人为的例子来展示我正在尝试做的事情:

boost::ptr_set<ServerConnection1> m_srv_conns1;
ServerConnection1 *this_ptr;
m_srv_conns1.insert(this_ptr = new ServerConnection1);
m_srv_conns1.erase(this_ptr); //It won't work!

有一个指向插入对象的this指针,我如何告诉boost::ptr_seterase(this) ?注意:我不再在插入的对象中,但我有一个指向它的指针。

更新

其中一个评论是我没有满足boost::ptr_set的所有要求。有什么要求?

我认为提供一个< operator会做的伎俩?

  1. m_srv_conns1.erase(this_ptr);更改为m_srv_conns1.erase(*this_ptr);
  2. 将以下代码放入ServerConnection1类中:

bool operator<(const ServerConnection1 & sc1) const
{
return (this < &sc1); //Pointer comparison
}

试试m_srv_conns1.erase(*this_ptr); .