Open In App
Related Articles

2D Vector In C++ With User Defined Size

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

A 2D vector is a vector of the vector. Like 2D arrays, we can declare and assign values to a 2D vector!


Assuming you are familiar with a normal vector in C++, with the help of an example we demonstrate how a 2D vector differs from a normal vector below: 

C++

/*
C++ program to demonstrate a 2D vector where
each of its elements is of different size.
*/
#include <iostream>
#include <vector>
using namespace std;
int main()
{
    /*
    We initialize a 2D vector 
    named "vect" on line 16 with
    different number of values 
    in each element.
    */
      
    vector<vector<int>> vect
    {
        /* Element one with 2 values in it. */
        {1, 2}, 
        
        /* Element two with 3 values in it. */
        {4, 5, 6}, 
        
         /* Element three with 4 values in it. */
        {7, 8, 9, 10} 
    };
  
    /*
    Now we print the vector that we 
    just defined using a simple
    nested for loop.
    */
      
    for (int i = 0; i < vect.size(); i++) 
    {
        for (int j = 0; j < vect[i].size(); j++)
        {
            cout << vect[i][j] << " ";
        }    
        cout << endl;
    }
    return 0;
}    

                    

In a 2D vector, every element is a vector.

Time Complexity: O(1)

Auxiliary Space: O(1)

C++

/* 
C++ program to create a 2D vector where 
every row has a certain number of values
as defined by the user.(On line 13)
*/   
  
#include <iostream>
#include <vector>
using namespace std;
int main()
{
      
    /* Here we tell how many rows 
    the 2D vector is going to have. */
    int row = 5;
    
    /* We define the number of values 
    each row is supposed to have. */
    int column[] = {5, 3, 4, 2, 1}; 
  
    /*
    We now create a vector of vector with size
    equal to row.
    */
      
    vector<vector<int>> vec(row);
    /*
    On line 21 we created a 2D vector and assigned
    it a capacity of "row"(in this case 5) units.
    */
      
    /*
    Now we will proceed to create the structure of
    our 2D vector by assigning the value of rows and
    columns through a nested for loop.
    */
  
    for(int i = 0; i < row; i++)
    {   
        /* Declaring the size of the column. */
        int col = column[i]; 
  
        /*
        On the 43rd line we declare the 
        i-th row to the size of the column.
        We create a normal vector of capacity "col" which
        in every iteration of the for loop will define the
        values inside of each row.
        */
        vec[i] = vector<int>(col);
        for(int j = 0; j < col; j++)
        {
            vec[i][j] = j + 1;
        }    
    }
      
    /*
    We now finally use a simple nested for loop
    to print the 2D vector that we just created above.
    */
  
    for(int i = 0; i < row; i++)
    {
        for (int j = 0; j < vec[i].size(); j++)
        {
            cout << vec[i][j] << " ";
        }    
        cout << endl;
    }
    return 0;
}

                    

Output
1 2 3 
4 5 6 
7 8 9 

Time Complexity: O(N*N)

Auxiliary Space: O(N*N) 

Another approach to access the vector elements:

C++

