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

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.

Leave a Comment