×

Array of Vectors in C++ STL

Prerequisites: C++ STL Arrays and C++ STL Vector.

A group of items kept in consecutive memory region is known as an array. It is to group similar objects of the same type in storage. As a result, it is simpler to access the pieces contained there thanks to their positions.

Vectors are referred to as dynamic arrays because they may dynamically resize themselves when an element is added or removed, with the container taking care of its storage automatically.By including the vector header file as #includevector> at the beginning of the program , these vectors are used in C++ program s.

As a result, an array of vectors is a two-dimensional array with a set number of rows and a variable number of rows per vector. Iterators can be used to traverse and access the vectors that are stored in each index of an array.

Syntax:

vector <data_type>identifier[size];

We can break down the syntax for a vector array into its component pieces because we have already seen that it is vector <data_type>identifier[size].

Example:

Vector <int>A [5];
Where A is the vectors array of int of size 5

Insertion:

The push back() function is used for vector array insertion.

Example:

for I n [0,n) {
          A[i]. push_back (35)
      }

The above pseudo-code inserts element 35 into the vector int> A[n] at every index.

Example:

#include <iostream>
#include <vector>
usingnamespacestd;
int main()
{
    // quantity of vectors
intn;
    court<<"Enter number of vectors in array : ";
cin>>n;
    // creating array of vectors "vec" with size n(vectors)
    vector<int>vec [n];
    // input components are added to each vector
for (inti=0; i<n; i++){
cout<<"Enter number of elements in vector "<<i+1<<" : ";
intnum;
cin>>num;
cout<<"Enter elements into vector "<<i+1<<" : ";
for (int j=0; j<num; j++){
inttemp;      // to pull a component from the input back into a vector
cin>>temp;
vec[i].push_back(temp);
        }
    }
    // printing vector-based elements
cout<<"Method 1 : Using size() method"<<endl;
cout<<"Elements are : "<<endl;
for(inti=0; i<n; i++){
cout<<"Vector "<<i+1<<" : ";
int size = vec[i].size();
for(int j=0; j<size; j++)
cout<<vec[i][j]<<" ";
cout<<endl;
    }
cout<<"Method 2 : Using iterators method"<<endl;
for(inti=0; i<n; i++){
cout<<"Vector "<<i+1<<" : ";
for(auto it = vec[i].begin();it != vec[i].end(); it++)
cout<< *it << ' ';
cout<<endl;
    }
return0;
}

Output:

Array of Vectors in C++ STL

Working

It is quite simple to carry out certain fundamental operations on an array using the STL library, including as sorting, searching, adding up the elements, and determining the array's minimum and maximum elements.

Traversal

Iterators are used to execute vector array traversal.

Example:

For I in [0,n) {
         For (iterator it = A [i]. begin ();
            It! =A[i].end (); it++) {
          Print (*it)
        }
     }

By using the beginning iterator, A[i]. begin() and the ending iterator A[i]. end, the pseudo-code explores the vector int> A[n] at each index (). Since iterators are pointers pointing to elements in vector int> A[n], it uses (*it) to access the element.

The program used to demonstrate the insertion into the vector array is shown below.

Example:

// a C++ application to display
// a vector array
  
#include <iostream>
#include <vector>
usingnamespacestd;
  
// declaring a vector array
// globally
vector<int>v[5];
  
// The ability to introduce elements
// in a vector array
voidinsertionInArrayOfVectors()
{
  
    for (inti = 0; i< 5; i++) {
  
        // Adding components at each
        // using push back in row I ()
        // a vectorized function
        for (int j = i + 1; j < 5; j++) {
            v[i].push_back(j);
        }
    }
}
  
// Function to print elements from a vector array
voidprintElements()
{
  
    // Vectors are traversed to print
    // elements kept there
    for (inti = 0; i< 5; i++) {
  
        cout<< "Elements at index "
             <<i<< ": ";
  
        // each column displaying a different element
        // The initial iterator is begin(),
        // The terminating iterator is end().
        for (auto it = v[i].begin();
             it != v[i].end(); it++) {
  
            // (*it) is used to get the
            // iterator value is
           //indicating
            cout<< *it << ' ';
        }
        cout<<endl;
    }
}
  