<div id="highlighter_223295" class="syntaxhighlighter nogutter  "><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="comments">/* C++ code to demonstrate a 2D vector  </code></div><div class="line number2 index1 alt1"><code class="undefined spaces">   </code><code class="comments">with elements(vectors) inside it. */</code></div><div class="line number3 index2 alt2"><code class="preprocessor">#include <iostream> </code></div><div class="line number4 index3 alt1"><code class="preprocessor">#include <vector> </code></div><div class="line number5 index4 alt2"><code class="keyword bold">using</code> <code class="keyword bold">namespace</code> <code class="plain">std; </code></div><div class="line number6 index5 alt1"><code class="undefined spaces"> </code> </div><div class="line number7 index6 alt2"><code class="color1 bold">int</code> <code class="plain">main() </code></div><div class="line number8 index7 alt1"><code class="plain">{ </code></div><div class="line number9 index8 alt2"><code class="undefined spaces">    </code><code class="comments">/* </code></div><div class="line number10 index9 alt1"><code class="undefined spaces">    </code><code class="comments">Below we initialize a 2D vector  </code></div><div class="line number11 index10 alt2"><code class="undefined spaces">    </code><code class="comments">named "vect" on line 12 and then </code></div><div class="line number12 index11 alt1"><code class="undefined spaces">    </code><code class="comments">we declare the values on  </code></div><div class="line number13 index12 alt2"><code class="undefined spaces">    </code><code class="comments">line 14, 15 and 16 respectively. </code></div><div class="line number14 index13 alt1"><code class="undefined spaces">    </code><code class="comments">*/</code></div><div class="line number15 index14 alt2"><code class="undefined spaces">     </code> </div><div class="line number16 index15 alt1"><code class="undefined spaces">    </code><code class="plain">vector<vector<</code><code class="color1 bold">int</code><code class="plain">>> vect </code></div><div class="line number17 index16 alt2"><code class="undefined spaces">    </code><code class="plain">{ </code></div><div class="line number18 index17 alt1"><code class="undefined spaces">        </code><code class="plain">{1, 2, 3}, </code></div><div class="line number19 index18 alt2"><code class="undefined spaces">        </code><code class="plain">{4, 5, 6}, </code></div><div class="line number20 index19 alt1"><code class="undefined spaces">        </code><code class="plain">{7, 8, 9} </code></div><div class="line number21 index20 alt2"><code class="undefined spaces">    </code><code class="plain">}; </code></div><div class="line number22 index21 alt1"><code class="undefined spaces">     </code> </div><div class="line number23 index22 alt2"><code class="undefined spaces">    </code><code class="comments">/* </code></div><div class="line number24 index23 alt1"><code class="undefined spaces">    </code><code class="comments">Now we print the values that  </code></div><div class="line number25 index24 alt2"><code class="undefined spaces">    </code><code class="comments">we just declared on lines </code></div><div class="line number26 index25 alt1"><code class="undefined spaces">    </code><code class="comments">14, 15 and 16 using a simple  </code></div><div class="line number27 index26 alt2"><code class="undefined spaces">    </code><code class="comments">nested for loop with the help of iterator. </code></div><div class="line number28 index27 alt1"><code class="undefined spaces">    </code><code class="comments">*/</code></div><div class="line number29 index28 alt2"><code class="undefined spaces">     </code> </div><div class="line number30 index29 alt1"><code class="undefined spaces">    </code><code class="comments">/* </code></div><div class="line number31 index30 alt2"><code class="undefined spaces">    </code><code class="comments">vector<vector<int>> vect </code></div><div class="line number32 index31 alt1"><code class="undefined spaces">    </code><code class="comments">We can divide this declaration to two parts, which will </code></div><div class="line number33 index32 alt2"><code class="undefined spaces">    </code><code class="comments">help us to understand the below concepts. </code></div><div class="line number34 index33 alt1"><code class="undefined spaces">     </code> </div><div class="line number35 index34 alt2"><code class="undefined spaces">    </code><code class="comments">1. vect is a 2D vector consisting multiple elements of type vector<int>.  </code></div><div class="line number36 index35 alt1"><code class="undefined spaces">    </code><code class="comments">2. vector<int> is a 1D vector consisting of multiple int data. </code></div><div class="line number37 index36 alt2"><code class="undefined spaces">     </code> </div><div class="line number38 index37 alt1"><code class="undefined spaces">    </code><code class="comments">So we can use iterator provided by STL instead of  </code></div><div class="line number39 index38 alt2"><code class="undefined spaces">    </code><code class="comments">i,j variable used in for loop. It can reduce the error which can  </code></div><div class="line number40 index39 alt1"><code class="undefined spaces">    </code><code class="comments">happen wrt to i, j operations(i++, j++)      </code></div><div class="line number41 index40 alt2"><code class="undefined spaces">     </code> </div><div class="line number42 index41 alt1"><code class="undefined spaces">    </code><code class="comments">In the below code we are using iterator to access the vector elements. </code></div><div class="line number43 index42 alt2"><code class="undefined spaces">    </code><code class="comments">1. We are getting vect1D vectors of type vector<int> from the 2D vector vect. </code></div><div class="line number44 index43 alt1"><code class="undefined spaces">    </code><code class="comments">2. We are getting int elements to x from the vector<int> vect 1D vector. </code></div><div class="line number45 index44 alt2"><code class="undefined spaces">     </code> </div><div class="line number46 index45 alt1"><code class="undefined spaces">    </code><code class="comments">*/</code></div><div class="line number47 index46 alt2"><code class="undefined spaces">     </code> </div><div class="line number48 index47 alt1"><code class="undefined spaces">    </code><code class="keyword bold">for</code> <code class="plain">(vector<</code><code class="color1 bold">int</code><code class="plain">> vect1D : vect) </code></div><div class="line number49 index48 alt2"><code class="undefined spaces">    </code><code class="plain">{ </code></div><div class="line number50 index49 alt1"><code class="undefined spaces">        </code><code class="keyword bold">for</code> <code class="plain">(</code><code class="color1 bold">int</code> <code class="plain">x : vect1D) </code></div><div class="line number51 index50 alt2"><code class="undefined spaces">        </code><code class="plain">{ </code></div><div class="line number52 index51 alt1"><code class="undefined spaces">            </code><code class="plain">cout << x << </code><code class="string">" "</code><code class="plain">; </code></div><div class="line number53 index52 alt2"><code class="undefined spaces">        </code><code class="plain">}     </code></div><div class="line number54 index53 alt1"><code class="undefined spaces">        </code><code class="plain">cout << endl; </code></div><div class="line number55 index54 alt2"><code class="undefined spaces">    </code><code class="plain">} </code></div><div class="line number56 index55 alt1"><code class="undefined spaces"> </code> </div><div class="line number57 index56 alt2"><code class="undefined spaces">    </code><code class="keyword bold">return</code> <code class="plain">0; </code></div><div class="line number58 index57 alt1"><code class="plain">} </code></div></div></td></tr></tbody></table></div>

                    

