Why doesn't vector::clear remove elements from a vector?

前端 未结 10 2429
我寻月下人不归
我寻月下人不归 2020-11-29 07:29

When I use clear() on a std::vector, it is supposed to destroy all the elements in the vector, but instead it doesn\'t.

Sample

10条回答
  •  眼角桃花
    2020-11-29 08:04

    The operator[] is efficient but comes at a price: it does not perform boundary checking.

    There are safer yet efficient way to access a vector, like iterators and so on.

    If you need a vector for random access (i.e. not always sequential), either be very careful on how you write your programs, or use the less efficient at(), which in the same conditions would have thrown an exception.

提交回复
热议问题