insert at position in vector c++

To insert an element at a specific position in a vector in C++, you can follow these steps:

  1. Use the insert function of the vector class.
  2. Pass the iterator pointing to the position where you want to insert the element as the first argument to the insert function.
  3. Provide the value of the element that you want to insert as the second argument to the insert function.

For example:

#include <iostream>
#include <vector>

int main() {
    std::vector<int> myVector = {1, 2, 3, 4, 5};
    auto it = myVector.begin() + 2; // Insert at position 2 (index 2)
    myVector.insert(it, 10); // Insert 10 at position 2
    for (int i : myVector) {
        std::cout << i << " ";
    }
    return 0;
}