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

前端 未结 6 785
不思量自难忘°
不思量自难忘° 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 19:04

    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.

提交回复
热议问题