std::map 擦除保留迭代器

std::map erase preserving iterators

我正在使用基本的二叉搜索树实现自己的 std::map。

擦除方法应该

invalidate the iterators referring to element removed and preserve the validity of all other iterators

然而,这是 BST“经典”删除算法正在做的事情:

如果要删除的节点A有两个children,用它的in-order后继/前任B“替换”A和删除B.

最后,B 的迭代器失效了。 因此不保留“其他”迭代器的有效性。

我发现的算法不合适吗?

Is the algorithm I've found not suitable ?

正确。您描述的算法不适合实施 std::map.