How to Insert an Element at a Specific Index in a Vector in C++? Last Updated : 22 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, vector allows access to its elements using indexes. In this article, we will learn how to insert an element at a specific index in a vector in C++.The simplest method to insert an element at a specific index in a vector is by using vector insert() method. Let’s take a look at a simple example: C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 2, 3, 4}; // Insert 9 at index 2 v.insert(v.begin() + 2, 9); for (auto i : v) cout << i << " "; return 0; } Output1 2 9 3 4 The vector insert() take the iterator to the position where the element is to be inserted, which we can find by adding the index to the vector begin() iterator.There are also some other methods in C++ to insert an element at a specific index in vector. Some of them are as follows:Table of ContentUsing vector emplace() Using rotate() Manually Shifting Elements and AssigningUsing Vector emplace() The vector emplace() method can also be used in place of vector insert(). The advantage of vector emplace() method is that it inserts the elements in-place avoiding the creation of any extra copies. C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 2, 3, 4}; // Emplace 9 at index 2 v.emplace(v.begin() + 2, 9); for (auto i : v) cout << i << " "; return 0; } Output1 2 9 3 4 Using rotate() The idea is to first insert the element at the end of vector using vector push_back() function and then perform a right rotation one time in the range [i, vector end()) using rotate() algorithm. C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 2, 3, 4}; // Insert 9 at end first v.push_back(9); // Rotate to move element to index 2 rotate(v.begin() + 2, v.end() - 1, v.end()); for (auto i : v) cout << i << " "; return 0; } Output1 2 9 3 4 Manually Shifting Elements and AssigningWe first shift all elements from the given index up to the last element by 1 place to make the space of new element. Then we assign the element at the given index using [] operator but we have to make sure that the vector has enough space for the new element. C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 2, 3, 4}; // Adding a temporrary element v.push_back(0); // Shift elements to the right starting // from the end for (int i = v.size() - 1; i > 2; --i) v[i] = v[i - 1]; // Insert new element at index 2 v[2] = 9; for (auto i : v) cout << i << " "; return 0; } Output1 2 9 3 4 Comment More infoAdvertise with us Next Article How to Find Index of a Given Element in a Vector in C++? A anushamahajan5 Follow Improve Article Tags : C++ Programs C++ STL cpp-vector CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Insert an Element in a Sorted Vector in C++? In C++, inserting element in a sorted vector should be done such that it preserves the order of elements. In this article, we will learn different methods to insert an element in a sorted vector in C++.The recommended way to insert an element is to first find the position of insertion using lower_bo 4 min read How to Replace an Element in a Vector in C++? In this article, we will learn how to replace an element in a vector in C++.The most efficient method to replace the old value with a new value is by using replace() method. Letâs take a look at an example:C++#include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 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 How to Insert Elements into 2D Vector in C++? In C++, 2D vectors provide several built-in methods to insert elements. The efficiency of the insertion depends on where the insertion occurs. In this article, we will explore different ways to insert elements into a 2D vector in C++ and their specific use cases.The simplest way to add elements to a 3 min read How to Add Element at the End of a Vector in C++? Vector allows fast insertion and deletion at the end. In this article, we will learn different ways to add an element at the end of the vector.The easiest way to add an element at the end of vector is by using vector push_back() function. Letâs take a look at a simple example:C++#include <bits/st 4 min read How to Change an Element in a Vector in C++? In C++, vectors are dynamic containers that store the data in a contiguous memory location but unlike arrays, they can resize themselves to store more elements. In this article, we will learn how to change a specific element by its index in a vector in C++.The simplest way to modify an element is by 3 min read Like