Array of Vectors in C++ STL
Last Updated :
12 Jul, 2025
Prerequisite: Arrays in C++,
Vector in C++ STL
An
array is a collection of items stored at contiguous memory locations. It is to store multiple items of the same type together. This makes it easier to get access to the elements stored in it by the position of each element.
Vectors are known as
dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container automatically.
Therefore,
array of vectors is two dimensional array with fixed number of rows where each row is vector of variable length. Each index of array stores a vector which can be traversed and accessed using
iterators.
Syntax:
vector <data_type> V[size];
Example:
vector <int> A[5];
where A is the array of vectors of int of size 5
Insertion: Insertion in array of vectors is done using
push_back() function.
For Example:
for i in [0, n) {
A[i].push_back(35)
}
Above pseudo-code inserts element 35 at every index of
vector <int> A[n].
Traversal: Traversal in an array of vectors is perform using
iterators.
For Example:
for i in [0, n) {
for(iterator it = A[i].begin();
it!=A[i].end(); it++) {
print(*it)
}
}
Above pseudo-code traverses
vector <int> A[n] at each index using starting iterators
A[i].begin() and ending iterator
A[i].end(). For accessing the element it uses
(*it) as iterators are pointers pointing to elements in
vector <int> A[n].
Below is the program to illustrate the insertion in the array of vectors.
CPP
// C++ program to illustrate
// array of vectors
#include <iostream>
#include <vector>
using namespace std;
// Declaring array of vectors
// globally
vector<int> v[5];
// Function for inserting elements
// in array of vectors
void insertionInArrayOfVectors()
{
for (int i = 0; i < 5; i++) {
// Inserting elements at every
// row i using push_back()
// function in vector
for (int j = i + 1; j < 5; j++) {
v[i].push_back(j);
}
}
}
// Function to print elements in array
// of vectors
void printElements()
{
// Traversing of vectors v to print
// elements stored in it
for (int i = 0; i < 5; i++) {
cout << "Elements at index "
<< i << ": ";
// Displaying element at each column,
// begin() is the starting iterator,
// end() is the ending iterator
for (auto it = v[i].begin();
it != v[i].end(); it++) {
// (*it) is used to get the
// value at iterator is
// pointing
cout << *it << ' ';
}
cout << endl;
}
}
// Function to illustrate array
// of vectors
void arrayOfVectors()
{
// Inserting elements in array
// of vectors
insertionInArrayOfVectors();
// Print elements stored in array
// of vectors
printElements();
}
// Driver code
int main()
{
arrayOfVectors();
return 0;
}
Output:
Elements at index 0: 1 2 3 4
Elements at index 1: 2 3 4
Elements at index 2: 3 4
Elements at index 3: 4
Elements at index 4:
Similar Reads
List of Vectors in C++ STL In C++, the list of vector refers to the list container in which each element is a vector. In this article, we will learn about the list of vectors in C++.Letâs take a look at an example:C++#include <bits/stdc++.h> using namespace std; int main() { list<vector<int>> l = {{1, 3}, {2
3 min read
Vector in C++ STL C++ vector is a dynamic array that stores collection of elements same type in contiguous memory. It has the ability to resize itself automatically when an element is inserted or deleted.Create a VectorBefore creating a vector, we must know that a vector is defined as the std::vector class template i
7 min read
Vector assign() in C++ STL In C++, the vector assign() is a built-in method used to assign the new values to the given vector by replacing old ones. It also modifies the size of the vector according to the given number of elements. Letâs take a look at an example that shows the how to use this function.C++#include <bits/st
4 min read
Vector of Strings in C++ In C++, a vector of strings is a std::vector container that stores multiple strings. It is useful when you need to store a collection of string data in a single container and refer to them quickly. In this article, we will learn about the vector of strings and how to create and use it in C++.Table o
3 min read
Vector size() in C++ STL In C++, the vector size() is a built-in method used to find the size of a vector. The size of a vector tells us the number of elements currently present in the vector. In this article, we will learn about the vector size() method.Let's take a look at the simple code example:C++#include <bits/stdc
3 min read
Vector data() in C++ STL In C++, the vector data() is a built-in function used to access the internal array used by the vector to store its elements. In this article, we will learn about vector data() in C++.Letâs take a look at an example that illustrates the vector data() method:C++#include <bits/stdc++.h> using nam
2 min read