地图的插入在 c++ 中到底如何工作?
我试图自己用 C++ 重新实现地图容器,但我陷入了 insert
方法。
注意:我知道地图使用自平衡树(红黑树),因此当您插入新元素时,您需要遵守二叉搜索树规则。
现在我的问题是:
(C++98) 中的映射有 3 个插入成员函数,
single element (1) pair<iterator,bool> insert (const value_type& val);
with hint (2) iterator insert (iterator position, const value_type& val);
range (3) template <class InputIterator>
void insert (InputIterator first, InputIterator last);
单个元素 (1) 很清楚。
但第二个不是。
我想知道如何使用提示,如何检查给我的位置是否尊重树的规则,以及如何修复它。
注 2:这是基于 c++98 我知道这在 C++11 中发生了变化。
I'm trying to reimplement the map container in c++ by myself but I get stuck in the insert
method.
NOTE: I know that map uses a self-balancing tree (Red-black Tree) so when You insert a new element you need to respect the binary search tree rules.
now my question is :
the map in (C++98) has 3 insert member functions
single element (1) pair<iterator,bool> insert (const value_type& val);
with hint (2) iterator insert (iterator position, const value_type& val);
range (3) template <class InputIterator>
void insert (InputIterator first, InputIterator last);
the single element (1) is clear.
but the second one is not.
I wanna know how the hint is used and how can I check if the position was given to me respects the rules of the tree or not and how can I fix it.
NOTE 2: This is based on c++98 I know that this changed in C++11.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当提示正确时,提示重载具有更严格的复杂性要求。如果提示不正确,则会被忽略。
如果您已经知道(或者甚至有一个很好的猜测)要插入的值应该放在哪里,例如您要插入一系列已排序的值,则可以使用提示重载作为优化。
The hint overloads have tighter complexity requirements when the hint is correct. If the hint is incorrect, it is ignored.
If you already know (or even have a good guess) where the value you are inserting should go, e.g. you are inserting a sequence of already sorted values, you can use the hint overload as an optimisation.