32

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

Is the difference in some memory layouts?

Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
  • 1
    Stacks default container is a deque - `class Container = deque` . – Jesse Good Jan 09 '12 at 09:08
  • @Jesse any links to support that? And what is vector's default container? – Aquarius_Girl Jan 09 '12 at 09:08
  • Here is a link to [MSDN](http://msdn.microsoft.com/en-us/library/56fa1zk5%28v=VS.100%29.aspx). You should also get a [pdf copy of the C++ standard draft](http://www.open-std.org/jtc1/sc22/wg21/) which contains the definitions. Vector is one of the standard stl containers, stack is a specialization *using one* of the standard containers. – Jesse Good Jan 09 '12 at 09:14

6 Answers6

23

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 is more flexible in that sense, where it has several operations, where you can insert() in between or erase() in between.

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

iammilind
  • 68,093
  • 33
  • 169
  • 336
  • You said: `The major point is that, std::stack needs to be provided the underlying container.` but this example doesn't show any extra effort of supplying a container? http://www.cplusplus.com/reference/stl/stack/push/ – Aquarius_Girl Jan 09 '12 at 09:07
  • 3
    @AnishaKaul, that is because by *default* it is, `template < class T, class Container = deque > class stack;`. So if you don't provide any container, it assumes it to be `std::deque`. See the link I posted in answer for more information. – iammilind Jan 09 '12 at 09:09
  • Ok, thanks, the container can be changed! Fine. Vector's default container is dynamic array created by new? – Aquarius_Girl Jan 09 '12 at 09:11
  • 1
    @AnishaKaul, no `std::vector` is an independent container. It uses `std::allocator` and yes it might be using `new[]`. – iammilind Jan 09 '12 at 09:15
  • 1
    @Anisha: When people use the word "container", they usually only refer to the stl containers, here is a [link](http://www.cplusplus.com/reference/stl/) which lists them (although there are other ones that have been added in C++11) – Jesse Good Jan 09 '12 at 09:17
  • @Jesse I have seen that link, and I too was referring to STL containers. – Aquarius_Girl Jan 09 '12 at 09:19
18

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.

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.

MikMik
  • 3,426
  • 2
  • 23
  • 41
  • 1
    +1 for **the** important point. The STL contains several **container adapters** which do not obey the general requirements of containers because... they are not. – Matthieu M. Jan 09 '12 at 09:57
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.

Peter Alexander
  • 53,344
  • 14
  • 119
  • 168
  • Right, I just saw that vector has an `erase` function which can erase from the middles. Stack doesn't have any such thing.:) so, vector is a flexible stack. – Aquarius_Girl Jan 09 '12 at 09:02
  • @Anisha: No, that is a wrong way to assume. If that should be the case a link-list , a simple array , a deque can all be called a stack..rt ? But stack is a set of properties defined for a container or a data structure. You can create a stack out of the above mentioned data structure if your implementation remains true for the properties defined for a stack. – Arunmu Jan 09 '12 at 09:59
  • @ArunMu Actually by stack I was referring to the "container" called stack. – Aquarius_Girl Jan 09 '12 at 10:00
  • @AnishaKaul: ohh. I was just saying, even if you add a function for operator[] for stack ..it would no longer be a stack :) as per the standards :). Was just saying.. – Arunmu Jan 09 '12 at 10:05
  • @ArunMu Can we add user defined function [] to the container stack? – Aquarius_Girl Jan 09 '12 at 10:06
  • 1
    @AnishaKaul: If you are thinking anywhere close to inheriting stack container then NOOOO.... STL container do not have virtual destructors. And I think as per STL implementation it would be a herculean task to do ...rightfully to preserve stack property – Arunmu Jan 09 '12 at 10:13
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).

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.

Ankit
  • 6,772
  • 11
  • 48
  • 84
  • 1
    The STL is an old thing; you probably mean the Standard Library. And no, its `stack` isn't defined as inheriting from `vector`; it is a templated adaptor that defaults to `deque` and for which a `vector` can be chosen as an option. – underscore_d Dec 07 '17 at 21:38
1

As cplusplus.com suggests:

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.

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.

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.

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<int> vec{ 7, 3, 1, 9, 5 };
for ( auto &i : vec ) {
    std::cout << i << std::endl;
}

Whereas it is not possible for std::stack.

Sam Mokari
  • 461
  • 3
  • 11
  • But the people choosing to use `std::stack` already know that they don't care about range-based iteration; rather, they want a concise interface to push and pop elements in a well-defined order. – underscore_d Dec 07 '17 at 21:39