In C++, the std::vector is a sequential container class from the Standard Template Library (STL). A vector is a dynamic array that can grow or shrink in size when elements are added or removed. In this article, we will learn how to remove an item from a vector with a certain value.
Example:Input: my_vector = {1, 3, 1, 4, 4, 6, 5, 6, 6} value_to_remove = 4
Output: my_vector = {1, 3, 1, 6, 5, 6, 6}
Explanation: In the above example, the input my_vector contains the value 4 twice. The goal is to remove all occurrences of the value 4 from my_vector. Ways to Remove an Item From a VectorThere are two efficient approaches for removing an item with a certain value from a vector in C++:
- Using the std::remove and std::erase functions
- Using the std::vector::erase with a loop
1. Remove Item Using std::remove and std::eraseIn C++, the std::remove function, declared in the <algorithm> header, rearranges the elements in the vector such that the elements to be removed are moved to the end of the vector. The std::erase function is then used to remove the elements beyond the new logical end of the vector.
Syntax of std::removeremove(first, last, value); Syntax of std::erasevector.erase(first, last); Example:Below is the implementation of the above approach to remove an item with a certain value from a vector using std::remove() and std::erase() function in C++.
C++
// C++ Program to illustrate how to remove an item from a
// vector using remove and erase functions
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> my_vector = { 1, 3, 1, 4, 4, 6, 5, 6, 6 };
int value_to_remove = 4;
// using remove function
auto it = remove(my_vector.begin(), my_vector.end(),
value_to_remove);
// erase the 'removed' elements
my_vector.erase(it, my_vector.end());
cout << "Vector after removing value "
<< value_to_remove << ": ";
for (int item : my_vector) {
cout << item << " ";
}
return 0;
}
OutputVector after removing value 4: 1 3 1 6 5 6 6 Time Complexity: O(n), where n is the number of elements Auxiliary Space: O(1)
2. Remove Item Using std::vector::erase with a LoopAnother method is to use a loop to iterate through the vector and remove the elements with the specified value using std::vector::erase method.
Example:The below example demonstrates how we can remove an item from a vector with a certain value using std::erase() method in C++.
C++
// C++ Program to illustrate how to remove an item from a
// vector using erase with a loop
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// Initialize the vector with elements
vector<int> my_vector = { 1, 3, 1, 4, 4, 6, 5, 6, 6 };
// Value to remove from the vector
int value_to_remove = 4;
// Iterate through the vector
for (auto it = my_vector.begin();
it != my_vector.end();) {
// Check if the current element is the value to
// remove
if (*it == value_to_remove) {
// Erase the element and update the iterator
it = my_vector.erase(it);
}
else {
// Move to the next element
++it;
}
}
// Print the vector after removing the specified value
cout << "Vector after removing value "
<< value_to_remove << ": ";
for (int item : my_vector) {
cout << item << " ";
}
return 0;
}
OutputVector after removing value 4: 1 3 1 6 5 6 6 Time Complexity: O(n), where n is the number of elements Auxiliary Space: O(1)
ConclusionIn this article, we explored two methods to remove an item from a vector with a certain value in C++. Both approaches are efficient and commonly used in practice. Depending on the use case, you can choose the method that best suits your needs.By understanding these techniques, we can effectively manage and manipulate data in vectors, making your C++ programming more robust and versatile.
|