C++ 向量和堆栈之间的主要区别是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8785841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the major difference between a vector and a stack?
提问by Aquarius_Girl
Both act like a stack. Both have push and pop operations.
两者都像一个堆栈。两者都有推送和弹出操作。
Is the difference in some memory layouts?
某些内存布局有区别吗?
采纳答案by iammilind
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::vector
与std::stack
. 在 的情况下std::stack
,您可能只能以系统的方式执行操作,您可以push()
在最后一个元素或pop()
最后一个元素之上。
std::vector
is more flexible in that sense, where it has several operations, where you can insert()
in between or erase()
in between.
std::vector
从这个意义上说,它更灵活,它有几个操作,你可以insert()
在两者之间或erase()
中间。
The major point is that, std::stackneeds 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 []
.
主要的一点是,std::stack需要提供底层容器。默认情况下它std::deque
,但它可以是std::vector
或std::list
过。
另一方面,std::vector
保证是一个可以使用访问的连续数组operator []
。
回答by MikMik
I'm not aware of all the implementation details, but according to this, stack is a container adaptor. It makes sure the underlying container, which can be a vector, list or deque, works as a stack, i.e. only allows push and pop, and not random access.
我不知道所有的实现细节,但根据this,stack 是一个容器适配器。它确保底层容器(可以是向量、列表或双端队列)作为堆栈工作,即只允许推送和弹出,而不允许随机访问。
So, a vector can work as a stack, but a stack cannot work as a vector, because you cannot insert or get an element at a random position.
因此,向量可以用作堆栈,但堆栈不能用作向量,因为您无法在随机位置插入或获取元素。
回答by Peter Alexander
stack
is a stack. It can only push and pop. A vector
can do other things, like insert into the middle. This increases flexibility, but reduces guarantees.
stack
是一个堆栈。它只能推和弹出。Avector
可以做其他事情,比如插入中间。这增加了灵活性,但减少了保证。
For example, for a stack, if you push A then B onto the back then you are guaranteed that they will be removed in the order B, then A. vector
doesn't guarantee that.
例如,对于堆栈,如果您将 A 然后 B 推到后面,那么您可以保证它们会按照 B 的顺序被移除,那么 A.vector
并不能保证这一点。
回答by Ankit
Stack is basically a special case of vector. Theoretically speaking vector can grow as you wish. You can remove elements at any index in a vector. However, in case of a stack you can remove elements and insert them only at its top (hence a special case of vector).
堆栈基本上是向量的一个特例。从理论上讲,向量可以随心所欲地增长。您可以删除向量中任何索引处的元素。但是,在堆栈的情况下,您可以删除元素并仅在其顶部插入它们(因此是向量的特殊情况)。
In face in many libraries that provide an implementation of a stack, they generally inherit from the vector class/structures. I am not sure, but I think STL (C++) does it.
面对许多提供堆栈实现的库,它们通常继承自向量类/结构。我不确定,但我认为 STL (C++) 做到了。
回答by Sam Mokari
I think the main difference is that vector is a range based container. It can be easily used thanks to its member functions such as begin and end. Vector can be easily initiated with {} form. We can use new features of modern C++ like range-based loops.
我认为主要区别在于 vector 是一个基于范围的容器。由于它的成员函数如开始和结束,它可以很容易地使用。Vector 可以使用 {} 形式轻松启动。我们可以使用现代 C++ 的新特性,比如基于范围的循环。
vector<int> vec{ 7, 3, 1, 9, 5 };
for ( auto &i : vec ) {
std::cout << i << std::endl;
}
Whereas it is not possible for std::stack.
而 std::stack 是不可能的。
回答by Garrett Gutierrez
As cplusplus.comsuggests:
正如cplusplus.com 所建议的:
Stacks are a type of container adaptor, specifically designed to operate in a LIFO context (last-in first-out), where elements are inserted and extracted only from one end of the container.
堆栈是一种容器适配器,专门设计用于在 LIFO 上下文(后进先出)中操作,其中元素仅从容器的一端插入和提取。
The key word here is only, as in elements are onlyinserted and extracted from one end of the container.
这里的关键词是only,因为元素只从容器的一端插入和提取。
You say both vectors and stacks act like stacks, but this is only partially true. Vectors canact like stacks, but they can also act very much not like stacks, by allowing you to do things such as insert at any index, access any element, iterate over the entire structure, etc.
你说向量和堆栈都像堆栈一样,但这只是部分正确。向量可以像堆栈一样运行,但它们也可以像堆栈一样运行,允许您执行诸如在任何索引处插入、访问任何元素、遍历整个结构等操作。
Stacks take a container (such as, for example, a vector) and only permit stack-like interactions with it. This effectively guarantees that all interactions with the container will obey LIFO: only the most recently added element in the container will be able to be accessed or removed.
堆栈采用一个容器(例如,一个向量)并且只允许与它进行类似堆栈的交互。这有效地保证了与容器的所有交互都将遵循 LIFO:只有容器中最近添加的元素才能被访问或删除。
If you want a container with stack-like behavior, you should use a stack if it is particularly important to you that it behaves exclusively a stack. You should use a vector if you want stack-like behavior but might also want to do things like iterate over elements or modify elements in arbitrary positions etc.
如果您想要一个具有类似堆栈行为的容器,并且对您来说特别重要的是它的行为完全是一个堆栈,那么您应该使用堆栈。如果您想要类似堆栈的行为,您应该使用向量,但也可能想要执行迭代元素或修改任意位置的元素等操作。