C++

Array of vectors C++

In C++, a programming language, a vector is a container that stores elements of similar data types, just like arrays. But now, the question arises, what is the difference between an array and a vector. It means at run time, the size of the vector can grow. But in the case of arrays, once the size is defined once, it is not easy to change the size later in the program. So in this article, we will explain the vectors in C++, also the arrays of vectors with examples.

Users must be aware of the basics of the C++ programming language. We have implemented this article in Linux operating system, so create a Linux environment through a virtual box. We have used a text editor for the codes, and to see the resultant value, the Linux terminal is used.

Declare C++ vector

Vectors are present in the C++ standard library. To use vectors, we have to introduce a vector header in the library.

#include <vector>

Once the header file is included, now we declare a vector in C++. The method of declaring is done by using std in the statement. So the syntax is written as:

Std::vector <T> vectorname;

<T> represents the type of the vector. It can be any data type like integer, character, and float, etc. Now, for instance, we use a data type of integer:

Vector<int> num;

You can see that we have not declared a size here because it is declared dynamically. Vector containers are not ordered in C++. The elements inside the vector are placed in the stores adjacent to each other so that each element can travel across using iterators. Inserting the data is time-consuming because, in some cases, we first need to extend the vector before entering the data. The class of vectors provides many methods to perform different operations on vectors. These functions include add elements, change elements, access and remove elements.

Now we will discuss some examples that will explain the phenomenon of the array of vectors.

Example 1
This example contains three major functions for insertion, displaying, and then illustrating the array of vectors. First, the library of the vector is used, as we have explained. First, declare the array of vectors with 5 vectors.

Vector <int > v[5];

Then we will insert elements in the array of vectors. This will be done in the function. Like a simple array, the values in the vector array are also added through the FOR loop. Here we have used a nested for loop to enter elements at every row using a vector built-in feature of the push_back() function. The index of the inner loop starts with the index incremented by 1 with the previous one.

V[i].push_back(j);

After inserting the values, now the main portion is displaying them with a special feature that in each row, one element is decreased from the starting value. So the printing procedure needs special logic. Let us have a look at the source code we have used in this program. Just like inserting elements, we also use a function to display the elements. First, we traverse the elements in an array of vectors inserted in the previous function. The First For loop will display the index number till 4, only starting from 0.

To display the elements of each column, we use a built-in feature begin() that starts the iterator to display the elements whereas end() is the ending iterator.

# V[i].begin();
# V[i].end();

Here * is used to get the value from the index at where the iterator is pointing at that time. And then, the value is taken one by one from each index, and then the control comes out from the inner loop, and then the outer loop displays each value. As we have displayed each value in a separate line, we have used ‘endl’. We have created another function here. For inserting function, we place a function call.

# insertionInArrayOfVectors();

And for the display function, we have used:

# printElements();

Whereas in the main program, only that function call is used in which both above function calls are made.

# arrayOfVectors();

Now save the code in the file with the extension of “.c”. To display the resultant value, go to the terminal and use a G++ compiler to execute the C++ code

$ g++ -o vector vector.c
$ ./vector

You can see that up to 5 times; the loop has been implemented. The values are decremented from the start value; this is done by the begin() function that starts from a specific index using an iterator.

Example 2
The second example uses a vector array, and the elements are directly assigned in the main program at the time of array declaration.  We have applied many features to them, like taking the sum of all the elements in the array, maximum number, and minimum number. For the sum of all the elements in a vector array, we use an accumulate function that takes a parameter. In the parameter, two built-in functions are used as the argument.

Accumulate(vec.begin(), vec.end() + 1, 0) ;

This will act as a loop to add all the elements. The second one is to get the maximum number for this function; the parameters will also be the same. These both begin and end functions take the values for the comparison purpose because when each value is compared, we will be able to get the maximum value.

*max_element(vec.begin(), vec.end());

Similar is the case with the minimum number.

When we execute the code, we can see the resultant value and use the compiler to see the execution. All the statements are displayed with the values, including the sum, maximum value, and minimum value.

Example 3
This example uses a vector class and std functions, including cout, setw, and array features. The vector array represents the 2-dimensional array having fixed rows of numbers and changing the number of columns. So the columns are added with the function push_back(). With the help of this function in the below code, we have entered 10 integers values randomly. This will give a 10*10 matrix. Let us see the source code.

A vector array is declared with the size; direct values are not assigned here because we need a random notation. A for loop is used here with a vector object that functions to create a matrix 2-dimensional array; a nested for loop is created that enters the value with a vec.push_back function. For displaying data, we again use a “For” loop, which displays the values in the form of the matrix. The inner for loop contains the item to be displayed with a distance, and this is done by setting a width from a function setw(3). This is a width of 3 points.

Now execute the code in the Ubuntu terminal.

Conclusion

This article ‘array of vectors C++’ contains the description of arrays and vectors together with the help of examples that are implemented on the Ubuntu operating system. Vectors array are dynamically created, and also their size can be defined initially by declaring them. Vector array contains some built-in features that we utilize in the examples in the current article.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.