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

前端 未结 10 2398
我寻月下人不归
我寻月下人不归 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 07:43

    You have no right to get a segmentation fault. For that matter, a segmentation fault isn't even part of C++. Your program is removing all elements from the vector, and you're illegally accessing the container out of bounds. This is undefined behaviour, which means anything can happen. And indeed, something happened.

提交回复
热议问题