vector_name.insert(iterator_position, value);
#include#include using namespace std; int main() { vector v = {1, 2, 3, 5, 6}; // Inserting a new element at the 4th position v.insert(v.begin() + 3, 4); // Printing the vector after insertion for (auto i : v) { cout << i << " "; } cout << endl; return 0; }
#includeIn this example, a new string element with a value of "orange" is inserted at the beginning of the vector. Package Library: The C++ Standard Library.#include using namespace std; int main() { vector v = {"apple", "banana", "grape"}; // Inserting a new element at the beginning v.insert(v.begin(), "orange"); // Printing the vector after insertion for (auto s : v) { cout << s << " "; } cout << endl; return 0; }