Vector insert() in C++ STL Last Updated : 19 May, 2025 Comments Improve Suggest changes Like Article Like Report In C++, the vector insert() is a built-in function used to insert new elements at the given position in a vector. In this article, we will learn about the vector insert() function in C++. Let’s take a look at an example that shows the how to use this function: C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 3, 7, 9}; // Insert element 8 at index 2 v.insert(v.begin() + 2, 8); for (auto i : v) cout << i << " "; return 0; } Output1 3 8 7 9 Explanation: The function inserts the element 8 at the 2nd index of vector.Syntax of Vector insert()The vector insert() is a member function of std::vector class defined inside <vector> header file and have 3 implementations: C++ v.insert(pos, val); // Insert single element v.insert(pos, n, val); // Insert multiple copies of an element v.insert(pos, {val1, val2, ...}) // Insert list of elements v.insert(pos, first, last); // Insert range of elements These implementations work on different cases which are as follows:Table of ContentInsert an Element at Given IndexInsert Multiple Copies of an ElementInsert List of ElementsInsert the Range of ElementsInsert an Element at Given IndexWe can use the vector insert() function to insert the element at the given index. All the elements to the right of the given index will be shifted once place to the right to make space for new element.Syntax C++ v.insert (pos, val); Parametersval: Value to be inserted.pos: Iterator to the position where val is to be inserted.Return ValueReturns an iterator to the inserted element.The insert() function is crucial for adding elements to a vector at specific positions. Example C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 2, 4, 5}; // Insert element 3 at index 2 v.insert(v.begin() + 2, 3); for (auto i : v) cout << i << " "; return 0; } Output1 2 3 4 5 Explanation: The vector insert() function insert the element 3 at the 2nd index and all the elements to the right of the 2nd index will be shifted once place to the right to make space for new element.Insert Multiple Copies of an ElementThe vector insert() can also be used to insert multiple copies of an element at the given index.Syntax C++ v.insert(pos, n, val) Parametersval - Value to be inserted.pos: Iterator to the position where val is to be inserted.n: Number of times val is to be inserted.Return ValueReturns an iterator that points to the first inserted element.If no element is inserted, then returns iterator to the pos.Example C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 3, 4, 5}; // Inserting value 9, 3 times at index 2 v.insert(v.begin() + 1, 3, 2); for (auto i : v) cout << i << " "; return 0; } Output1 2 2 2 3 4 5 Insert List of ElementsThe vector insert() function can also insert elements from an initializer list at given index. C++ v.insert(pos, {val1, val2, ...}); Parameterspos- Iterator to the position where range is to be inserted.{val1, val2, ...} - Initializer list containing values to insert.Return ValueReturns an iterator that points to the first inserted element.If no element is inserted, then returns iterator to the pos.Example C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v{1, 2, 3,}; // Inserting elements to v v.insert(v.begin() + 3, {4, 5}); for (auto i : v) cout << i << " "; return 0; } Output1 2 3 4 5 Insert the Range of ElementsThe vector insert() function can also insert elements from a range into the vector at given index. This range can be any STL container or an array. C++ v.insert(pos, first, last) Parameterspos- Iterator to the position where range is to be inserted.first- Iterator to the first element in the range from which the elements are to be inserted.last- Iterator to the element one place after the last element in the range.Return ValueReturns an iterator that points to the first inserted element.If no element is inserted, then returns iterator to the pos.Example C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v{1, 2, 3,}; list<int> l{4, 5}; // Inserting all elements of l to v v.insert(v.begin() + 3, l.begin(), l.end()); for (auto i : v) cout << i << " "; return 0; } Output1 2 3 4 5 Vector insert() vs push_back()The vector insert() and vector push_back() are both used to insert new elements in a vector, but they differ in the way they perform insertion. Following table lists the primary differences between vector insert() and vector push_back():FeatureVector insert()Vector push_back()PositionInserts at any specified position.Always appends at the end of the vector.Number of ElementsCan insert single or multiple elements or a range.Inserts only one element at a time.Element ShiftingShifts elements after the insertion point.No shifting of elements (added at the end).Time ComplexityO(n)O(1) Comment More infoAdvertise with us Next Article vector emplace() in C++ STL T Twinkl Bajaj Follow Improve Article Tags : Misc C++ STL CPP-Functions cpp-vector cpp-containers-library +2 More Practice Tags : CPPMiscSTL Similar Reads Vector in C++ STL C++ vector is a dynamic array that stores collection of elements same type in contiguous memory. It has the ability to resize itself automatically when an element is inserted or deleted.Create a VectorBefore creating a vector, we must know that a vector is defined as the std::vector class template i 7 min read 8 Ways to Initialize Vector in C++ Initializing a vector means assigning some initial values to the std::vector elements. In this article, we will learn 8 different ways to initialize a vector in C++.Table of ContentUsing Initializer ListOne by One InitializationWith a Single ValueFrom an ArrayFrom Another VectorFrom Any STL Containe 5 min read Commonly Used MethodsVector end() in C++ STLIn C++, the vector end() is a built-in method used to obtain an iterator pointing to the theoretical element after the last element of the vector. Even though this iterator does not point to a valid element, it serves as a marker for the end of the vector.Letâs take a look at an example that illustr 3 min read Vector empty() in C++ STLIn C++, vector empty() is a built-in method used to check whether the given vector is empty or not. In this article, we will learn about vector empty() method in C++.Letâs take a look at an example that illustrates the vector empty() method:C++#include <bits/stdc++.h> using namespace std; int 2 min read Vector operator[ ] in C++ STLIn C++, the vector operator [] is used to randomly access or update the elements of vector using their indexes. It is similar to the vector at() function but it doesn't check whether the given index lies in the vector or not.Letâs take a look at a simple code example:C++#include <bits/stdc++.h 3 min read Vector front() in C++ STLIn C++, the vector front() is a built-in function used to retrieve the first element of the vector. It provides a reference to the first element, allowing you to read or modify it directly.Letâs take a quick look at a simple example that uses the vector front() method:C++#include <bits/stdc++.h 2 min read Vector push_back() in C++ STLIn C++, the vector push_back() is a built-in method used to add a new element at the end of the vector. It automatically resizes the vector if there is not enough space to accommodate the new element.Letâs take a look at an example that illustrates the vector push_back() method:C++#include <bits/ 2 min read Vector insert() in C++ STLIn C++, the vector insert() is a built-in function used to insert new elements at the given position in a vector. In this article, we will learn about the vector insert() function in C++. Letâs take a look at an example that shows the how to use this function:C++#include <bits/stdc++.h> using 4 min read vector emplace() in C++ STLIn C++, vector emplace() is used to insert elements at the given position in a vector by constructing it in-place within the vector, rather than creating a temporary object and then moving or copying it into the vector.Let's take a quick look at a simple example that uses vector emplace() method:C++ 3 min read Vector assign() in C++ STLIn C++, the vector assign() is a built-in method used to assign the new values to the given vector by replacing old ones. It also modifies the size of the vector according to the given number of elements. Letâs take a look at an example that shows the how to use this function.C++#include <bits/st 4 min read Vector erase() in C++ STLIn C++, vector erase() is a built-in function that is used to delete elements from the vector. It removes an element of a specific position or range of elements from the vector. Letâs take a simple example that uses the vector erase() method:C++#include <bits/stdc++.h> using namespace std; int 3 min read Other Member MethodsVector max_size() in C++ STLIn C++, vector max_size() is a built-in function used to find the maximum number of elements that can be held by the vector container. In this article, we will learn about the vector max_size() function in C++.Let's take a look at an example that illustrates this method:CPP#include <bits/stdc++.h 3 min read Vector capacity() in C++ STLIn C++, the vector capacity() is a built-in method used to find the capacity of vector. The capacity indicates how many elements the vector can hold before it needs to reallocate additional memory. In this article, we will learn about vector capacity() method in C++.Letâs take a look at an example t 3 min read vector rbegin() and rend() Functions in C++ STLIn C++, std::vector::rbegin() and std::vector::rend() are built-in functions used to retrieve reverse iterators to the reverse beginning and reverse end of a vector. These functions allow easy traversal of vectors in reverse i.e. from the last element to the first. They are the member functions of t 3 min read vector :: cbegin() and vector :: cend() in C++ STLVectors are known as dynamic arrays which can change its size automatically when an element is inserted or deleted. This storage is maintained by container. vector::cbegin() The function returns an iterator which is used to iterate container. The iterator points to the beginning of the vector.Iterat 2 min read Vector crend() in C++ STLIn C++, the vector crend() is a built-in method used to obtain a constant reverse iterator pointing to the theoretical element just before the first element of the vector. It is used to mark the reverse end of the vector.Letâs take a look at an example: C++#include <bits/stdc++.h> using namesp 2 min read Vector resize() in C++ STLIn C++, the vector resize() is a built-in method used to change the size of vector container after it is declared. It can be used to increase or decrease the size of vector.Letâs take a look at an example that illustrates the vector resize() method:C++#include <bits/stdc++.h> using namespace s 3 min read Vector shrink_to_fit() in C++ STLIn C++, vector shrink_to_fit() is a built-in function used to reduce the capacity of the vector to fit its size and destroys all elements beyond the size. In this article we will learn about vector shrink_to_fit() in C++.Letâs take a quick look at an example that illustrates vector shrink_to_fit() m 2 min read When to use Vector reserve() in C++?In a vector, when the number of elements to be inserted are greater than the current capacity, it is reallocated to a larger memory block and all the items are copied to this new block making this reallocation an expensive operation with time complexity of O(n).This mechanism works well if you don't 2 min read Vector data() in C++ STLIn C++, the vector data() is a built-in function used to access the internal array used by the vector to store its elements. In this article, we will learn about vector data() in C++.Letâs take a look at an example that illustrates the vector data() method:C++#include <bits/stdc++.h> using nam 2 min read 2D Vector in C++ A 2D vector is a vector of the vectors i.e. each element is a vector in itself. It can be visualised as a matrix where each inner vector represents a row, and the number of rows represents the maximum columns. A 2D vector is dynamically resizable in both dimensions.SyntaxC++vector<vector<data_ 7 min read Passing Vector to a Function in C++ To perform operations on vector belonging to one function inside other function, we pass this vector to the function as arguments during the function call.C++ provides three methods to pass a vector to a function in C++. Let's look at each of them one by one.Table of ContentPass Vector by ValuePass 5 min read How Does a Vector Internally Works in C++? In C++, a vector is a dynamic array that can resize itself when more elements are added or deleted. So, one may ask that how a vector internally works to achieve its dynamic resizing capability while maintaining similar efficiency to static arrays in operations?For vector to work as a resizable dyna 5 min read How to implement our own Vector Class in C++? The given task is to implement a class in C++ which behaves just like the Vector class.Vectors are the same as dynamic arrays with the ability to resize themselves automatically when an element is inserted or deleted, with their storage being handled automatically by the container. Vector elements a 5 min read Advantages of Vector Over Array in C++ In C++, both vectors and arrays are used to store collections of elements, but vector offers significant advantages over arrays in terms of flexibility, functionality, and ease of use. This article explores the benefits of using vectors in C++ programming.The major advantages of vector over arrays a 3 min read Common Vector ProgramsSorting a Vector in C++Sorting a vector means arranging the elements of vector in ascending order or in descending order or in desired defined order. In this article, we will learn about different ways to sort the vector in C++.The most efficient way to sort the vector is by using sort() method. Letâs take a look at an ex 3 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 How to Find the Minimum and Maximum Element of a Vector Using STL in C++?In this article, we will learn how to find the minimum and maximum element in vector in C++.The simplest method to find the minimum and maximum element in vector is by using min_element() and max_element(). Letâs take a look at a simple example:C++#include <bits/stdc++.h> using namespace std; 2 min read How to Find Index of a Given Element in a Vector in C++?Vectors stores elements in contiguous memory and these elements can be accessed by their indexes. In this article, we will learn the reverse process, i.e., finding the index of the given element in a vector in C++.The simplest way to find the index of the given element in the vector is by using find 3 min read Like