What is a vector <vector<int>>

closed account (EwCjE3v7)
What is a
 
  vector <vector<int>> ivv;

I know what a vector <int> iv; but what is vector <vector<int>>

Thanks
closed account (N36fSL3A)
It's a 2D vector. Just like a 2D array.
It's a 2D vector. Just like a 2D array.

Just a small observation: a plain 2D array looks like a matrix, because all rows have equal lengths.

However with a vector of vectors, you need to make sure personally that this is so:

int a[3][4];

[[xxxx]
 [xxxx]
 [xxxx]]

vector<vector<int> > vvi;

[
]

vector<vector<int> > vvi(3);

[[]
 []
 []]

vector<vector<int> > vvi(3, vector<int>(4));

[[xxxx]
 [xxxx]
 [xxxx]]
closed account (EwCjE3v7)
Sorry for my late reply. I still don`t understand as i donno 2d arrays or even arrays :-/
closed account (N36fSL3A)
They're not really required to be learned. They are just arrays of arrays.

http://www.cplusplus.com/doc/tutorial/arrays/
A vector of vectors also can have rows of unequal sizes.
You need to learn arrays first and then multi-dimensional arrays. An array is just like a collection of stuff of the same data type.

Here is a crash-course for you:

You declare an array like this:
data_type arrayname[ARRAY_SIZE];

An example:
int numbers[5]; //This creates an array of type int to store 5 integers

Indexing of the array starts at 0 and ends at ARRAY_SIZE-1.

If I have an array of size 5, the first element would be at index 0, the second would be at index 1 and the last element at index 4 (5-1).

You access an element in the array with array_name[index];.

An array's size cannot be changed once declared.

A 2D array is basically an array of arrays. You declare it like this type array_name[siz_x][siz_y];;

It looks like this:
1
2
3
4
5
6
7
8
int myarray[3][3];

/*
If you fill the above with zeroes it'd look like:
[[0,0,0],
[0,0,0],
[0,0,0]]
*/


An example program using array:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>

int main()
{
    int userInputs[5];
    
    int sum = 0;
    
    std::cout << "Enter a number: ";
    
    std::cin >>  userInputs[0];
    
    for(int i = 1; i<5; i++)
    {
        std::cout << "Enter another number: ";
        std::cin >> userInputs[i];
    }
    
    for(int j = 0; j<5; j++)
    {
        sum += userInputs[j];
    }
    
    std::cout << "The sum of those numbers are: " << sum << std::endl;
    
    return 0;
}

Last edited on
closed account (EwCjE3v7)
Thanks guys, I'll learn arrays once they come in my book even tho I think I know most because of Stormboy. Thank you all great help
Topic archived. No new replies allowed.