delete specific vector element c++
To delete a specific element from a vector in C++, you can follow these steps:
- Use the erase() function of the vector to remove the element at the desired position.
- Pass the iterator pointing to the element you want to remove to the erase() function.
- The erase() function will remove the element and shift all the subsequent elements to fill the gap.
Here's a sample code snippet to demonstrate the process:
#include <iostream>
#include <vector>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
// Erase the element at index 2 (3rd element)
vec.erase(vec.begin() + 2);
// Output the modified vector
for (int i : vec) {
std::cout << i << " ";
}
return 0;
}