Table of Contents
- What are vectors in C++?
- Where can we remove an element from a vector?
- How to remove element by value in vector in C++?
- Using the std::find() function to remove element by value in vector in C++.
- Using the remove() function to remove element by value in vector in C++.
- Using the remove_copy() function to remove element by value in vector in C++.
- Using erase() and remove() functions to remove element by value in vector in C++.
- Conclusion
In this post, we will see how to remove element by value in vector in C++.
Vectors are a highly recognized concept and are widely used in the world of C++ programming. Vectors are flexible and are utilized in both easy and complex codes. This tutorial focuses on and demonstrates the different ways available to remove elements by value in vector in C++.
What are vectors in C++?
A big part of C++ programming, Vectors can be defined as dynamic arrays that are capable of resizing themselves any time an element is inserted or removed into the given vector. The storage of the vectors is automatically managed by the container. The key to this dynamic ability is the fact that the vector elements are accessed and traversed with the help of iterators easily as they are stored contiguously.
Where can we remove an element from a vector?
An element can be only be inserted at the end of a vector. However, there is no such restriction on removing an element from the vector as it is possible to remove an element from the middle, first, or the last of the vector. We can also remove elements by value in vector in C++.
How to remove element by value in vector in C++?
- Using the
std::find()
function. - Using the
remove()
function. - Using the
remove_copy()
function. - Using
erase()
andremove()
functions.
The other available methods to remove element by value in vector in C++ namely the standalone erase()
method, the std::erase()
method, and the remove_if()
method can not be utilized in this case as they remove an element from the given vector based on the position of the element rather than the value.
Using the std::find()
function to remove element by value in vector in C++.
This is a manual method by which we can remove a single element at once from the given vector if it finds a match with the specified value.
The std::find()
function helps in finding an element with the same value as mentioned in the function. Afterward, the found value is then deleted with the help of the generic erase()
method.
The following code uses the std::find()
function to remove element by value in vector in C++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { std::vector<int> v; v.push_back(10); v.push_back(20); v.push_back(40); v.push_back(30); v.push_back(80); v.push_back(20); v.push_back(70); auto it = std::find(v.begin(), v.end(), 20); if(it != v.end()) v.erase(it); for (auto it = v.begin(); it != v.end(); ++it) cout << ' ' << *it; return 0; } |
The above code provides the following output:
The drawback of using this function is that even though the vector might contain multiple matches of the same value as passed in the function, only one element can be erased from the given vector at once.
Further reading:
Using the remove()
function to remove element by value in vector in C++.
We can utilize the remove()
function that removes all elements that match a certain specified value. Afterward, the iterator’s position is at the new end of the range of elements of the vector.
The syntax for the remove()
function is quite simple and is mentioned below for ease of understanding.
1 2 3 |
remove(v.begin(),v.end(),val) |
The above function takes in the following parameters:
- first: The beginning of the specified range of elements. The range includes the
first
element as well. - last: The end of the specified range of elements. The range does not include the
last
element and it is just a reference point. - val: The value that needs to be removed.
The following code uses the remove()
function remove element by value in vector in C++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include<bits/stdc++.h> using namespace std; int main(){ vector<string> v; v.push_back("hello"); v.push_back("java2blog"); v.push_back("viewers"); v.push_back("hello"); v.push_back("are"); v.push_back("geniuses"); v.push_back("really"); vector<string>::iterator new_end; new_end = remove(v.begin(), v.end(), "hello"); for(int i=0;i < v.size(); i++){ cout<< v[i] << " "; } return 0; } |
The above code provides the following output:
As we can see from the above code, the remove()
function removes the occurrence of the given value hello
in the given string vector successfully. However, we noted that the output for int vectors
might have some disparity from the expected output.
Using the remove_copy()
function to remove element by value in vector in C++.
The remove_copy()
function copies all the elements except for the matches values to another vector in order to eliminate some values in vector in C++.
The syntax for the remove_copy()
function is quite simple and is mentioned below for ease of understanding.
1 2 3 |
remove_copy(vector1.begin(),vector1.end(),vector2.begin(),val) |
The above function takes in the following parameters:
- vector1.begin(): The beginning of the specified range of elements of the original vector.
- vector1.end: The end of the specified range of elements of the original vector.
- vector2.begin(): The beginning or the start point of the second vector that is used for the copy process.
- val: The value that needs to be removed, or in this case, the one which does need to be copied.
The following code uses the remove_copy()
function remove element by value in vector in C++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<pre code = "c++" #include <bits/stdc++.h> using namespace std; int main() { vector<int> v {50,30,10,30,20,20,80,90}; vector<int> vf(8); remove_copy(v.begin(), v.end(), vf.begin(), 30); for(int i=0;i<vf.size();i++) { cout << vf[i] << " "; } return 0; } </pre> |
The above code provides the following output:
The element 30
occurs thrice in the original vector and is not copied to the second vector. The 3 spaces of the second vector are taken as 0 by default.
Using erase()
and remove()
functions to remove element by value in vector in C++.
A combination of the erase()
and the remove()
functions can be utilized to remove elements by value in vector in C++. To utilize the remove()
function, we will have to add an algorithm
header to the C++ code.
The following code uses erase()
and remove()
functions to remove element by value in vector in C++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { vector<string> v; v.push_back("hello"); v.push_back("java2blog"); v.push_back("viewers"); v.push_back("hello"); v.push_back("are"); v.push_back("geniuses"); v.push_back("really"); cout<<"Original Vector is:"; for (auto it = v.begin(); it != v.end(); ++it) cout << ' ' << *it; v.erase(remove(v.begin(),v.end(),"hello"),v.end()); cout<<"\nAfter Removing elements vector is:"; for (auto it = v.begin(); it != v.end(); ++it) cout << ' ' << *it; return 0; } |
The above code provides the following output:
After Removing elements vector is: java2blog viewers are geniuses really
We should note that this method does not work in the cases where the const_iterator
is used.
Conclusion
We discussed four different methods to remove elements by value in vector in C++. The std::find()
function only removes one element from the given vector. The remove()
function works perfectly for string vectors
but might not give the desired output when dealing with int vectors
. The other functions work efficiently to remove one or multiple elements matching a given value in vector in C++.
That’s all about how to remove element by value in vector in C++