简体   繁体   English

std::map 擦除保留迭代器

[英]std::map erase preserving iterators

I'm implementing my own std::map, using a basic binary search tree.我正在使用基本的二叉搜索树实现我自己的 std::map。

The erase method is supposed to擦除方法应该是

invalidate the iterators referring to element removed and preserve the validity of all other iterators使引用元素的迭代器无效并保留所有其他迭代器的有效性

Yet here is what the BST "classic" delete algorithm is doing:然而,这就是 BST“经典”删除算法正在做的事情:

If node to be deleted A has two children, "replace" A by its in-order successor / predecessor B and the delete B.如果要删除的节点 A 有两个子节点,则将 A 替换为其按顺序的后继/前任 B 和删除 B。

In the end, iterators to B are invalidated.最后,B 的迭代器无效。 So validity of "other" iterator is not preserved.所以“其他”迭代器的有效性不会被保留。

Is the algorithm I've found not suitable?我发现的算法不合适吗?

Is the algorithm I've found not suitable?我发现的算法不合适吗?

Correct.正确的。 The algorithm that you describe would not be suitable for implementing std::map .您描述的算法不适合实现std::map

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM