How to Resize a 2D Vector in C++? Last Updated : 18 Nov, 2024 Comments Improve Suggest changes Like Article Like Report In C++, resizing the 2D vector means increasing or decreasing the row and column size of 2D vector. In this article, we will learn different methods to resize a 2D vector in C++.The most efficient way to resize the 2D vector is by using vector resize() function. Let’s take a look at a simple example: C++ #include <bits/stdc++.h> using namespace std; int main() { vector<vector<int>> v = {{1, 2, 3}, {7, 8, 9}, {4, 5, 6}}; // Change the column size v.resize(2); // Change the row size for (auto &i : v) i.resize(3); cout << "Rows: " << v.size() << endl; cout << "Cols: " << v[0].size(); return 0; } OutputRows: 2 Cols: 3Explanation: The column size (number of rows) can be changed using vector resize() on the 2D vector, but the row size (number of columns) has to be changed one by one for individual member vector.C++ also provides other methods to resize the 2D vector. Some of them are as follows:Table of ContentResize Empty 2D VectorUsing assign() MethodResize Empty 2D VectorIf the 2D vector is empty, then the vector resize() function can directly resize the rows and columns simultaneously in a single line. C++ #include <bits/stdc++.h> using namespace std; int main() { vector<vector<int>> v; // Resize the rows and cols of 2d vector v.resize(2, vector<int>(3)); cout << "Rows: " << v.size() << endl; cout << "Cols: " << v[0].size(); return 0; } OutputRows: 2 Cols: 3This method cannot be used on the vectors that contains elements.Using assign() MethodThe vector assign() method works by assigning a newly created 2d vector of desired size to our current vector. C++ #include <bits/stdc++.h> using namespace std; int main() { vector<vector<int>> v(4, vector<int>(5)); // Resize the 2d vector v.assign(2, vector<int>(3)); cout << "Rows: " << v.size() << endl; cout << "Cols: " << v[0].size(); return 0; } OutputRows: 2 Cols: 3Vector assign() creates a new vector, removing all previously stored elements. Comment More infoAdvertise with us Next Article How to Resize a 2D Vector in C++? P prajwalgawande998 Follow Improve Article Tags : C++ Programs C++ cpp-vector CPP Examples Practice Tags : CPP Similar Reads How to Initialize 2D Vector in C++? Initializing a 2D vector refers to the process of assigning initial values to the elements of a 2D vector. In this article, we will learn different methods to initialize a 2D vector in C++.The simplest way to initialize a 2D vector is by passing the elements inside an initializer list. This method i 3 min read How to Iterate 2D Vector in C++? Iterating or traversing a 2D vector means accessing each element of the 2D vector sequentially. In this article, we will explore different methods to iterate over a 2D vector in C++.The simplest way to iterate a 2D vector is by using a range-based for loop. Let's take a look at a simple example that 3 min read How to Find the Size of a Vector in C++? The size of a vector means the number of elements currently stored in the vector container. In this article, we will learn how to find the size of vector in C++.The easiest way to find the size of vector is by using vector size() function. Letâs take a look at a simple example:C++#include <bits/s 2 min read How to Initialize 3D Vector in C++ STL? Initializing 3D vector refers to the process of assigning the initial values to the elements of 3D Vector. In this article, we will learn different methods to initialize the 3D vector in C++.The simplest ways to initialize the 3D vector is by passing the elements inside the initializer list. This me 4 min read How to Merge Two Sorted Vector in C++? Merging two sorted vectors means storing the elements of a vector into a single vector in a sorted order. In this article, we will learn how to merge two sorted vectors in C++.The simplest way to merge the two sorted vectors is by using the merge() function. Letâs take a look at an example:C++#inclu 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 Declare a Vector in C++? In C++, the vector is a dynamic array that can resize itself automatically to accommodate new elements. In this article, we will learn how to declare a vector in C++. Declare a Vector in C++In C++, vectors are defined as the class templates in <vector> header file. So, to declare a std::vector 2 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 Resize an Array of Strings in C++? In C++, the array of strings is useful for storing many strings in the same container. Sometimes, we need to change the size of this array. In this article, we will look at how to resize the array of strings in C++. Resize String Array in C++There is no way to directly resize the previously allocate 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 Like