Avatar ·

How to remove an element from std::vector by index in C++?

Hello colleagues! I'm new to C++ and encountered a problem when working with std::vector. I need to remove an element by its index, but I can't find the correct way. Here's my code: ```cpp #include #include int main() { std::vector numbers = {10, 20, 30, 40, 50}; int index_to_remove = 2; // Want to remove element 30 // Trying to use erase, but don't know how to pass the index // numbers.erase(index_to_remove); // Compilation error // Trying via iterator numbers.erase(numbers.begin() + index_to_remove); for (int n : numbers) { std::cout << n << " "; } // Expected: 10 20 40 50, but code crashes or behaves strangely return 0; } ``` I read the documentation but still get confused. Why does `erase` n