Output
1 2 3 
4 5 6 
7 8 9 

Time Complexity: O(N*N)

Auxiliary Space: O(N*N) 

Like Java’s jagged arrays, each element of a 2D vector can contain a different number of values.

C++

/*
C++ program to demonstrate a 2D vector where
each of its elements is of different size.
*/
#include <iostream>
#include <vector>
using namespace std;
int main()
{
    /*
    We initialize a 2D vector 
    named "vect" on line 16 with
    different number of values 
    in each element.
    */
      
    vector<vector<int>> vect
    {
        /* Element one with 2 values in it. */
        {1, 2}, 
        
        /* Element two with 3 values in it. */
        {4, 5, 6}, 
        
         /* Element three with 4 values in it. */
        {7, 8, 9, 10} 
    };
  
    /*
    Now we print the vector that we 
    just defined using a simple
    nested for loop.
    */
      
    for (int i = 0; i < vect.size(); i++) 
    {
        for (int j = 0; j < vect[i].size(); j++)
        {
            cout << vect[i][j] << " ";
        }    
        cout << endl;
    }
    return 0;
}    

                    

Output
1 2 
4 5 6 
7 8 9 10 

Exercise Problem: Define the 2D vector with different sizes of columns. 
Examples: 

Input : Number of rows : 5 
        Number of columns in rows : 
        2 3 4 5 1
Output : 1 2
         1 2 3
         1 2 3 4
         1 2 3 4 5 
         1

Input : Number of rows : 3
        Number of columns in rows : 
        3 2 1

Output : 1 2 3
         1 2
         1

2D vectors are often treated as a matrix with “rows” and “columns” inside it. Under the hood they are actually elements of the 2D vector. 
We first declare an integer variable named “row” and then an array named “column” which is going to hold the value of the size of each row. 

After that we proceed to initialize the memory of every row by the size of column.

C++

// CPP program
#include <iostream>
#include <vector> 
using namespace std;
int main()
{
    int n = 4;
    int m = 5;
  
    /*
    Create a vector containing "n"
    vectors each of size "m".
    */ 
    vector<vector<int>> vec( n , vector<int> (m)); 
  
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            vec[i][j] = j + i + 1;
        }
    }
  
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            cout << vec[i][j] << " ";
        }
        cout << endl;
    }
      
   return 0;
}

                    

Output
1 2 3 4 5 
1 2 3 
1 2 3 4 
1 2 
1 

Another Approach 
Suppose we want to initialize a 2D vector of “n” rows and “m” columns, with a value 0.

C++


                    

Output
0 0 0 0 
0 0 0 0 
0 0 0 0 

Time Complexity: O(N*M)

Auxiliary Space: O(N*M) 

Yet Another Approach: 
Suppose we want to create a 2D vector of “n” rows and “m” columns and input values. 

C++

// CPP program
#include <iostream>
#include <vector> 
using namespace std;
int main()
{
    int n = 4;
    int m = 5;
  
    /*
    Create a vector containing "n"
    vectors each of size "m".
    */ 
    vector<vector<int>> vec( n , vector<int> (m)); 
  
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            vec[i][j] = j + i + 1;
        }
    }
  
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            cout << vec[i][j] << " ";
        }
        cout << endl;
    }
      
   return 0;
}

                    

Output
1 2 3 4 5 
2 3 4 5 6 
3 4 5 6 7 
4 5 6 7 8 

Time Complexity: O(N*M)

Auxiliary Space: O(N*M)

We hope you that you leave this article with a better understanding of 2D vectors and are now confident enough to apply them on your own.




Last Updated : 10 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads