
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove Item from C++ STL Vector by Value
To remove an item from a C++ STL vectors by value, you can use the std::remove inside the argument of erase method of the vector. There are some other methods as well. In this article, we will learn all the approaches to remove an item from a C++ STL vector.
The easiest way to remove an item from a vector is to use the std::remove algorithm inside the erase method of the vector. For example:
vector<int> v = {1, 2, 2, 3, 2, 5}; // Remove all occurrences of 2 v.erase(remove(v.begin(), v.end(), 2), v.end());
Remove an Item from a Vector
Here is the list of all the approaches to remove an item from a vector in C++ STL:
Remove Item from Vector Using std::remove and erase
In this approach, we will use the std::remove algorithm to shift all elements that are not equal to the specified value to the front of the vector, and then we will use the erase method to remove the specified value from the end of the vector. All the occurrences of the specified value will be removed.
Example
In the code below, we will create a vector with some integers and then remove all occurrences of the integer 2.
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { vector<int> v = {1, 2, 2, 3, 2, 5}; // Remove all occurrences of 2 v.erase(remove(v.begin(), v.end(), 2), v.end()); cout << "Vector after removing 2: "; for (const auto& item : v) { cout << item << " "; } cout << endl; return 0; }
The output of the above code will be:
Vector after removing 2: 1 3 5
Remove Item from Vector Using std::find and erase
In this approach, we will use the std::find algorithm to find the first occurrence of the specified value in the vector, and then we will use the erase method to remove it. This method will only remove the first occurrence of the specified value.
Example
In the code below, we will remove the first occurrence of the value 2 from the vector.
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { vector<int> v = {1, 2, 2, 3, 2, 5}; // Find and remove the first occurrence of 2 auto it = find(v.begin(), v.end(), 2); if (it != v.end()) { v.erase(it); } cout << "Vector after removing first occurrence of 2: "; for (const auto& item : v) { cout << item << " "; } cout << endl; return 0; }
The output of the above code will be:
Vector after removing first occurrence of 2: 1 2 3 2 5
Remove Item from Vector Using Loop
In this approach, we will use a loop to iterate through the vector and remove the specified value using the erase method. This method is more manual and you can decide how many occurrences to be removed.
Example
In the code below, we will remove all occurrences of the value 2 from the vector using a loop.
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { vector<int> v = {1, 2, 2, 3, 2, 5}; // Remove all occurrences of 2 using a loop for (auto it = v.begin(); it != v.end();) { if (*it == 2) { it = v.erase(it); // erase returns the next iterator } else { ++it; } } cout << "Vector after removing all occurrences of 2: "; for (const auto& item : v) { cout << item << " "; } cout << endl; return 0; }
The output of the above code will be:
Vector after removing all occurrences of 2: 1 3 5