How to Delete Multiple Elements from a Vector in C++? Last Updated : 21 Nov, 2024 Comments Improve Suggest changes Like Article Like Report 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() { vector<int> v = {1, 2, 3, 4, 5}; // Delete all elements from 1st index // to 3rd index v.erase(v.begin() + 1, v.begin() + 4); for (auto i : v) cout << i << " "; return 0; } Output1 5 Explanation: The vector erase() method removed all element of given range [first, last) which here is from index 1 to 4.There are also some other methods in C++ to delete the multiple elements from a vector. Some of them are as follows:Using Loop with Vector erase()If the elements to be deleted are stored non-contiguously in the vector, then they have to be manually deleted the elements using loop with vector erase(). C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 2, 3, 4, 5}; // Delete the even elements for (auto i = v.begin(); i != v.end();) { if ((*i) % 2 == 0) i = v.erase(i); else i++; } for (auto i : v) cout << i << " "; return 0; } Output1 3 5 Explanation: In the above code, if the elements is even then we delete the element from vector otherwise we increment the iterator.Using vector clear()If all the elements are to be deleted, we can use vector clear() method that removes all the elements in one go. C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 2, 3, 4, 5}; // Remove all elements from vector v.clear(); cout << v.size(); return 0; } Output0 Comment More infoAdvertise with us Next Article How to Delete Multiple Elements from a Vector in C++? gaurav472 Follow Improve Article Tags : C++ Programs C++ STL cpp-vector CPP Examples +1 More Practice Tags : CPPSTL Similar Reads 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 Delete All Elements from a Vector in C++? 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 exa 2 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 the Last Element from a Multiset in C++? In C++, a multiset is a container that can store multiple elements, including duplicates. In this article, we will learn how to delete the last element from a multiset in C++. Example: Input:myMultiset = { 1, 2, 2, 3, 3, 3, 4, 4, 4, 4 }Element to delete = 4Output: myMultiset = { 1, 2, 2, 3, 3, 3, 4, 2 min read 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 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 Remove an Element from Vector in C++? In this article, we will learn how to remove a given element from the vector in C++.The most straightforward method to delete a particular element from a vector is to use the vector erase() method. Let's look at a simple example that shows how to use this function:C++#include <bits/stdc++.h> u 2 min read How to Delete Element from Specific Position in Vector? In C++, vector provides access to its elements using their index values. In this article, we will learn how to delete an element from specific position in vector in C++.The recommended method to delete an element from specific position in vector is by using vector erase() method. Letâs take a look a 3 min read How to Remove Last Element from Vector in C++? Given a vector of n elements, the task is to remove the last element from the vector in C++.The most efficient method to remove the last element from vector is by using vector pop_back() method. Letâs take a look at a simple example:C++#include <bits/stdc++.h> using namespace std; int main() { 2 min read How to Add Multiple Elements at the End of Vector in C++? In C++, vectors are dynamic arrays that can grow and reduce in size as per requirements. In this article, we will learn how to add multiple elements at the end of a vector in C++. Example Input:myVector = {10, 20, 30, 40, 50}elements_to_add = {60, 70, 80}Output:updated_vector: 10 20 30 40 50 60 70 8 2 min read Like