c++ - STL 队列(或堆栈)的双端队列和链表(+ vector )实现之间有什么区别?

标签 c++ stl stack queue implementation

我们知道标准的容器类模板deque和list可以用来实现queue,+vector来实现stack。但是,如果我们总是使用相同的方法访问并且也不能像使用双端队列( vector )那样使用 at() 或 [] 访问任意元素,那么这两种实现之间有什么区别。

最佳答案

容器适配器,例如 stackqueue,是使用特定的操作子集实现的——任何将与特定适配器一起使用的容器都必须支持所有适配器需要的操作。

vector 不能与 queue 容器适配器一起使用,因为:

Any sequence supporting operations front(), back(), push_back() and pop_front() can be used to instantiate queue.

并且 vector 不支持 pop_front()

vectordequelist 可以与 stack 一起使用,因为所有这三个容器都支持这些操作堆栈需要:

Any sequence supporting operations back(), push_back() and pop_back() can be used to instantiate stack.

关于c++ - STL 队列(或堆栈)的双端队列和链表(+ vector )实现之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9655265/

相关文章:

JavaScript 堆栈执行问题

c - 在 c [BEGINNER] 中使用 malloc 的堆栈实现

c - 堆栈上声明的最大允许大小是多少?

c++ - "points to uninitialised byte(s)"Valgrind 错误

python - affdex.cpp :9:31: fatal error: pybind11/pybind11. h : No such file or directory #include <pybind11/pybind11. h>

c++ - 如何在大型机中显示 CFormView?

c++ - C++ 结构中的 vector

c++ - 如何定义和使用 boost::function with "optional arguments"?

c++ - std::map 中某个键的值可以被锁定吗?

c++ - 设置运行时刻bitset的大小