How to Delete All Elements from a Vector in C++?
Last Updated :
25 Nov, 2024
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;
}
Explanation: 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;
}
Explanation: Using the range as v.begin() and v.end() removes all elements.
By Swapping with Empty Vector
Swap 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;
}
By Assigning Empty Vector
Instead 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;
}
Using 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;
}
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