how to empty a std vector
To empty a std::vector
in C++, you can use the clear()
method. Here are the steps to empty a std::vector
:
- Declare and initialize the vector: Start by declaring and initializing a
std::vector
object. For example, you can declare a vector of integers as follows:
std::vector<int> myVector = {1, 2, 3, 4, 5};
- Call the clear() method: To empty the vector, call the
clear()
method on the vector object. This method removes all elements from the vector, effectively making it empty. Here's how you can useclear()
:
myVector.clear();
After calling clear()
, the vector will be empty. The clear()
method removes all elements from the vector by resetting its size to 0.
That's it! Following these steps will empty a std::vector
in C++.