How to Insert into Vector Using Iterator in C++? Last Updated : 28 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, a vector is a dynamic array that can grow and shrink in size as needed. Vectors are sequence containers representing arrays that can change in size. In this article, we will learn how to insert elements into a vector using an iterator in C++. Example: Input: myVector = {10,20,30}; Output: Vector after insertion: 10 20 100 30Insertion into Vector using Iterator in C++To insert elements into a vector using an iterator in C++ STL, you can use the insert() function that takes the iterator and the element to be inserted. This function will insert the given element at the position referred by the iterator and all the elements to the right of that position will be shifted by one place. C++ Program to Insert into Vector Using Iterator C++ // C++ Program to show how to Insert into a Vector Using an // Iterator #include <iostream> #include <vector> using namespace std; // Driver Code int main() { vector<int> myVector = { 1, 2, 3, 4, 5 }; // Insertion in the middle using iterator vector<int>::iterator it = myVector.begin() + 2; // insertion myVector.insert(it, 10); // Display the vector for (int i = 0; i < myVector.size(); i++) { cout << myVector[i] << " "; } return 0; } Output1 2 10 3 4 5 Time Complexity: O(N), where N is the size of the vector.Sapce Complexity: O(1) Comment More infoAdvertise with us Next Article How to Iterate Through a Vector Without Using Iterators in C++? A amritkumasakv Follow Improve Article Tags : C++ Programs C++ cpp-iterator STL cpp-vector CPP Examples +2 More Practice Tags : CPPSTL Similar Reads How to Insert Elements into a Set Using Iterator in C++? In C++, a set is a container provided by the Standard Template Library(STL) that stores unique elements of the same type in a sorted order. In this article, we will learn how to use an iterator to insert elements into a set in C++. Example: Input: myVector = {10, 20, 30, 40, 50} Output: myVector = { 2 min read How to Traverse Vector using const_iterator in C++? In C++, a const_iterator is a type of iterator that points to constant content means by using a const_iterator, we can read from but cannot write to the element it points to. In this article, we will learn how to use a const_iterator with a vector in C++ STL. Example: Input: myVector = {1, 2, 3} Out 2 min read How to Use Iterator with a Vector in C++? In C++, vectors are the same as dynamic arrays with the ability to resize themselves automatically when an element is inserted or deleted. An iterator is a pointer-like object that can be used to access elements of a container (like an array or a vector). In this article, we will learn how to use an 2 min read How to Iterate Through a Vector Without Using Iterators in C++? In this article, we will learn how to iterate through the vector without using iterator in C++.The most efficient method to iterate through the vector without using iterator is by using traditional for loop. It accesses all the elements using index starting from 0 to vector size() - 1. Letâs take a 2 min read How to Access Vector Element Using Iterator in C++? In C++, vectors are container that works like a dynamic array. STL provides iterators for the traversal of these containers. In this article, we will learn how to access an element in a vector using an iterator in C++. Example Input: myVector = {1, 5, 8, 1, 3, 4} Output: myVector value at index 5 us 2 min read How to Reverse a Vector using STL in C++? Reversing the vector means the swapping last element and first element, second last element and second element, and so on. In this article, we will learn how to reverse a vector using STL in C++.The most efficient method to reverse the vector is by using reverse() function. Letâs take a look at a si 3 min read Like