remove and insert vector C++

To remove an element from a vector in C++, you can use the erase() function. The erase() function takes an iterator as its argument, specifying the position of the element to be removed.

Here's an example of how to remove an element from a vector:

#include <iostream>
#include <vector>

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

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

  // Print the updated vector
  for (int num : numbers) {
    std::cout << num << " ";
  }

  return 0;
}

Output:

1 2 4 5

To insert an element into a vector at a specific position, you can use the insert() function. The insert() function takes an iterator and a value as its arguments, specifying the position and the element to be inserted.

Here's an example of how to insert an element into a vector:

#include <iostream>
#include <vector>

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

  // Insert 3 at index 2
  numbers.insert(numbers.begin() + 2, 3);

  // Print the updated vector
  for (int num : numbers) {
    std::cout << num << " ";
  }

  return 0;
}

Output:

1 2 3 4 5

In the examples above, the erase() function is used to remove an element at a specific index from the vector, and the insert() function is used to insert an element at a specific index in the vector.