How to Delete All Elements from a Vector in C++? Last Updated : 25 Nov, 2024 Comments Improve Suggest changes Like Article Like Report In C++, you can delete all items from a vector to either clear its contents or free its memory. In this article, we will learn how to delete all items from a vector in C++.The recommended way to delete all items from a vector is by using the vector clear() function. Let’s take a look at a simple example: C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 2, 3, 4, 5}; // Clear the vector v.clear(); cout << v.size(); return 0; } Output0Explanation: The vector clear() function removes all elements from the vector, reducing its size to zero.Note: Although clear() removes all elements, the vector's capacity (memory allocation) remains unchanged. To reduce memory usage, use shrink_to_fit() after clearing.C++ provides a few more methods to delete all the elements and clear the vector. Some of them are as follows:Using Vector erase()The vector erase() function can remove all elements from a vector by specifying the range [begin, end). C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 2, 3, 4, 5}; // Erase all elements v.erase(v.begin(), v.end()); cout << v.size(); return 0; } Output0Explanation: Using the range as v.begin() and v.end() removes all elements.By Swapping with Empty VectorSwap the vector with an empty vector using vector swap() method to delete all elements and reset its capacity. C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 2, 3, 4, 5}; // Swap with an empty vector vector<int>().swap(v); cout << v.size(); return 0; } Output0By Assigning Empty VectorInstead of swapping, a vector can also be assigned an empty vector using vector assign() or simply = operator to delete all the elements. This method also clears the capacity. C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 2, 3, 4, 5}; // Assign an empty vector v = vector<int>(); cout << v.size(); return 0; } Output0Using Vector resize()The vector resize()method can increase or decrease the size of vector, so to clear all elements of vector we can decrease its size to 0 using resize(). C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 3, 4, 6, 7}; // Clear the vector v.resize(0); cout << v.size() << endl; return 0; } Output0 Comment More infoAdvertise with us Next Article How to Delete All Elements from a Vector in C++? A anmolpanxq Follow Improve Article Tags : C++ Programs C++ STL cpp-vector CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Delete an Element from a Set in C++? A set in C++ is a container that stores unique elements in a sorted order. In this article, we will learn how to delete a specific element from a set. Example: Input: mySet = {5, 2, 8, 1, 4} Element to delete: 2 Output: mySet = {5, 1, 8, 4}Delete an Element from a Set in C++To delete a specific elem 2 min read How to Delete Multiple Elements from a Vector in C++? In this article, we will learn how to erase multiple elements from a vector in C++.The easiest and most efficient method to delete the multiple elements from the vector is by using vector erase() method. Letâs take a look at an example:C++#include <bits/stdc++.h> using namespace std; int main( 2 min read How to Clear All Elements from a List in C++? In C++, std::list is a container provided by the Standard Template Library (STL) that represents a doubly linked list and stores elements in non-contiguous memory locations. In this article, we will learn how to clear all elements from a list in C++. Example: Input: myList = {10, 20, 60, 80, 90}; Ou 2 min read How to Delete an Element from the Set in C++? In C++, the set container stores unique elements in the sorted order and the deletion operation should not disrupt this sorted order.C++ provides a built-in function set erase(), which can be used to easily delete the given element by passing its value to the function. Let's take a look at the simpl 1 min read How to Delete an Element from a Multiset in C++? In C++, a multiset is a container that stores elements in a specific order. Multiple elements can have the same values. In this article, we will learn how to delete a specific element from a multiset. Example: Input: myMultiset = {5, 2, 8, 5, 8, 8}; Element to delete: 8 Output: myMultiset = {5, 2, 5 2 min read How to Delete an Element from an Array in C++? In C++, arrays are data structures that allow users to store data of the same type in contiguous memory locations. In this article, we will learn how to delete an element from an array in C++. Example: Input:myarr = {20, 5, 1, 10, 15};Target: 1Output:Array after deletion: 20 5 10 15Remove an Element 3 min read How to Delete Multiple Elements from a Set in C++? In C++, sets are containers that store unique elements in some sorted order. In this article, we will learn how to delete multiple elements from a set using C++. Example: Input: set = {10, 20, 30, 40, 50, 60}elements_to_delete = {20, 30, 40}Output: Elements of set after deletion: 10 50 60Removing Mu 2 min read How to Remove an Element from a Deque in C++? In C++ STL, a container called deque (known as a double-ended queue) allows us to insert and delete elements at both its beginning and its end. In this article, we will learn how to remove a specific element from a deque in C++ STL. Example: Input: myDeque= {4, 2, 3, 5, 2} Target = 4 Output: Deque A 2 min read How to Clear All Elements from a Deque in C++? In C++, deque also known as double-ended queue is a container provided by the Standard Template Library (STL) that allows efficient insertion and deletion at both ends. In this article, we will learn how to clear all elements from a deque in C++. Example: Input: myDeque = {10, 20, 30, 40}; Output: A 2 min read Like