Arrays Of Vectors In C++ STL - C++ Programming Tutorial
C++ Course / STL Vector / Arrays Of Vectors In C++ STL

Arrays Of Vectors In C++ STL

BLUF: Mastering Arrays Of Vectors In C++ STL is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Arrays Of Vectors In C++ STL

C++ is renowned for its efficiency. Learn how Arrays Of Vectors In C++ STL enables low-level control and high-performance computing in the tutorial below.

What are Arrays?

Arrays are sequential data structures that store elements of the same type in a linear arrangement. Elements within an array are allocated memory in a consecutive manner.

Arrays can come in different types depending on their dimensions such as a single-dimensional array, two-dimensional array, three-dimensional array, and so on.

Multidimensional arrays are also called arrays.

In C or C++, when declaring an array, it is necessary to specify the size by indicating the number of elements or the required memory space for those elements. Subsequently, the array can be populated with the desired values.

Syntax:

Example

data_type array_name[number_of_elements];

What are Vectors?

Vectors are similar to arrays in that they store data values, but they possess the ability to adjust their size. Because they can expand automatically, vectors are often referred to as dynamic arrays.

In arrays, we specify the number of elements during declaration, whereas vectors do not require a predefined size as they can dynamically resize as needed.

Vectors are specified within the C++ Standard Template Library (STL), therefore to utilize vectors, it is necessary to include the STL library in the program file.

Since vectors are specified in the Standard Template Library (STL), it offers numerous pre-existing functions for inserting, removing, or altering elements at any given index, be it at the beginning, end, or any other position within the vector.

For example: size,capacity,push_back etc.

There are the following inbuilt functions used in vectors:

  • size : This function returns the number of elements present in the vector.
  • capacity : This function returns the number of elements or the capacity of the vector.
  • empty : It returns whether the current vector is empty or not empty.
  • push_back : With the help of this function, we can insert an element from the backside of the vector.
  • pop_back : With the help of this function, we can remove or delete the element from the backside of the vector.
  • insert : With this function, we can insert the element in the vector at any specified position.
  • erase : With the help of this function, we can remove the elements in the vector from the specific position.

Syntax:

Example

vector<data_type> vector_name;

Arrays of Vectors

Vectors arrays are essentially 2D matrices or arrays with a variable number of columns, while maintaining a constant number of rows.

Since each row corresponds to a single vector and a vector can contain a variable number of elements, each row is allowed to contain a flexible number of columns.

It can look like this:

Syntax:

Example

vector<data_type> array_name[array_size];

The syntax closely resembles declaring an array, however, the array's data type is a vector.

C++ Example:

Example

#include <iostream>
#include<vector>

using namespace std;
int main() {
    int n =5;// number of rows is 5
    vector<int> vc[n];
    //inserting in the first row
    vc[0].push_back(1);
    vc[0].push_back(2);
    vc[0].push_back(3);
    vc[0].push_back(4);
    vc[0].push_back(5);

    //inserting in the second row
    vc[1].push_back(6);
    vc[1].push_back(7);
    vc[1].push_back(8);
    
    //inserting in the third row
    vc[2].push_back(9);
    vc[2].push_back(10);
    vc[2].push_back(11);
    vc[2].push_back(12);
    
    //inserting in the fourth row
    vc[3].push_back(13);
    vc[3].push_back(14);
    
    //inserting in the fifth row
    vc[4].push_back(15);
    
    for(int i=0;i<n;i++){
        cout<<"number of column in row number "<<i+1<<" is "<<vc[i].size()<<endl;
        for(int j=0;j<vc[i].size();j++){
            cout<<vc[i][j]<<" ";
        }
        cout<<endl;
    }

    return 0;
}

Output:

Explanation:

In the preceding code snippet, we initialized an array of vectors with a length of 5, signifying a fixed number of rows indicated by the array size of 5. This implies that we will have five vectors, each capable of varying in size.

In the initial vector, we include five elements: 1, 2, 3, 4, and 5.

In the subsequent vector, we include three elements namely: 6, 7, 8.

In the third vector, we insert four elements specifically: 9, 10, 11, and 12.

In the fourth vector, we insert two elements, specifically: 13 and 14.

In the fifth vector, we inserted a single element, specifically the number 15.

Now, we employ a pair of loops to display each element within our vector array.

The outer iteration will iterate to determine the total row count, executing ```

datatype arrayname[numberofelements];

Example

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience