#include#include using namespace std; int main() { vector v{ 10, 20, 30, 40, 50 }; v.erase(v.begin() + 2); // removing element at index 2 // Printing the vector elements after removing the element for (auto i : v) cout << i << " "; return 0; }
10 20 40 50
#include#include using namespace std; int main() { vector v{ 10, 20, 30, 40, 50 }; v.erase(v.begin() + 1, v.begin() + 3); // removing two elements within index 1 and 3 // Printing the vector elements after removing the elements for (auto i : v) cout << i << " "; return 0; }
10 40 50In this example, we have created a vector v of integers with five elements. We have used the vector erase function to remove two elements (20 and 30) using the index range. We have used the begin() function with the vector v to get the iterator pointing to the first element, then added the index position of the start and end elements to be removed (1 and 3) in the two arguments of the erase function. Package/Library: The vector erase function is a built-in function in the C++ Standard Library. The #include