// Function that displays an array
// Using vectors
voidarrayOfVectors()
{
    // adding elements to an array
    // using vectors
    insertionInArrayOfVectors();
  
    // Print the array's stored elements.
    // using vectors
    printElements();
}
  
// driver's code
int main()
{
    arrayOfVectors();
    return0;
}
    return0;
}

Output:

Array of Vectors in C++ STL

Related Topics

C++ Program to move all zeros to the end of the array

Write a program to move all the zeros in the arr[] to the end. The order of the non-zero elements should not be altered and all the zeros should be...

3 minutes read.

C++ Bidirectional Iterators

Iterators : Iterators serve as a link between algorithms and STL containers, allowing the data inside the container to be modified. They let you to iterate through the container, access and...

3 minutes read.

How is multiset implemented in C++

Similar to sets, multisets are an associative container type where several items may share the same values. Associative containers implement instantly searchable sorted data structures with O(log n) complexity. In a multiset,...

5 minutes read.

Advanced C++ with Boost Library

The goal of the Boost Libraries is to be widely applicable and used in a variety of applications. For instance, they can in handy when working with huge numbers whose...

4 minutes read.

C++ Bitset

Overview In C++, bitset represents a fixed-sequence of some bits values by either 0 and 1. Zero represents the value as false or unset, while 1 represents the value as true...

4 minutes read.

Division in C++

C++ Division Arithmetic Operation In C++ the arithmetic operator / is used for division. This operator takes two operands and returns the result of dividing the left operand by the right...

3 minutes read.

Scope Resolution Operator vs this Pointer in C++

In this tutorial, we will compare the Scope Resolution operation to this Pointer in C++ language. Scope Resolution Operator The Scope Resolution Operator in C++ programming language is usually denoted by (::)....

3 minutes read.

How to make a password program in C++

Before understanding the password program, one must know about a password and why it is required. Password: A password is a word that permits access to somewhere or something. A password...

4 minutes read.

Ascending order in C++

In C++, the term "ascending order" refers to a specific order in which a list of elements is arranged. When a list of elements is arranged in ascending order, the...

3 minutes read.

C++ Break

In this article, we will discuss the C++ Break statement with its syntax, algorithm, pseudocode, and examples. The C++ break statement also terminates the currently active loop or switch statement immediately....

4 minutes read.

Armstrong number using for loop in C++

What is For Loop? A for loop is a repetitive control structure that allows you to create a loop to execute a specific number of times efficiently. The syntax that can be...

4 minutes read.

C++ STL Components

C++ STL Components In today’s article, we are going to learn about all the points things that is related to STL in C++ so stay connected because you are going to...

6 minutes read.

Copy elision in C++

The Copy Omission is another name for the Copy Elision. One of the several compiler optimization techniques is copy elision. It prevents items from being copied inadvertently. This Copy Elision approach...

3 minutes read.

Bitwise Operator vs Logical Operator

Bitwise Operator  Bitwise operators perform operations bit by bit on bits.The value is converted to abinary during operations like addition, subtraction, division, and so on. These operations are carried out at the...

3 minutes read.

Web Development in C++

Before learning above C++ web development, we need to learn about CGI What is CGI? CGI stands for common gateway interface. CGI is a standard that tells us how the exchange of...

4 minutes read.

Array of sets in C++

Instead of defining distinct variables for each item, arrays are used to hold numerous values in a single variable. A collection of data objects stored in a continuous way is...

6 minutes read.

C++ add two numbers using the function

Here we will learn how to add two numbers by creating function in C++. Let’s learn this with help of example. Code: - #include <iostream> using namespace std; int add_two_no(int a, int b); int main(){   int...

1 minute read.

Armstrong Number Program in C++

Let's first define Armstrong number before writing the C++ program to check whether the number is Armstrong or not. The sum of the cubes of its digits is equal to the...

2 minutes read.

Lambda Expression in C++

The lambda expression was introduced in C++ 11. It is used to write the inline function in C++. The code written in lambda expression cannot be reused further. The syntax...

3 minutes read.

C++ cin and cout

In this article, we will discuss the C++ cin and cout with their library and examples. C++ Standard Input/Output: User-program communication is made possible by C++’s usage of input and output (I/O)...

5 minutes read.