更新集合中的指针以指向 C++ 中具有不同属性的等效对象?

Update a pointer in a set to point to an equivalent object with different properties in C++?

如果我有一组指针 set<A*, comp> as ,当我插入一个指向等效对象但具有不同属性的指针 as.insert(p) 时,我如何使用 insert( ) 更新指针以指向新对象?

对象 a1 的名称为 "a",大小为 5

对象 a2 的名称为 "a",大小为 10

对象 a3 的名称为 "a",大小为 25

集合按对象名称排序。

as.insert(&a1);
as.insert(&a2);
as.insert(&a3);

我希望集合中的指针指向最后插入的对象 a3,大小为 25。

谢谢

您无法更改 set 中的指针指向的内容。允许这样做会破坏 set 中存在的隐式排序。

您需要删除旧指针并插入新指针。

auto found = as.find(&a3);
if ( found != as.end() )
{
   as.erase(found);
}

as.insert(&a3);