How to create vector of vectors in C++

vector of vectors C++

In this article we are going to see about vector of vectors in C++ which is equivalent to 2D array in general sense.

vector of vectors vs 2D array

In the previous article about vector in C++, we have reviewed the comparison of vectors with 1D array. We can extend the similar idea here.
We declare a static 2D array as:

Where,
Type_t=any data type like int/float/char etc.
array=array name
row_size=number of rows of the array
column_size=number of columns of the array

So, the static 2D array is restricted by its predefined size (in both dimension). But, vector is dynamic in nature. One can extend as much as possible.
Vector of vectors can be supplementary to the 2D array with additional benefits. We can declare such vector of vectors as:

Where,
Type_t=any data type like int/float/char etc.
myvector =name of the vector of vector

So the outer vector can be thought of as a vector (1D) whose element is also a vector.
Advantages for vector of vectors compared to 2D array, is that one can expand vector as much as he wants. Like, you have no restriction for size. For example,
You need to specify a 2D array as

Or if you want to dynamically allocate, still you need size specifications:

Where m=row_size
n=col_size

So, there is no way that you can avoid size restrictions. You have to restrict yourself by some predefined size-limit. Of course, you can set high max limit like following:

But, it’s wastage of storage simply. Here vector of vectors resolves.
Another important advantage is that in 2D array all the rows must have same dimensions, i.e., every row must have similar number of elements which is column_size.
But for vector, it’s not. Check the following section for example.

Initializing a vector of vectors

You can set a vector of vector to some initial values as per choice like the below way:

Output:

row no: 1
This row has 4 number of elements
Elements are:
1 2 3 4
row no: 2
This row has 4 number of elements
Elements are:
4 5 6 7
row no: 3
This row has 6 number of elements
Elements are:
7 8 9 10 11 12

Creating a vector of vectors without considering size restriction

To show the above example, consider the following case.
Let’s build a vector of vectors with type positive integers. Positive numbers will be entered from terminal as elements and 0 to end the row. Follow the code and output to get the details of the example. The main motivation is to show that you don’t need to specify the row_size or column_size like you do for 2D array.

Instructions
1.Start entering positive integers for each row.
2.Input 0 if you want to stop extending current row.
3.Answer the prompt message appearing for creating new rows
Do you want to create a new row? Press 1 if yes, else 0
1
Ok, you want to create a new row
Enter your elements as many as you want.
Last element should be 0 to indicate the end of row
5 3 2 9 0
End of current row
Do you want to create a new row? Press 1 if yes, else 0
1
Ok, you want to create a new row
Enter your elements as many as you want.
Last element should be 0 to indicate the end of row
67 8 0
End of current row
Do you want to create a new row? Press 1 if yes, else 0
1
Ok, you want to create a new row
Enter your elements as many as you want.
Last element should be 0 to indicate the end of row
25 0
End of current row
Do you want to create a new row? Press 1 if yes, else 0
0

Printing the vector of vector
row no: 1
This row has 4 number of elements
Elements are:
5 3 2 9
row no: 2
This row has 2 number of elements
Elements are:
67 8
row no: 3
This row has 1 number of elements
Elements are:
25

Let us elaborate what we did in the above example and the code.

  • First was to initialize the vector what we have covered already. Second was to create the vector of vectors in C++.
    Creating vector of vector is nothing but similar to how we created vector.
  • Create a 1D vector first (as discussed in article vector in C++)
    Then push it back to the 2D vector as element. The element of 2D vector (vector of vector in our discussion) is a 1D vector. We can create the vector of vectors in that way. The above code has liberty of adding as much column and row as per user need. It’s totally customized, no restriction of size etc. 🙂
  • Last one is about printing. Same as we printed 1D vector, we just add one more loop to iterate the outer vector.

This is the overall concept of vector of vectors in C++.

Other details about vector of vectors in C++

Here are some other details for vector of vectors.

Resize()

If you want to bring 2D array kind of behaviour to vector of declaring a predefined size, resize() function helps.

Following is the example:

Fill Constructor

Another way of doing the same thing is by using fill constructor.

That’s all about vector of vectors in C++.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *