. Advertisement .
..3..
. Advertisement .
..4..
This instruction shall introduce to you the best approaches to remove element from vector C ++. Wait for no more but dig in for further helpful details!
Vector In C++
Vectors have the same properties as dynamic arrays, including the capability to efficiently resize when an element is added or removed and automated storage management by the container.
That way, iterators can access and navigate vector items since they are stored in contiguous storage.
What’s more, data is added at the end of vectors. Inserting at the end requires more time since the array may occasionally need to be enlarged.
On the contrary, removing the last element consumes only constant time due to no happening for resizing. I guess we can just say time is linear when information is added or deleted in the beginning or midway.
How To Remove Element From Vector C++
Method #1: Adopt std::erase() and std::remove()
This technique, also known as the “Erase-remove idiom,” eliminates all elements from a range that match a specific value or a predetermined condition.
You should be aware that such a solution has certain unique characteristics, like the inability to be utilized with containers returning const iterators.
Running the code:
#include <iostream>
#include <algorithm>
#include <vector>
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::vector;
using std::erase;
void PrintVec(vector<string> &vec)
{
for (const auto &item : vec) {
cout << item << "; ";
}
cout << endl;
}
int main() {
vector<string> str_vec = {"man", "volka",
"desk", "loan",
"settle", "man",
"stock", "quiz",
"man", "spin"};
PrintVec(str_vec);
// DELETES all elements with value "man"
str_vec.erase(std::remove(str_vec.begin(), str_vec.end(), "map"), str_vec.end());
PrintVec(str_vec);
return EXIT_SUCCESS;
}
Output:
man; volka; desk; loan; settle; man; stock; quiz; man; spin;
volka; desk; loan; settle; stock; quiz; spin;
Method #2: Utilize the std::erase() Method
This non-member method, std::erase()
, applies to a variety and a value to compare with each element before deleting it when a match is made.
If just one element has to be deleted, such an approach may not be ideal. However, you may circumvent this problem by giving the second parameter as an array variable, such as arr[index]
.
The element array is the only thing wipe removes, as seen in the following example.
Running the code:
#include <iostream>
#include <vector>
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::vector;
using std::erase;
void PrintVec(vector<string> &vec)
{
for (const auto &item : vec) {
cout << item << "; ";
}
cout << endl;
}
int main() {
vector<string> str_vec = {"alley", "volka",
"desk", "loan",
"settle", "man",
"stock", "quiz",
"malfunction", "spin"};
PrintVec(str_vec);
// DELETE elemen "alley"
erase(str_vec, str_vec[0]);
PrintVec(str_vec);
return EXIT_SUCCESS;
}
Output:
alley; volka; desk; loan; settle; man; stock; quiz; malfunction; spin;
volka; desk; loan; settle; stock; quiz; malfunction; spin;
Method #3: Employ the erase() Method
The [first, last] range or a single element of the vector can be erased using the erase()
method, which is a member function of the std::vector
class.
Along with the last element that was deleted, the method produces an iterator. The iterator supplied must be dereferenceable if only one element is deleted, as is the case in the following code snippet.
Running the code:
#include <iostream>
#include <vector>
using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector;
void PrintVec(vector<string> &vec)
{
for (const auto &item : vec) {
cout << item << "; ";
}
cout << endl;
}
int main() {
vector<string> str_vec = {"alley", "volka",
"desk", "loan",
"settle", "man",
"stock", "quiz",
"malfunction", "spin"};
PrintVec(str_vec);
// DELETE element "settle"
auto elem_to_remove = str_vec.begin() + 4;
if (elem_to_remove != str_vec.end()) {
str_vec.erase(elem_to_remove);
}
PrintVec(str_vec);
return EXIT_SUCCESS;
}
Output:
alley; volka; desk; loan; settle; man; stock; quiz; malfunction; spin;
alley; volka; desk; loan; man; stock; quiz; malfunction; spin;
Conclusion
How to remove element from vector C++ has been outlined in our above tutorial. Hopefully, this article can be of great use to your C++ conquest. See then!
Leave a comment