How to Insert Elements into 2D Vector in C++?
Last Updated :
22 Nov, 2024
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 2D vector is by appending the rows of elements (1D vectors) to the end using the vector push_back() method. Let's take a look at an example:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<vector<int>> v = {{1, 2, 3},
{4, 5, 6}};
// Adding a new row at the end
v.push_back({7, 8, 9});
// Adding an element to the last row
v[2].push_back(10);
for (const auto& i : v) {
for (int j : i)
cout << j << " ";
cout << endl;
}
return 0;
}
Output1 2 3
4 5 6
7 8 9 10
Inserting elements in the middle is an expensive operation in a vector because all elements after the insertion point need to be shifted.
Using Vector insert()
The vector insert() method is versatile and can insert elements at any position in a 2D vector. It can also insert multiple elements simultaneously.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<vector<int>> v = {{1, 3, 5},
{7, 9, 11}};
// Inserting a new row at position 1
v.insert(v.begin() + 1, {4, 6, 8});
// Inserting a value into 2nd row at position 2
v[1].insert(v[1].begin() + 2, 10);
for (const auto& i : v) {
for (int j : i)
cout << j << " ";
cout << endl;
}
return 0;
}
Output1 3 5
4 6 10 8
7 9 11
Using vector emplace()
The vector emplace() method works similarly to vector insert(), but instead of copying an element, it constructs the element directly in the vector. This avoids unnecessary copies, improving performance.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<vector<int>> v = {{1, 3, 5},
{7, 9, 11}};
// Emplacing a new row at position 1
v.emplace(v.begin() + 1, vector<int>{4, 6, 8});
// Emplacing value into 2nd row at position 2
v[1].emplace(v[1].begin() + 2, 10);
for (const auto& i : v) {
for (int j : i)
cout << j << " ";
cout << endl;
}
return 0;
}
Output1 3 5
4 6 10 8
7 9 11
Using vector emplace_back()
The vector emplace_back() method is the emplacing counterpart of push_back(). It constructs the new element directly in the vector, avoiding extra copies.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<vector<int>> v = {{1, 3, 5},
{7, 9, 11}};
// Emplacing a new row at the end
v.emplace_back(vector<int>{4, 6, 8});
// Emplacing a value in the last row
v[2].emplace_back(10);
for (const auto& i : v) {
for (int j : i)
cout << j << " ";
cout << endl;
}
return 0;
}
Output1 3 5
7 9 11
4 6 8 10
Similar Reads
How to Insert Elements from Vectors to a Map in C++? In C++, vectors are the dynamic array that stores data in contiguous memory locations while maps are the data containers that store the key-value pair and are sorted on the basis of the key. In this article, we will learn how to efficiently map elements from vectors to a map in C++. Example Input: v
2 min read
How to Add Elements in a Vector in C++? In C++, vector provides several built-in methods to insert the elements and efficiency of the insertion depends on the position where the insertion takes place. In this article, we will learn different ways to insert elements into a vector in C++ and also compare their efficiency.The simplest way to
3 min read
How to Add Element to Vector of Pairs in C++? In C++, vectors are dynamic arrays that can grow and shrink in size whereas a pair is a container that can store two data elements or objects. A vector of pairs, therefore, is a dynamic array of pairs. In this article, we will learn how to add an element to a vector of pairs in C++. Example: Input:v
2 min read
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 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