Vectors are useful data structures that act like dynamic one-dimensional arrays.
The clear()
function is defined in the <vector>
library. It is used to delete all the elements in a vector. When the vector is empty, its size becomes zero.
myVec.clear()
This function takes no parameters.
The function returns nothing.
The changes are made directly to the vector.
#include <iostream>#include <vector> // first include <vector>using namespace std;int main(){//declaration of int vectorvector<int> vec = {1, 2, 3, 4, 5};// returns length of vector as unsigned intunsigned int vecSize = vec.size();cout << "Before using clear(), size of vector: " << vecSize << endl;// calling clear()vec.clear();// recalculate size of vectorvecSize = vec.size();cout << "After using clear(), size of vector: " << vecSize << endl;cout << endl;return 0;}