How to Remove an Element from Vector in C++? Last Updated : 22 Nov, 2024 Comments Improve Suggest changes Like Article Like Report 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> using namespace std; int main() { vector<int> v = {1, 5, 7, 4}; // Removing 7 from vector v v.erase(find(v.begin(), v.end(), 7)); for (auto i : v) cout << i << " "; return 0; } Output1 5 4 Explanation: The vector erase() function removes the element using iterator not value, so iterator to the element to be removed is determined using find() function.C++ also provide more methods to delete the element or multiple elements from the given vector. Some of them are as follows:Using remove() with Vector pop_back()The remove() function shift an element that have to be remove to the end of vector. The last element is then deleted by vector pop_back() method.Example C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 5, 7, 4}; // Shifting 7 to the end of vector remove(v.begin(), v.end(), 7); // Deleting the last element i.e. 7 v.pop_back(); for (auto i : v) cout << i << " "; return 0; } Output1 5 4 We can also use vector erase() method to delete the last element.Using vector clear()The vector clear() method is used when all the elements of the vector have to be removed. C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 5, 7, 4}; // Removing all the elements v.clear(); for (auto i : v) cout << i << " "; return 0; } Output(empty) Comment More infoAdvertise with us Next Article How to Remove an Element from 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 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 Remove an Element from a Set in C++? In C++, sets are a type of associative container in which each element has to be unique. The values are stored in a specific sorted order i.e. either ascending or descending. In this article, we will see how to remove specific elements from a set in C++. Example Input: set = {100,120,12,56,78,9,32,4 2 min read How to Remove an Element from Array in C++? In C++, removing an element from an array is tricky because we cannot modify the memory space occupied by the array. So, we pseudo delete the element by moving it to the end of the array and reducing the size. In this article, we will learn how to remove a value from an array in C++.ExamplesInput: a 3 min read How to Remove an Element from a List in C++? In C++, the STL provides a std::list container that represents a doubly linked list to store the sequential data in non-contiguous memory locations. In this article, we will learn how to remove an element from a list in C++. Example: Input: myList = {1, 2, 3, 4, 5, 6, 7, 8} Target = 5 Output: // rem 2 min read How to Remove Duplicates from a Vector in C++? In this article, we will learn how to remove the duplicates from the vector in C++.The recommended way to remove the duplicates from the vector is by using sort() with unique() to move all the duplicate elements at the end and then remove them using vector erase(). Letâs take a look at an example:C+ 3 min read How to Remove Elements from a Vector while Iterating in C++? Erasing elements from a vector while iterating can be challenging because removing an element may invalidate iterators or change the size of the vector. In this article, we will learn how to erase an elements from vector while iterating in C++.To efficiently remove elements from a vector while itera 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 Remove Last Occurrence of an Element from a Vector in C++? In C++, vectors are the same as dynamic arrays with the ability to resize automatically when an element is inserted or deleted, with their storage being handled automatically by the container. In this article, we will learn how to remove the last occurrence of a specific element in a vector. Input: 2 min read How to Replace an Element in a Vector in C++? In this article, we will learn how to replace an element in a vector in C++.The most efficient method to replace the old value with a new value is by using replace() method. Letâs take a look at an example:C++#include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 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 Like