## Arrays of Vectors in C++ STL## What are Arrays?Arrays are linear data structures which store the data or values of the same data type in a linear fashion. Values or data stored in the array are assigned memory in contiguous order. Arrays can have various types based on their dimensions like a one-dimensional array, two-dimensional array, three-dimensional array etc. Multidimensional arrays are also called arrays. In C or C++, to declare the array firstly, we have to tell the number of elements or the memory we need for the elements. Then we can fill the values in the declared array.
## What are Vectors?Vectors are also the data structure which stores the value in the same way as an array does, but they have the capability to resize themselves. Due to increasing the size of itself automatically, the vectors are also known as dynamic arrays. In the arrays, at the declaration time, we have to tell the number of elements we want to put into the array, but in the case of vectors, we do not need to declare the number of elements because it can increase its size when it is filled completely. Vectors are defined in the STL (Standard Template Library) of C++, so to use the vectors, we have to import the STL library into the program file. Since vectors are defined in the STL (Standard Template Library), it has a lot of built-in functions for insertion, deletion or modification at any index, whether it is at starting, ending or at any position. For example: size(),capacity(),push_back() etc.
**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.
## Arrays of VectorsArrays of vectors are basically two-dimensional matrices or arrays where the number of columns can be anything, but the number of rows is fixed. Since each row represents one vector and one vector can have any number of elements, each row can have any number of columns. It can look like this:
The syntax is similar to array declaration, but the data type of the array is a vector.
In the above code, we created the array of vectors of size 5 which means the number of rows which are fixed and they are represented by array size is 5. Since we have an array size of five, it also means we will be having the five vectors, and they can have different sizes. In the first vector, we put five elements which are: 1, 2, 3, 4, 5. In the second vector, we put three elements which are: 6, 7, 8. In the third vector, we put four elements which are: 9,10,11,12. In the fourth vector, we put two elements which are: 13 and 14. In the fifth vector, we put one element, which is 15. Now, we use two loops to print all the elements of our array of vectors. The outer loop will count the number of rows so that it will run for n times, and the inner loop will run for the number of elements in each vector. |

For Videos Join Our Youtube Channel: Join Now

- Send your Feedback to [email protected]