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

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

    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.

提交回复
热议问题