// Using clear() to clear a vector of integers #include#include using namespace std; int main(){ vector myvector; myvector.push_back(1); myvector.push_back(2); myvector.push_back(3); cout << "Size of vector: " << myvector.size() << endl; // Output: 3 myvector.clear(); cout << "Size of vector after clearing: " << myvector.size() << endl; // Output: 0 return 0; }
// Using clear() to clear a vector of strings #includePackage library: The vector clear() function is a part of the C++ Standard Template Library (STL) which is a built-in package library in C++. No external package or library needs to be installed to use this function.#include #include using namespace std; int main(){ vector myvector; string input; // Populate vector through user input for(int i=0; i<3; i++){ cout << "Enter a word: "; cin >> input; myvector.push_back(input); } cout << "Size of vector: " << myvector.size() << endl; // Output: 3 myvector.clear(); cout << "Size of vector after clearing: " << myvector.size() << endl; // Output: 0 return 0; }