| by Arround The Web | No comments

Array of Vectors in C++

C++ is a programming language that has strict syntax to be followed; we cannot skip even a semicolon while coding. Arrays are an important part of coding. Without arrays, programming would be very difficult. Arrays work as a container that contains the data of the same type in one place. Arrays have a fixed size; the size of an array cannot be changed automatically. Manually updating the size of an array is difficult, and this is now a problem. The solution to this problem is vectors which are known as dynamic arrays. That means that the size of arrays is flexible and we can insert more elements in an array after the initialization without worrying about the size. The arrays of vectors are like two-dimensional arrays that contain different columns and rows. We cannot change the number of rows but each row is a vector whose length can be changed. Each vector is a dynamic array of one dimension. Every index of the array is a vector whose elements are accessed using the iterators like for, foreach, etc.

The arrays of vectors make the data storage of similar types easy and flexible. These are mostly used when we don’t know the size of the array and when we are dealing with a two-dimensional array. Vectors are defined in STL which is the standard template library in the C++ programming language. Before using vectors, we have to import this library by including this piece of code in our program before the “namespace std”.

#include<vectors>

Syntax:

vector <Data_Type_Name> Variable_Name [Size_No_of_rows];

Here, it is:

vector <double> Length_0[2];

The syntax of the vector array is, as shown previously, the “vector” keyword is placed. After that, we define the type of the vector in angle brackets. Here, the type of vector is “double”. We declare the variable name with the size outside the angle brackets. The size is the number of rows that we want in our array. The size of rows cannot be changed but the columns are flexible. The array of the vector is defined after the “namespace std” in the code.

Array of Vectors Initialization

There are numerous techniques that are used to initialize the vector array. A few of them are explained in the following:

  • Using Push_Back
Vector_name.push_back(element);

The “Vector_name” attribute is the name of the vector that we declare while creating the vector. And “push_back” adds the element in the parenthesis from the back of the array.

  • Using Constructor
vector <double> Length_0(repetition, element);

It is defined at the time of vector declaration. Within the parenthesis, place the number of repetitions that we want and then mention the element that we want in the vector array.

  • Using an Array Method
vector <double> Length_0{val_1, val_2,..};

This method is also done at the time of vector declaration. After the “variable_name” inside the curly braces, initialize the vector array as we do in any normal array.

Example 1:

Use the push_back() function to insert the values in the vector array and get the outcome using an iterator.

#include <iostream>

#include <vector>

using namespace std;
vector Numbers[3];

int main()
{
    for (int i = 0; i < 3; i++) {
        for (int j = 0 ; j < 3; j++) {
             Numbers[i].push_back(j);
        }
    }
    cout<<"The stored vector array is \n";
        for (int i = 0; i < 3; i++) {
        for (auto iter =  Numbers[i].begin();
            iter !=  Numbers[i].end(); iter++) {
            cout << *iter << ' ';
        }
        cout << endl;
    }
    return 0;
}

Integrate the <iostream> and <vector> libraries in the first statement. Then, define the vector array of size 3 having integer type after the “namespace”. Inside the main() method, use a nested “for” loop to initialize the array that has three rows. The outer “for” loop starts from zero and iterates 3 times. The inner “for” loop iterates three times. Then, at index “i”, it pushes the value of “j” in it. This process repeats itself three times and saves the value in an array of vectors. Then, represent a “The stored vector array is” message on the console before printing the stored values.

Now, to print the output, we employ a nested “for” loop. Here, the outer “for” loops three times and the inner loop sets the automatic iterator that starts the iteration from the index of “i”. The begin() method starts the iteration and ends until the condition turns false. The condition says that the loop goes on until the iterator is not equal to the index “i” of the vector. The end() method loops until it reaches the end of the array. Then, the *iter prints the value that it contains after completing the iteration and the cursor moves to the next line because of “cout<<endl”. The process is repeated three times because the condition is set to loop until the vector array ends and the array is of size 3. So, it loops three times and then the “for” loop terminates.

Example 2:

Let’s see how we can initialize an array of vectors by utilizing the array method.

#include <iostream>

#include <vector>

using namespace std;  
vector Numbers_1{19.3, 5.7, 3.2, 40.0};
int main()  
 {  
  cout << "The vector elements are: \n";
  for (int i = 0; i < Numbers_1.size(); i++)  
  {  cout << Numbers_1[i] << "    " ;   }
  return 0;  
}

We first import the header files to use the STL where the vectors are defined. Next, declare the array of a vector of float type and store 4 elements in it using the array syntax for initialization. After employing the main() function, print the “The vector elements are” text on the output screen by calling the “cout<<” statement. Now, apply the “for” loop to show the values that are stored in the array of a vector. Define and initialize the “i” iterator with the zero value. Specify the condition to the size of the array and then increment the iterator. Inside the “for” loop, display the values of the vector array with the help of the “cout<<” statement like Numbers_1[i] at index 0. The value which is saved in “Numbers_1” is displayed on the terminal. After printing the value, 2 spaces are passed and then the loop iterates until the required condition turns false. The elements that are saved in the array are shown on the terminal.

Conclusion

We explored the topic about “arrays of vectors in C++” in detail. Before having the background information on any topic, we cannot proceed forward especially when the topic has a link with the previous topic. So, we first described the arrays, what they are, and why we use them. After that, we elaborated briefly on the vectors and arrays of vectors. The arrays of vectors are discussed with the syntax, initialization methods, and coding examples to make it easier and more understandable to you.

Share Button

Source: linuxhint.com

Leave a Reply