Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I have to clear a C++ vector after declaration?

Tags:

c++

std

stdvector

Assume I declare a vector:

std::vector <int> v;

After this declaration, is it guaranteed that the vector is empty or do I have to explicitly clear/initialize it? Is calling v.clear() after the declaration useless or recommended?

like image 872
Botond Avatar asked Oct 26 '25 08:10

Botond


1 Answers

When defining a variable like that, the default constructor is called. In vector's case, the default constructor creates an empty vector with no elements, so no, you don't need to explicitly clear it.

like image 67
Mureinik Avatar answered Oct 29 '25 05:10

Mureinik