地图的插入在 c++ 中到底如何工作?

发布于 2025-01-11 12:26:19 字数 605 浏览 5 评论 0原文

我试图自己用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我三岁 2025-01-18 12:26:19

当提示正确时,提示重载具有更严格的复杂性要求。如果提示不正确,则会被忽略。

复杂性

如果插入发生在提示之后的位置,则为摊销常量,以容器大小的对数表示
否则。

如果您已经知道(或者甚至有一个很好的猜测)要插入的值应该放在哪里,例如您要插入一系列已排序的值,则可以使用提示重载作为优化。

The hint overloads have tighter complexity requirements when the hint is correct. If the hint is incorrect, it is ignored.

Complexity

Amortized constant if the insertion happens in the position just after the hint, logarithmic in the size of the container
otherwise.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文