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

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

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

    Whereas it is not possible for std::stack.

提交回复
热议问题