STL 中的双端队列到底是什么?

编程入门 行业动态 更新时间:2024-10-09 09:15:45
本文介绍了STL 中的双端队列到底是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在查看 STL 容器并试图弄清楚它们到底是什么(即使用的数据结构),deque 阻止了我:我一开始以为它是一个双链表,这将允许在恒定时间内从两端插入和删除,但我对

在 vector 的比较进行了很好的分析Depth-Study-of-the-STL-Deque-Container" rel="noreferrer">CodeProject.

GCC 标准库实现内部使用一个 T** 来表示地图.每个数据块都是一个T*,它被分配了一些固定大小的__deque_buf_size(取决于sizeof(T)).

I was looking at STL containers and trying to figure what they really are (i.e. 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. In a linked list, arbitrary access should be O(n), right?

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.

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.

解决方案

A deque is somewhat recursively defined: internally it maintains a double-ended queue of chunks of fixed size. Each chunk is a vector, and the queue ("map" in the graphic below) of chunks itself is also a vector.

There’s a great analysis of the performance characteristics and how it compares to the vector over at CodeProject.

The GCC standard library implementation internally uses a T** to represent the map. Each data block is a T* which is allocated with some fixed size __deque_buf_size (which depends on sizeof(T)).

更多推荐

STL 中的双端队列到底是什么?

本文发布于:2023-11-30 06:49:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649076.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:队列   到底是什么   STL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!