微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c – std :: deque :: push_back / front的复杂性要求

由于前几天的 this问题,有一些事情一直在告诉我有关std :: deque :: push_back / push_front的复杂性要求与野外实际的std :: deque实现.

一个问题的结果是,这些操作需要具有O(1)最差的情况复杂性.我证实在c 11中确实是这样:

from 23.3.3.4 deque modifiers,refering to insert,push/emplace front/back

Complexity: The complexity is linear in the number of elements inserted plus the
lesser of the distances to the beginning and end of the deque. Inserting a single
element either at the beginning or end of a deque always takes constant time and
causes a single call to a constructor of T.

这与通过运算符[]等的索引的O(1)复杂度要求相结合

问题在于实现不能严格满足这些要求.

在msvc和gcc方面,std :: deque实现是一个阻塞的数据结构,由一个指向(固定大小)块的指针的动态数组组成,其中每个块存储多个数据元素.

在最坏的情况下,push_back / front等可能需要分配一个额外的块(这是很好的 – 固定大小的分配是O(1)),但是它也可能要求块指针的动态数组被调整大小 – 这不是因为这是O(m),其中m是块的数量,其在一天结束时是O(n).

显然,这仍然是摊销O(1)的复杂性,并且由于通常m <在实践中将会很快.但似乎有一个一致性的问题? 另外,我看不出如何设计一个严格满足push_back / front等和operator []的O(1)复杂度的数据结构.你可以有一个链接列表的块指针,但这不会给你所需的operator []行为.实际上可以做到吗?

解决方法

在C11 Fdis中,我们可以看到:

23.2.3 Sequence containers [sequence.reqmts]

16/ Table 101 lists operations that are provided for some types of sequence containers but not others. An implementation shall provide these operations for all container types shown in the “container” column,and shall implement them so as to take amortized constant time.

其中表101被命名为可选序列容器操作,并列出push_back和push_front操作的deque.

因此,您所引用的段落似乎更像是略有遗漏.也许值得缺陷报告?

请注意,单个调用构造函数仍然保持.

原文地址:https://www.jb51.cc/c/114281.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