Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multidimensional Vectors in C++

I just started learning C++. I was trying to grasp the syntax for multidimensional arrays and vectors when I started to get fairly confused. I get how to initialize multidimensional arrays. It seems straightforward: Rows followed by columns. However, vectors are a little more challenging. Do I have to initialize them in the same way or do I create a vector of vectors?

like image 801
Nikhil Sridhar Avatar asked Dec 26 '16 00:12

Nikhil Sridhar


People also ask

What are multidimensional vectors?

For a multidimensional array, memory for the elements of the array is required to be allocated contiguously. For a vector of vector, the memory for the elements is most likely going to be disjoint. Also, it is possible to defined a vector of vectors in which the number of columns is not same for each row.

What is multidimensional C?

A multi-dimensional array can be termed as an array of arrays that stores homogeneous data in tabular form. Data in multidimensional arrays are stored in row-major order. The general form of declaring N-dimensional arrays is: data_type array_name[size1][size2]....[sizeN];

Does C allow multidimensional arrays?

In C programming, you can create an array of arrays. These arrays are known as multidimensional arrays. For example, float x[3][4];

What is multi-dimensional array in C with example?

A multi-dimensional array is an array with more than one level or dimension. For example, a 2D array, or two-dimensional array, is an array of arrays, meaning it is a matrix of rows and columns (think of a table). A 3D array adds another dimension, turning it into an array of arrays of arrays.


2 Answers

declare a multidimensional vector:

vector<vector<int>> test(4,vector<int>(20));

This creates a 2D vector 4 X 20. Of course since they're vectors that can be changed as needed. The indexing is the same as an array test[3][19].

like image 136
tinstaafl Avatar answered Sep 22 '22 19:09

tinstaafl


If you are able to use C++11, multidimensional arrays and vectors of vectors can be initialized in a similar manner.

int a1[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
std::vector<std::vector<int>> a2 = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };

However, there are differences that must be understood to access the elements without running into undefined behavior.

For a multidimensional array, memory for the elements of the array is required to be allocated contiguously. For a vector of vector, the memory for the elements is most likely going to be disjoint.

Memory for a1:

a1[0][0]    a1[1][0]    a1[2][0]
|           |           |
v           v           v
+---+---+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+---+---+

Memory for a2 (most likely):

a2[0][0]
|
v
+---+---+---+
|   |   |   |
+---+---+---+

a2[1][0]
|
v
+---+---+---+
|   |   |   |
+---+---+---+

a2[2][0]
|
v
+---+---+---+
|   |   |   |
+---+---+---+

Also, it is possible to defined a vector of vectors in which the number of columns is not same for each row.

std::vector<std::vector<int>> a2 = { {1, 2, 3}, {4, 5}, {6, 7, 8, 9} };

In a multidimensional array, the number of columns is guaranteed to be same for each row.

Given the above multidimensional array a1, a1[1][2] will be a valid element and a1[2][3] will be an invalid element. In the case of a vector of vectors, using the above line, a2[1][2] is not a valid element and a2[2][3] is a valid element.

like image 36
R Sahu Avatar answered Sep 19 '22 19:09

R Sahu