## Vector of vectors (C++ programming tutorial)

#### Sharing buttons:

hey welcome back in this video we are

learning how to make vectors of vectors

and C++ so it's a little bit similar to

a 2d array but instead of using arrays

we're using vectors now I think by the

end of this what we want to do is we

just want to make a little table of

numbers sort of like a matrix of numbers

and I'm gonna say that's going to be a

three by three matrix of numbers that

will just print to the screen so you'll

be able to see how to populate the

vector of vectors and then also how to

print all of that stuff to the screen

all right so it's going to be a lot of

information to take in but I think we

can get it done in ten minutes

so first of all we need to define our

vector of vectors and what we would do

for example if it was a vector of

integers we would go like this and we

would give it a name you know pick up

the value pick its element type here and

name it now we don't want to make a

vector of integers we want to make a

vector of vectors so instead of writing

int here we're going to write vector I

did mention that eventually we'll make a

little table of numbers so I guess those

numbers can be inserted eventually as

integers so let's clean up these spaces

here I just separated it out so you can

see now if I have an older compiler

actually so if I remove this final space

it's going to see these guys and think

it's the insertion operator and that's

not the case so I have to leave a little

space here but if you have a newer

compiler I actually I don't think this

is a problem anymore for newer compilers

but anyways watch out if you're an older

compiler and leave a space there so

I just defined a vector of vectors

called stuff so this is my vector

it's called stuff and it's elements are

the tar a vector of integers notice I

didn't put it I didn't name this vector

of integers here that's cuz I'm not

actually defining it I'm just saying

that for this vector called stuff

instead of its element type being

integer or double or something or string

its integer type is vector it's

basically just going to accept vectors

filled with integers sort of as its

argument so there we go now I say here

we're gonna fill the inner vector and

then insert it into the outer vector so

basically we need to create a vector

here we do need to define a vector

eventually the a vector of integers and

we need to fill that we need to populate

it with elements and then once we have

some elements in there then we're going

to

would use push back to do that then

we'll use a separate push back function

to actually take that vector that we've

created and drop it in as the first

element stuff and it's okay that that

element is a vector because that's what

it has to be okay so first of all we're

going to use nested for-loops to do this

so I'll just set this up first of all we

can talk about it okay so for int I

equals 0 and then I less than 3 I

mentioned we're going to sort of you

know make a three by three little matrix

that'll work for us and we'll go I plus

plus okay so inside this vector it's

right inside this for loop what we

should do is we should define our

temporary vector this inner vector that

we're going to be sort of passing in

this argument sort of but we're passing

in two stuff so we'll say a vector and

and it's a temporary vector let's just

call it temp all right so what we're

actually doing here this for loop that

runs three times eventually we'll use

this for loop to pass in this vector

three times and you'll see that in one

second I guess eventually what here I

can just write it down here will have

stuff dot push push back no stuff dot

push back push back so eventually we're

going to just pass attempt into stuff as

we're passing an entire vector in but we

need to fill out that vector first of

all before this makes any sense so we

use another for loop here so for int J

equals zero

J at less than three again this is just

to get us a four a three by three little

matrix guy and then J plus plus so

inside this for loop what we're gonna do

is we want to use push back to fill out

started to create elements and tamp and

put values there so tamp dot push the

bush temp top push back and for now

let's just drop in the original index

value here I so it'll actually just fill

so it'll so when we create our first

element in temp it will be a little

it'll actually the first element will be

a zero and then when we run this loop

again the first

still be 0 because I hasn't changed and

the third element will still be zero

because I still hasn't changed J is

changing so this will actually here or

the first iteration of the program will

actually create integer a vector of

integers called temp and it'll have

three zeros for you know each value will

be a zero and then what we do is that

once it kicks us out of that loop after

J after this isn't satisfied anymore

then we're going to use push back but

we're gonna use it on stuff and stuff

here is our sort of parent vector this

outside vector of vectors and right now

where we did where we defined it it

would have no size but here when we use

push back we're actually creating the

first element and stuff and then we're

passing in we're passing in tamp here

and temp is a vector of integers and it

already has three values in it so there

we go the second time so that takes us

here to the end of our leap now when we

run this loop again and I will increment

to 1 we'll do everything again we'll

create will sort of recreate temp here

and then I will be 1 so we'll actually

we'll fill 10 with 3 ones and then once

that kicks us out of the loop then we'll

always push back on stuff again stuff is

our sort of parent vector and it'll

actually create the second element of

stuff and the second element will get

passed in the value there or pass in

like a vector because it's passing in

temp and this time it's passing in a

vector of integers and each of those

integers has the value of 1 the third

time you run this it'll pass in

basically at the end of the day a vector

with three twos in it so there you go

now if we actually just ran this program

right now it would work properly but we

wouldn't see anything so we do need to

see out some stuff to us to know that

we've done this correctly so we're going

to use again to sort of nested for loop

so we'll go for int I equals 0

I so I could say here at less than 3 but

something that's actually gonna be way

more weight better for us to use is

we'll go stuff size because if I afraid

runs some you know if I if I later on if

I change this to a 5 or something I

would have to remember to change this to

the 5 but this way it'll already know

so this is kind of protects us against

that all right so I plus plus now inside

here I mentioned it's going to be a

nested for loop so we'll just drop this

in right away

so for and J equals zero and now this

one's a little bit more interesting so

again we could say J less than three but

that doesn't protect us against much so

well actually go one step further will

you use will find this index sort of

value here and dot size and like that so

basically what this is doing this is

checking this is checking the the length

of the row that we've put in here so

you'll see that all changes for more on

the program make it three by three and

we'll run this afterwards and you'll see

how these update but this again this

protects us against changing the values

in here that were actually inputting

okay then we gotta go with J plus plus

and we're almost there so see out we

want to print this out of the screen so

we're gonna say C out stuff and similar

to the 2d array we're gonna call two

index values here just like that

IJ and then maybe to make this look nice

outside of this sleep but inside of this

loop we're gonna put here so you know

just an end line and that way we're

gonna print just you know three times

there are three values at a time

basically okay so I think we got two

here that's about eight minutes in looks

like we're missing some semicolons

alright my bad see I think we can finish

this off in ten minutes all right so

we'll go and run this program so look at

that that's pretty cool when we build

and run this we're actually seeing 0 0 1

1 1 2 2 2 so if you were following along

with what I was saying this is what we

were expecting now what if we change

these values here so let's change this

to like a 5 let's change this to a 9 so

if we go build and run that let's see

what happens

so instead now look at this we have 1 2

3 4 5 rows and if you count this across

we actually have 9 elements in each row

so there you go it's actually well I

don't think I have no time to completely

trace through all this code about why

it's working

actually why it's inserting properly and

also why it's printing to the screen

properly but you can clearly see you

know if we make this let's like let's

make this too and let's make this you

know 25 or something

just to kind of accentuate it so there

we go it's actually there's one two rows

now and 25 elements across okay so there

you go I think we're out of time now but

maybe join me in the next video and

of in will be actually trace exactly

what's getting created when and where

and I think that should really clear

things up but otherwise if you were just

looking for the code to to know how this

works

then then here it is and you should be

good to go alright see you guys in the

next video and we'll talk more about

this