# Array Of Vectors In C++ : STL

Hey folks! Vectors are used to store similar data types of elements dynamically, thereby known as dynamic arrays. These vectors are used in the C++ programs by including vector header file as #include<vector> at the start of the program. Elements can be inserted/deleted/modified at the start/end/middle of a vector using built-in methods of vectors. Now, in this article, we are going to explore on “array of vectors in C++”.

## Syntax: Array of Vectors in C++

• Vector : `vector <dataType> identifier;`
• Array  :  `dataType identifier [ size ];`
• Array of Vectors : `vector <dataType> identifier [ size ];`

## Explanation:

As you have seen the syntax for an array of vectors as vector<dataType>identifier[size], we can divide it into subparts.

• vector      : definition for vector

• dataType : data types like int, char, float, double, etc.

• identifier  : this is the user-defined name for the vector

• size         : number of vectors in the array

## Operations with C++ code:

```#include <iostream>
#include <vector>
using namespace std;

int main()
{
// number of vectors
int n;
court<<"Enter number of vectors in array : ";
cin>>n;

// creating array of vectors "vec" with size n(vectors)
vector<int> vec [n];

// Inserting input elements into each vector
for (int i=0; i<n; i++){
cout<<"Enter number of elements in vector "<<i+1<<" : ";
int num;
cin>>num;
cout<<"Enter elements into vector "<<i+1<<" : ";
for (int j=0; j<num; j++){
int temp;      // to take input element & push back into vector
cin>>temp;
vec[i].push_back(temp);
}
}

// Printing elements vector-wise
cout<<"Method 1 : Using size() method"<<endl;
cout<<"Elements are : "<<endl;
for(int i=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(int i=0; i<n; i++){
cout<<"Vector "<<i+1<<" : ";
for(auto it = vec[i].begin();it != vec[i].end(); it++)
cout << *it << ' ';
cout<<endl;
}
return 0;
}
```

#### Output:

```Enter number of vectors in array : 3
Enter number of elements in vector 1 : 3
Enter elements into vector 1 : 1 2 3
Enter number of elements in vector 2 : 4
Enter elements into vector 2 : 4 5 6 7
Enter number of elements in vector 3 : 5
Enter elements into vector 3 : 8 9 10 11 12
Method 1 : Using size() method
Elements are :
Vector 1 : 1 2 3
Vector 2 : 4 5 6 7
Vector 3 : 8 9 10 11 12
Method 2 : Using iterators method
Vector 1 : 1 2 3
Vector 2 : 4 5 6 7
Vector 3 : 8 9 10 11 12```

### Conclusion:

So, as you have seen both methods to print the elements using the size() method and iterators, most of the programmers prefer iterators for accessing or printing. Hope you have liked the article, and you can also explore more on vector & array operations.

Happy coding!!! 😀