What is the major difference between a vector and a stack?

前端 未结 6 776
不思量自难忘°
不思量自难忘° 2021-01-31 18:29

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

Is the difference in some memory layouts?

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-31 18:52

    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.

提交回复
热议问题