STL 中的双端队列到底是什么? - What really is a deque in STL?

10/14 00:15
阅读数 0

问题:

I was looking at STL containers and trying to figure what they really are (ie the data structure used), and the deque stopped me: I thought at first that it was a double linked list, which would allow insertion and deletion from both ends in constant time, but I am troubled by the promise made by the operator [] to be done in constant time.我正在查看 STL 容器并试图弄清楚它们到底是什么(即使用的数据结构),而deque阻止了我:我起初以为它是一个双链表,它允许从两端插入和删除恒定时间,但我对操作员 [] 做出的在恒定时间内完成的承诺感到困扰。 In a linked list, arbitrary access should be O(n), right?在链表中,任意访问应该是 O(n),对吗?

And if it's a dynamic array, how can it add elements in constant time?而如果是动态数组,它如何在恒定时间内添加元素 It should be mentioned that reallocation may happen, and that O(1) is an amortized cost, like for a vector .应该提到的是,可能会发生重新分配,并且 O(1) 是摊销成本,就像向量一样

So I wonder what is this structure that allows arbitrary access in constant time, and at the same time never needs to be moved to a new bigger place.所以我想知道这个允许在恒定时间内任意访问的结构是什么,同时永远不需要移动到一个新的更大的地方。


解决方案:

参考一: https://en.stackoom.com/question/QOvE
参考二: https://stackoom.com/question/QOvE
展开阅读全文
c++
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部