Construct a vector of vectors in C++
This post provides an overview of the available alternatives to construct a vector of vectors in C++.
In C++, we can define a vector of vectors of ints as follows:
|
1 |
vector<vector<int>> v; |
The above definition results in an empty two-dimensional vector. To use it, we have to define the vector size and allocate storage for its elements. There are several methods to grow a two-dimensional vector with the help of resize() or push_back() functions or using the fill constructor or initializer lists. Now let’s explore each alternative in detail:
1. Using resize() function
The resize() function is used to resize a vector to the specified size. We can use it to initialize a vector of vectors, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <vector> using namespace std; #define R 4 #define C 5 int main() { // instantiate a vector of `R` objects of type vector<int> // and resize each object to size `C` vector<vector<int>> mat(R); for (int i = 0 ; i < R ; i++) { mat[i].resize(C); } // print the vector return 0; } |
We can picture a vector of vectors as a two-dimensional array consisting of R rows and C columns. Here’s an alternative version of the above code, which uses an overloaded version of the resize() function, which accepts the container size, and the object to be copied in that container.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <vector> using namespace std; #define R 4 #define C 5 int main() { // instantiate vector object of type vector<int> vector<vector<int>> mat; // resize the vector to `R` elements of type vector<int>, each having size `C` mat.resize(R, std::vector<int>(C)); // print the vector return 0; } |
2. Using push_back() function
Another plausible way of initializing a vector of vectors is to use the push_back() function, which adds a given element at the vector’s end. The following C++ program demonstrates it:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <iostream> #include <vector> using namespace std; #define R 4 #define C 5 int main() { // instantiate vector object of type vector<int> and // use `push_back()` function to resize it vector<vector<int>> mat; for (int i = 0; i < R; i++) { // construct a vector of int vector<int> v; for (int j = 0; j < C; j++) { v.push_back(0); } // push back above one-dimensional vector mat.push_back(v); } // print the vector return 0; } |
Note when dimensions R and C are large, the above code suffers from potential performance penalties caused by frequent reallocation of memory by the push_back() function. This should be used only when vector dimensions are not known in advance.
3. Using Fill Constructor
The recommended approach uses a fill constructor of the vector container for constructing a vector of vectors. The fill constructor accepts an initial size n and a value and creates a vector of n elements, and fills with the specified default value.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <vector> using namespace std; #define R 4 #define C 5 int main() { // Using the fill constructor to construct a vector of vectors vector<vector<int>> mat(R, vector<int>(C)); // print the vector return 0; } |
We can split the above initialization into two parts – first initialize a vector of ints and then use it to initialize the vector of vectors using the fill constructor. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <vector> using namespace std; #define R 4 #define C 5 int main() { // first, initialize a vector of ints vector<int> v(C); // Use the above vector to initialize the vector of vectors // using the fill constructor vector<vector<int>> mat(R, v); // print the vector return 0; } |
4. Using Initializer list
Finally, we can use initializer lists introduced with C++11 to construct a vector of vectors, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <vector> using namespace std; int main() { // using an initializer list to construct a vector of vectors vector<vector<int>> mat { { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 } }; // print the vector return 0; } |
How to print the vector of vectors?
The following procedure would display a vector of vectors of an integer using nested loops:
|
1 2 3 4 5 6 7 8 9 10 |
template<class T> void printVector(vector<vector<T>> const &mat) { for (vector<T> row: mat) { for (T val: row) { cout << val << " "; } cout << endl; } } |
That’s all about constructing a vector of vectors in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)