remove elements from vector

Example of Removing Elements from a Vector in C++

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> vec = {1, 2, 3, 4, 5};

    vec.erase(vec.begin() + 2); // Remove element at index 2

    vec.erase(vec.begin() + 1, vec.begin() + 3); // Remove elements in the range [1, 3)

    for (int i : vec) {
        std::cout << i << " ";
    }

    return 0;
}

Explanation:

1. Include Necessary Headers: - We include the necessary headers for input/output, vectors, and algorithms.

2. Create a Vector: - We create a vector of integers and initialize it with values {1, 2, 3, 4, 5}.

3. Remove Element at Specific Index: - We use the erase function to remove the element at index 2 from the vector.

4. Remove Elements in a Range: - We use the erase function with a range to remove elements from index 1 to 2 (not including 3) from the vector.

5. Output the Modified Vector: - We use a for loop to output the elements of the modified vector after the removal operations.

6. Return 0: - We return 0 to indicate successful execution of the program.