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.

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

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

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...