How does C++ STL use vector’s vector? Code samples

Vectors are known as dynamic arrays, capable of automatically resizing themselves when elements are inserted or deleted, and containers automatically handle their storage.

A vector of a vector is a two-dimensional vector with a variable number of rows, where each row is a vector. Each index of a vector stores a vector, which can be traversed and accessed using the following methods. It is similar to a vector array, but with dynamic properties.

The syntax is as follows:

vector<vector<data_type>> vec;

Example:

vector<vector<int>> vec{ { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9, 4 } }; 
where vec is the vector of vectors with different
      number of elements in different rows

Inserts in a vector of vectors

Element can use the function of pushing back ()C++ STL.

The following example illustrates an insertion operation in a vector carrier. The code uses the push_back() function to create a 2D vector and then displays the matrix.

The syntax is as follows:

vector_name.push_back(value)

where value refers to the element
      to be added in the back of the vector

Example 1:

v2 = {1, 2, 3}
v1.push_back(v2);

This function pushes vector v2 into the vector of vector v1. So v1 becomes {{1, 2, 3}.

Example 2:

v2 = {4, 5, 6}
v1.push_back(v2);

This function pushes vector v2 into the existing vector of vector v1, and v1 becomes v1 = {{{1, 2, 3}, {4, 5, 6}}

The following is an example that demonstrates the insertion vector vector.

// C++ program to demonstrate insertion
// into a vector of vectors
  
#include <iostream>
#include <vector>
using namespace std;
  
// Defining the rows and columns of
// vector of vectors
#define ROW 4
#define COL 5
  
int main()
{
     // Initializing the vector of vectors
     vector<vector< int > > vec;
  
     // Elements to insert in column
     int num = 10;
  
     // Inserting elements into vector
     for ( int i = 0; i < ROW; i++) {
         // Vector to store column elements
         vector< int > v1;
  
         for ( int j = 0; j < COL; j++) {
             v1.push_back(num);
             num += 5;
         }
  
         // Pushing back above 1D vector
         // to create the 2D vector
         vec.push_back(v1);
     }
  
     // Displaying the 2D vector
     for ( int i = 0; i < vec.size(); i++) {
         for ( int j = 0; j < vec[i].size(); j++)
             cout << vec[i][j] << " " ;
         cout << endl;
     }
     return 0;
}

The output is as follows:

10 15 20 25 30 
35 40 45 50 55 
60 65 70 75 80 
85 90 95 100 105

Delete or delete in a vector vector

You can use vectors to remove elements from vectors pop_back the functionality of ()C++ STL.

The following example illustrates a delete operation in a vector vector. The code uses the pop_back() function to remove elements from the 2D vector and then display the matrix.

The syntax is as follows:

vector_name[row_position].pop_back()

Example 1: Let the vector of a vector be vector v = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}

v[2].pop_back()

This function removes element 9 from the last row of vectors. So v becomes {{1, 2, 3}, {4, 5, 6}, {7, 8}.

Example 2:

v[1].pop_back()

This function removes element 6 from the last second row vector. So v becomes {{1, 2, 3}, {4, 5}, {7, 8}}.

The following is an example that demonstrates the removal from a vector carrier.

// C++ program to demonstrate removal
// from a vector of vectors
  
#include <iostream>
#include <vector>
using namespace std;
  
// Driver Method
int main()
{
     // Initializing 2D vector "vect" with
     // sample values
     vector<vector< int > > vec{ { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
  
     // Removing elements from the
     // last row of the vector
     vec[2].pop_back();
     vec[1].pop_back();
  
     // Displaying the 2D vector
     for ( int i = 0; i < 3; i++) {
         for (
             auto it = vec[i].begin();
             it != vec[i].end(); it++)
             cout << *it << " " ;
         cout << endl;
     }
     return 0;
}

The output is as follows:

1 2 3 
4 5 
7 8

Traversal of vectors

Vectors of vectors can be used with iterators in C++. The following code demonstrates the traversal of a 2D vector.

The syntax is as follows:

for i in [0, n)
{
    for (iterator it = v[i].begin();
         it != v[i].end(); it++) 
   {
        // Operations to be done
        // For example to print
        print(*it)
    }
}

The following is an example that demonstrates traversal in a vector vector.

// C++ code to demonstrate traversal
// of a 2D vector
  
#include <iostream>
#include <vector>
using namespace std;
  
// Driver Method
int main()
{
     // Initializing 2D vector "vect" with
     // sample values
     vector<vector< int > > vec{ { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
  
     // Displaying the 2D vector
     for ( int i = 0; i < 3; i++) {
         for (
             auto it = vec[i].begin();
             it != vec[i].end(); it++)
             cout << *it << " " ;
         cout << endl;
     }
  
     return 0;
}

The output is as follows:

1 2 3 
4 5 6 
7 8 9

Considered one of the most sought-after skills in the industry, we have our own coding foundation, C++ STL, to train and master these concepts through an intense problem-solving process.