Vector clear() in C++ STL Last Updated : 09 Apr, 2025 Comments Improve Suggest changes Like Article Like Report In C++, vector clear() is a built-in method used to remove all elements from a vector, making it empty. In this article, we will learn about the vector clear() method in C++.Let’s take a look at an example that illustrates the vector clear() method: C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {11, 23, 45, 9}; // Remove all elements from the vector v.clear(); // Check if the vector is empty if (v.empty()) { cout << "Vector is Empty"; } else { cout << "Vector is Not Empty"; } return 0; } OutputVector is EmptyThis article covers the syntax, usage, and common examples of the vector clear() method in C++ STL.Syntax of Vector clear()The vector clear() method is defined inside the std::vector class in the <vector> header file.v.clear();This function does not take any parameters, nor it returns any value.Examples of Vector clear()The below examples demonstrate the use and behaviour of vector clear() method in practical scenarios.Delete All the Elements of a Vector C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {11, 23, 45, 9}; // Clear the vector v.clear(); // Check the size of the vector cout << v.size(); return 0; } Output0Effect of Vector clear() on the Capacity C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {11, 23, 45, 9}; // Initial capacity cout << v.capacity() << endl; // Clear the vector v.clear(); cout << v.capacity(); return 0; } Output4 4Explanation: As we can see, the vector clear() method does not affects the capacity of the vector. It means that although it deletes the memory Comment More infoAdvertise with us Next Article Vector clear() in C++ STL A abhishekcpp Follow Improve Article Tags : C++ STL cpp-vector cpp-containers-library CPP Examples +1 More Practice Tags : CPPSTL Similar Reads Vector erase() in C++ STL In C++, vector erase() is a built-in function that is used to delete elements from the vector. It removes an element of a specific position or range of elements from the vector. Letâs take a simple example that uses the vector erase() method:C++#include <bits/stdc++.h> using namespace std; int 3 min read Vector end() in C++ STL In C++, the vector end() is a built-in method used to obtain an iterator pointing to the theoretical element after the last element of the vector. Even though this iterator does not point to a valid element, it serves as a marker for the end of the vector.Letâs take a look at an example that illustr 3 min read Vector data() in C++ STL In C++, the vector data() is a built-in function used to access the internal array used by the vector to store its elements. In this article, we will learn about vector data() in C++.Letâs take a look at an example that illustrates the vector data() method:C++#include <bits/stdc++.h> using nam 2 min read Vector back() in C++ STL In C++, the vector back() is a built-in function used to retrieve the last element of the vector. It provides a reference to the last element which allows us to read or modify it directly.Letâs take a quick look at a simple example that illustrates the vector back() method:C++#include <bits/stdc+ 2 min read Vector in C++ STL C++ vector is a dynamic array that stores collection of elements same type in contiguous memory. It has the ability to resize itself automatically when an element is inserted or deleted.Create a VectorBefore creating a vector, we must know that a vector is defined as the std::vector class template i 7 min read Like