C++ vector clear() function
Example
Clear the contents of a vector:
vector<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};
cout << "Size before: " << cars.size() << "\n";
cars.clear();
cout << "Size after: " << cars.size() << "\n";
Try it Yourself »
Definition and Usage
The clear()
function removes all elements from a vector.
Syntax
vector.clear();
Parameter Values
None.
Related Pages
Read more about vectors in our Vector Tutorial.