hey everyone I hope you all are safe and doing good so in the series of learning C++ programming language till now we have discussed what is Vector what is pair and what is Vector of pairs in STM in C++ right now the next topic is about nesting of vectors nesting means like uh Vector within Vector these kind of things right so here we have two things one is array of vectors and next is Vector of vectors so these two things are little bit confusing so we'll see both these things in uh this video only we'll differentiate these two things with the help of proper program I'll show you everything about what is array of vectors and what is Vector of vectors right and how to use these things right now before that I just want to tell you one thing if you are gate aspirant means you're preparing for gate 2025 or if you are planning to prepare for gate 2026 then there is a great news for you guys from unacademy now you can get one year unacademy gate subscription at lowest price ever around uh rupes 12,000 and in just one subscription you will get access to all the batches uh test series as well as digital notes and the new batches for gate 2025 as well as for gate 2026 are going to start from 30th of April right and now gate es environmental science and gate instrument engineering is also available in an academy right but the sofware is valid till 27th of April right you have to take care this thing this lowest price offer right so if you are a gate aspirant then don't wait grab it now and just accelerate your gate preparation with unacademy and use my code Jenny to get this over so all the relevant links and details I'll put in the description box of this video you can go and check out okay so back to the topic now now first we'll see what is array of vectors I hope you know what is Vector what is array right array is what basically what is a collection of elements of same data type which are stored in continuous memory locations right and one thing is what main drawback of array is what arrays of fixed size like this if I declare int array name is a and five means here a is an array which can store only five elements of integer type right so AR R of having fixed size now Vector is what it's a dynamic array which can also store like same as array it can uh it store element of same data type right but it can increase automatically its size according to the requirement this is what Vector so we call Vector is a dynamic array vectors are not of fixed size right like this we can uh the synx of vector is we just write vector and here the data type int and Vector name that's it no need to give the size and all this is V Vector according to our requirement we can like uh put or push back or add as many number of elements as we want in Vector right you can check out the maximum size of a vector many elements like we can store here about vectors and arrays we have discussed in the previous videos and this playlist you can check out those videos first right now what is array of vectors just combine both the things array of vectors right see if I write something like this vector and suppose data type I'm taking int only this is what Vector but I want to take array of vectors if I want to take like uh array of integer means this is the thing array of five integer now I want to take array of five Vector in an array I want to store five Vector so the synx is almost same here this array name V and just you have to give size like five so this is what array of vectors if you write something like this means how you are going to uh find out what is this thing just start with this the variable name V and in spiral like in clockwise Direction you have to move something like this to find out what is this like here we have V if you move something like this since this is what in subscript you have five the size means array like so V is an array which can contain five vector vector of type integer so this is how you can find out what is this thing right I hope you got now why I'm calling this array of vector because syntax is almost same here array of five integer here array of five Vector right but what is this now array of vector how to if I you know uh visualize this thing then here like 0 1 2 3 4 five integer I can store like maybe 10 - 1 0 1 2 like this right index is 0 1 2 3 4 but now I want to store rather than single element I want to store in this array I want to store five vectors five vectors so this is name is V index the same 1 2 3 4 but here rather than individual elements now here we have a vector complete Vector within this this also complete Vector within this also a vector within this also a vector like this and we know Vector is a dynamic array I can store one element Two element three element or as many element I want right so how to visualize this thing how to visualize array of vectors see it is a 2d array something like this see see this is an array here first in zero both index 1 second third fourth and each index we have a vector complete Vector so at this index I have a vector of 0 1 2 3 size 4 at this I have a vector of size two Vector of size three size 4 size 1 2 3 4 5 6 6 so like this this is a 2d array this is how we are going to visualize I mean obviously in memory this is not going to store in this form only we are just visualizing this thing it is a 2D array right where number of rows are fixed and each row is a vector number of rows are fixed why because we have this is an array we have given the size that is five so number of rows are five only but each row is a vector so each row is having variable number of columns I mean as anything the number of columns can be anything but number of rows are fixed in array of factors I hope you are getting this point right so here we can say every index every index of this array stores a vector of variable length and we can access these elements of the vector using iterators yeah we can use iterators with vectors we have done this thing previous in previous videos right so number of rows are fixed but number of columns are you can say in 2D array uh can be any number of columns or can be variable right these are not fixed length having any number of length because these are this is Vector this is one vector second Vector third four five five Vector so this V is an array of size five which is having Vector of in inte iser Vector right so basic General syntax if you want to write then you can write down like something like this Vector here the data type here array name then in this subscript array size that is a simple syntax to cleared array of vectors right now let me show you this thing with the help of a program so let's create a new file here array of vectors and same include those header files we have a vector no not a vector we have an array of vectors so name I'm taking V only you can take AR r r x y z any name you can take right or array of vector name you can also take like something like this array of Vector like this but a simple name like V I'm taking so in subscript just uh give the size I'm taking three that's it right so here B is our array of vectors of size three I mean integer vectors right now how to put elements in this array of vectors this is an array within array every index of array is a vector right so I can simp can I simply write V Dot like push back and uh in bracket just put one no this would be wrong sorry it's dot see this expression must have class T but it has Type uh this vect and all because V is not a vector push back member function we can use with Vector right but I mean Vector object obviously but this V is not a vector now this is an array so with array we cannot use push back right within array like array is V so V of z v of Z is a vector right V of 1 is also a vector v of 2 is also a vector because at every index every element of the aray is a vector now V of Z is a vector so with v of Z I can use push back do push back one see here it is not giving any error right so write this one ways uh to insert element within this Vector is this thing right array of within VAR of vector so V of z v of 0 Two element I just want to insert that's it now same because here we have now three vectors so V of 0 in V of 0 at first Vector I have inserted two values now second Vector is at V of 1 so there I can insert as many values I want using push back at a v of two I have third Vector there also you can insert as many value as you want right so let's insert so I have inserted one value in first Vector then you have one only sorry two value in Vector first Vector second Vector only one value and in third Vector I have inserted three values this is one way obviously this is not you know uh efficient way to insert values like this we can use four Loop but that also I'll show you now let's print these vectors so obviously it is a 2d array so for that to print one uh we need two Loops one is to trace the number of rows one is to handle the number of columns two Loops you are going to need so outer loop is for obviously number of rows so for simple I'm taking I equal to 0 and I less than outer loop I'm saying is for how many uh elements in there in that array size of array is three so outer loop will run till three so I should be less than three right and I ++ now within this we have another loop also to print each Vector right so let's take another value another uh obviously variable to trace to print the each Vector J is equal to z and j should be less than they should be less than this is what I equal to at first zero means at first at zeroth index we have now at now we are at zeroth index in the array and within that array at zero index we have a complete Vector so for that to print that Vector I'm taking variable J so now at first J is zero so at first element of zero index of that Vector till we are going to move Vector size because we don't know what is the vector size because Vector are of Dynamic Vector Dynamic array right so how to how to put that termination condition less then till Vector size we have a function size we can use that thing but we cannot use V do size because V is not a vector v is actually a ARR V of I do size and then j++ I hope you got this obviously this is a function so you have to put these brackets right V of I means at first I zero so V of zero do size the vector at zero index then I is equal to 1 once I become one then V of I do size so right uh this is how we can print all the vectors within the array so now how to print in see out we use if you use subscript then first VY at which row we have V of zero Row V of zero means first element of the but that element is itself a vector so how to access the element of vector we have for that we have this variable G so V of I write down some space after every element right that's it and after printing every Vector I want a the next Vector should print in next line so after this with inner for Loop we just print C out e andl for a new line new line operator also you can put right I hope you got this synex or this logic let me run this and we'll show you what output you are getting so we have first Vector then we have 134 in next Vector we have only four and in third Vector we have three elements right so this is how we can print but rather than this push back push back one more method we have with for Loop also we can use so obviously it is a 2d array so you need obviously to in for taking input also you need two Loop right so first Loop should be I less than tell the size of the array three because for three rows I want to take input right I ++ now within this now I want to take individual input as an uh individual Vector as an output but I don't know the size of vector right so how many element you want want in that particular Vector first obviously you will ask from the user so for that let's take uh a variable int and N right see out enter size of vector and which vector suppose enter size of vector 1 enter size of Vector 2 Enter size of vector three three Vector are there because size is three right so how to print that I + 1 because I is zero at first so I + 1 is 1 so it will ask you enter size of vector 1 now C in and we have variable n like this we can take the size of this Vector so now inner for Loop will go till 0 to less than size of vector is n from user I'm taking that input and j++ now we take actual elements of that Vector from the user right so for that let's take let's take um variable in X and just I'm taking C in a variable a I'm taking C in a and v of I dot push back and here we take the variable we have taken a v v do push back we cannot write down because V is not a vector here V is an array of vectors and V of I at first I is zero so V of Z is a vector we know V of 1 is a vector v of2 is a vector because because this every element of a is a vector so V of I do push back is fine and whatever you will input that would be push back into that Vector now one way is this this is how we can take input let's run this Ander size of vector one suppose three so you now you have to enter three element 1 2 3 enter size of vector two suppose two 1 2 1 2 Enter size of vector 3 only one 45 see first Vector is 1 2 3 then 12 12 then 45 right decorate a little bit after Vector one should print some space or uh this kind of thing and if in output also if you want to print like size of vector one size of Vector 2 size of vector 3 then also we can print so before printing these actual values we can first print here the number of elements in that particular Vector so using C out number of elements in Vector in which vector vector one so I + 1 because I is zero R how many elements would be there whatever the size of that vector so V of I dot size semic after this obviously we want Vector should be printed in new line so e and DL let's run this see number of elements in Vector one are one only 23 in Vector 2 we have inserted three elements in Vector 3 we have inserted four elements so like this so this is how we can use array of vectors here number of rows are fixed right so now let's see what is Vector of vectors so now we are clear with array of vectors here we have fixed number of rows but number of columns can be anything but if you want if you don't want fixed number of rows you want that I can take as many number of rows as I want right so both number of rows and number of columns I want Dynamic variable not fixed in that case we use Vector of vectors now how to uh declare Vector of vectors see see this is an array of vectors but I don't want any array I just want a vector so no need to specify the size so this is a vector of type int right but I want Vector of vectors in this Vector I can store only integer value but I want in a vector I can store vectors so here data type you have to modify the data type should be Vector so vector and type in I'm taking int the data type is now Vector itself and the name p so this is what vector v of vectors right so here we have a vector of any length I don't know any number of uh elements we can store in this Vector because this is dynamic array but each element of this Vector is a vector right so it is a you can say 2D Vector number of rows are also not fixed number of columns are obviously not fixed right so like the 0 1 2 number of rows I can take as many number of rows as I want and within this number of rows also there are vectors of variable length right so it is also a 2d Vector right so each index of this Vector like V of V of 0 V of 1 each index of this Vector is a vector itself and we can access the element of vector we know simple for Loop also and we can use iterators as right same function we can apply here those push back pop back erase clear these functions right so let me show you this thing also with the help of uh program let's create a new file Vector of vectors and so now we will take a vector of vectors so the in this type name type name obviously we will take Vector only so Vector integer type and V so here V is a vector of vectors each element of this Vector is itself a vector right now directly how can you use we can use push back here V Dot push back can we use yes we can use because V is a vector here it's not an array we is a vector so we can use push back but now every element of this Vector is a vector so in push back we cannot simply write one this will give error right because it is expecting what a complete vector and one is simply a you know simple value I'm giving no a complete Vector so like this we can like uh 1 2 3 4 this is a complete Vector having only two values that is fine it will work right okay this is for this is qu quiz question for you guys if I insert something like this will it work yes obviously because we are taking this one now in the this Braes curly Braes so this is what a vector this is considered as a vector here because this is how we can give uh we can initialize or this initializer list for Vector right in curly bracket you can give one value that is fine but without curly bracket you can't give single value right this is also fine right so this is a complete Vector this is a complete vector and suppose we do push back one more Vector I going to insert here Min -1 67 so now we have three vectors within that Vector right so let me print these three vectors now so four now obviously we need two Loops one is two for the number of rows and one for number of columns but here number of rows are not fixed we don't know the size in previous case number of rows was three so we know the size so we have put here I less than three that is fine but here we don't know number of rows we don't know how many vectors are there within this Vector right so number of rows are also not fixed so we can simply use that which function dot size to calculate the size and I ++ now this is for number of rows now within rows we have each row is a vector itself so for that to X is the element of that Vector we need a for Loop so rather than simple variable this time I'm going to use um I'm using here iterator to access those Traverse to Traverse that Vector right so let's have a uh iterator it now V of I do begin right V of I do begin because now we are within that inside Vector inner Vector so V of I at first I Z so V of Z first element I'm accessing of that Vector outside vector v so V of 0 do begin it will point to the first element of that zeroth Vector right the the vector at zeroth element okay so we of Z do begin until I T should be less than V of or not less than it not equal to V of I do end and it ++ so these iterators concept we have uh already discussed in detail in previous videos you can check out those videos first so this is it equal to okay this end is also a function so obviously we use this bracket it should be uh not equal to sorry right now see out we just use estc it because it is now obviously it's iterated so it is you can say containing the location or the address of the element so how to dreference the pointer it's like a pointer so how to deference it using SRI right this SRI this will give whatever the value at that particular address right now after this same we use emdl so here we using this AO keyword I hope you're getting why I'm using this AO keyword because see we are using directly here do begin the iterator so it will automat automatically detect that yeah it is an iterator so it will automatically becomes an iterator that is why we are using this Auto keyword here right to detect automatically the type of whatever of the variable we are using here right now let's run this okay before this it should be I less than b do size right I was just checking you guys are attentive or not right uh so let's run this and see what output you are getting here see both the three vectors it is printing both the three vectors right now using iterator I have accessed elements now here also rather than simple variable we can use iterator here also right now same you can use push back uh sorry Pop back function so if you want to use do popb so it is going to remove the last element so we can say the last element means the last vector this was the last Vector to be inserted right and after pop back if you again print so just put like after pop back and before there were three vectors and after pop back we have only two vectors the last one was removed the latest one right same you can use your insert if you at specific position you want to insert a vector then you can use if from a specific position you want to delete something you can use erase function for insert what you need to do suppose uh after pop back at at this location after this first Vector I want to insert a uh Vector right so before pop back or to insert you need to give one the position where you want to insert as well as the value so position means obviously you give that position using iterator right so let's first create an iterator iterator of vector of vectors so this is the formula like I'm creating it and it is equal to V do begin at first I'm assigning this at at starting the it is pointing to V do begin beginning of the vector v and now V do insert I want to insert something so first at second position at like first index 0 1 so it + 1 this will give the position as well as what value you want to insert only uh this Vector I want to insert at it + 1 I mean at first index I mean after this vector and before this Vector right so yeah that's done right and after that if you don't do any pop back and after insertion let's just print simply what whatever the vector is so let's run this and see what output will get here see first was these three vectors and after that these four vectors 5 6 7 8 this Vector has been inserted at the specified position right same you can use arrays here there also you need to give location I mean the address so there you will give obviously iterator right so just try out these different different functions whatever function member function you can use on Vector you can use on Vector of vector so this is your assignment you just try out all those member functions and if you're finding any uh you know if you're stuck somewhere you can just comment down here and we can make a separate video on this right so I hope difference between array of vectors and Vector of vectors is clear to you guys right in the next video we'll see some coding exercise based on uh these concept right so now I'll see you in the next video till then bye- bye take care