To remove a specific element from an array in C++, you can use the `erase` method of vectors or create a new vector excluding the element to be removed.
In this example,
#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers = {1, 2, 3, 4, 5};
int elementToRemove = 3;
numbers.erase(std::remove(numbers.begin(), numbers.end(), elementToRemove), numbers.end());
std::cout << "Vector after removing " << elementToRemove << ": ";
for (int num : numbers) {
std::cout << num << ' ';
}
return 0;
}
Vector after removing 3: 1 2 4 5
In this tutorial, we learned How to Remove Specific Element from an Array in C++ language with well detailed examples.