向量和堆栈之间的主要区别是什么? [英] What is the major difference between a vector and a stack?
问题描述
两者的行为都一样。两者都有推送和弹出操作。
Both act like a stack. Both have push and pop operations.
某些内存布局是否有所不同?
Is the difference in some memory layouts?
推荐答案
std :: vector
与 std :: stack
相比,具有多种可访问性和修改操作。在 std :: stack
的情况下,您可能只能以系统的方式执行操作,可以在其中进行 push()
在最后一个元素上方,或 pop()
最后一个元素。
std::vector
has several accessibility and modification operations compared to std::stack
. In case of std::stack
, you may have to perform operations only in systematic way, where you can push()
above the last element or pop()
the last element.
std ::向量
在这种意义上更灵活,它具有多个操作,您可以在其中进行 insert()
或擦除()
。
std::vector
is more flexible in that sense, where it has several operations, where you can insert()
in between or erase()
in between.
重点是 std :: stack 提供基础容器。默认情况下为 std :: deque
,但可以为 std :: vector
或 std :: list
。
另一方面, std :: vector
可以保证是一个连续数组,可以是使用操作符[]
访问。
The major point is that, std::stack needs to be provided the underlying container. By default it's std::deque
, but it can be std::vector
or std::list
too.
On other hand, std::vector
is guaranteed to be a contiguous array which can be accessed using operator []
.
这篇关于向量和堆栈之间的主要区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!