向量和堆栈之间的主要区别是什么?

时间:2020-12-29 03:14:40

Both act like a stack. Both have push and pop operations.

两者都像堆栈一样。两者都有推送和弹出操作。

Is the difference in some memory layouts?

一些内存布局有区别吗?

6 个解决方案

#1


14  

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 :: stack相比,std :: vector有几个可访问性和修改操作。对于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在这个意义上更灵活,它有几个操作,你可以在其间插入()或在其间插入erase()。

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 [].

重点是,std :: stack需要提供底层容器。默认情况下它是std :: deque,但它也可以是std :: vector或std :: list。另一方面,std :: vector保证是一个连续的数组,可以使用operator []访问。

#2


13  

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.

我不知道所有的实现细节,但据此,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.

因此,向量可以作为堆栈工作,但堆栈不能用作向量,因为您无法在随机位置插入或获取元素。

#3


9  

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.

堆栈是一个堆栈。它只能推动和弹出。矢量可以做其他事情,比如插入中间。这增加了灵活性,但减少了保证。

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.矢量不保证。

#4


2  

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).

Stack基本上是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 ++)做到了。

#5


0  

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是一个基于范围的容器。由于其成员函数(如begin和end),它可以很容易地使用。可以使用{}形式轻松启动矢量。我们可以使用现代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则不可能。

#6


0  

As cplusplus.com suggests:

正如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 only inserted and extracted from one end of the container.

这里的关键词只是,因为元素只是从容器的一端插入和提取。

You say both vectors and stacks act like stacks, but this is only partially true. Vectors can act 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.

如果你想要一个具有类似堆栈行为的容器,你应该使用一个堆栈,如果它对你来说特别重要,它只能表现为堆栈。如果你想要类似堆栈的行为,你应该使用一个向量,但也可能想要做一些事情,比如迭代元素或修改任意位置的元素等。

#1


14  

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 :: stack相比,std :: vector有几个可访问性和修改操作。对于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在这个意义上更灵活,它有几个操作,你可以在其间插入()或在其间插入erase()。

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 [].

重点是,std :: stack需要提供底层容器。默认情况下它是std :: deque,但它也可以是std :: vector或std :: list。另一方面,std :: vector保证是一个连续的数组,可以使用operator []访问。

#2


13  

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.

我不知道所有的实现细节,但据此,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.

因此,向量可以作为堆栈工作,但堆栈不能用作向量,因为您无法在随机位置插入或获取元素。

#3


9  

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.

堆栈是一个堆栈。它只能推动和弹出。矢量可以做其他事情,比如插入中间。这增加了灵活性,但减少了保证。

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.矢量不保证。

#4


2  

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).

Stack基本上是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 ++)做到了。

#5


0  

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是一个基于范围的容器。由于其成员函数(如begin和end),它可以很容易地使用。可以使用{}形式轻松启动矢量。我们可以使用现代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则不可能。

#6


0  

As cplusplus.com suggests:

正如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 only inserted and extracted from one end of the container.

这里的关键词只是,因为元素只是从容器的一端插入和提取。

You say both vectors and stacks act like stacks, but this is only partially true. Vectors can act 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.

如果你想要一个具有类似堆栈行为的容器,你应该使用一个堆栈,如果它对你来说特别重要,它只能表现为堆栈。如果你想要类似堆栈的行为,你应该使用一个向量,但也可能想要做一些事情,比如迭代元素或修改任意位置的元素等。