Hi everyone.
So in this session we are going to learn something which is called victors.
So if you remember the oboe section so in loops with color no problem.
We implemented a glass and then all the glass was dynamic added glass.
Now what is a.
So that is basically inbuilt dynamic getting so whenever we have a feeling that I want to use that dynamic
Eddie what we will use we will use vector because it does invert it is provided by C++ so it isn't built
and we can directly use vectors.
Now what does that mean to properties of vector.
So just like the dynamic any glass that is exactly same as dynamic any glass or what will happen.
So the first minute properties basically you do not have to give the size of the vector add friend you
do not have to give.
So if you are creating an eddy.
How do we create an edit V right in a and then we will give the sides for example the scientist in mark
for creating the vector.
You do not have to give size.
So that is the main benefit of the vector.
There is no need to give size.
And the second benefit of using vector or whatever it is is basically you can insert as much number
of elements as you want.
If we consider the array you cannot push.
You cannot insert more than 10 elements.
You cannot insert eleven elements inside this area but if you are using vector since the event is dynamic.
So what will happen.
We all know the property of that base.
Basically it will double the size of entities food so for example if the event plays initially of size
to if you will push the two elements.
Basically the vector is field then what will happen that will double in size.
So now the current size of the vector is basically four and you can insert the elements here.
Similarly if this compute that base is filled then what will happen vector will again depoliticize it.
So the size will become it if this factory is full then the size will become 16.
So effectively keep doubling its size and basically you can insert as much number of elements as you
want but with Daddy.
Once you give the size you cannot insert more than 10 elements in this case but with vector you can
insert as much number of elements as you want.
And initially you do not have to give any size to career development to these other main properties
why we use vector.
So now let's see that in action.
Let us see how we can use vector so first let us let's see how do we create a vector so I have to write
the keyword vector so I will write vector Victor V.
So is this right or wrong this is wrong why.
Because vector is implemented using templates.
So basically you have to give that in a type also.
So I want to create that proof in integers so you have to write what you want to store in your vector.
I want to store integer so you have to give in Peterhead now.
As discussed many times there are two ways to create work.
One is using the static a location and the other one is basically dynamic integration so that I see
this index for creating vector dynamically so start VB vector pointer.
This is new keyword then vector in tejas and this syntax.
So this is actually dynamic and this one is actually static and we all know the difference between the
starting and the dynamic dynamic means we have to dilute it ourself.
It will not be deleted automatically we have to delete this vector but if we are looking static memory
then what will happen so this vector will get deleted automatically.
Once it will reach out of scope.
Simple.
So now let's see how we can insert the elements inside the vector.
So for inserting the elements inside the vector we have a function pushback.
So we start pushing back and let's say I want to insert certain so the suggest what is the use of pushback.
So suppose this is my vector no pushback means as the name suggests I will push the element at the last.
So Ben will be pushed at the last but since at this point of time my back is empty.
So Ben will be pushed out different because the vector is empty but if we will push more elements for
example you will push quanta and then you will push too hard.
Then what will happen.
So first they will be pushed.
Then they will be pushed.
So this will be the state of the vector 10 20 and then 30.
Okay so let's see pushback function in action.
So let us push more elements.
So let us copy this and we do not have to worry about size.
If the vector gets filled it size will become double to push as much a number of elements as you want
so currently the size of my VAT is basically three disk containing three elements.
Now how do accessed those elements.
So we have square brackets.
So just like in every we can access the elements of the vector using this credit brackets so let us
copy this and let us access the elements
visible we want and we do.
So I am pushing these three elements and then I am printing these three elements simple so let's then
our program so basically we are getting added at this line and what does that.
Let's see the edit is basically V was not declared in the scope so if you want to use vector what we
have to do we have to include a file hash include vector
so after including this file hash include vector it will not give us error so let's say in our program
and this is my output.
So that's how Victor is working
now.
Can I change the elements once I've inserted those elements obviously.
So what I will use I will use square brackets to change the elements.
So suppose I want to change the element presented index 1 So what I will do V of 1 is let's say a hundred.
So initially it was 20.
Now I change its value 200.
So let's see
so then a hundred and then thirty initially this hundred it was 20 so this squared Beckett's just like
it used to work in error.
It would also work for vectors so since vector it also has square brackets.
So what is the need of push back function.
So what I want to say here is.
So initially I have three elements inside the vector.
Now let's push the fourth element so can I push the elements using this grid backwards just like we
used to do in eddies.
Can we do this too.
Are these two lines valid.
So let us try to understand so if you will think logically these two lines should not work because if
these two lines worked then what is the use of pushback function.
So that means these two lines will not work.
So why do we need pushback function.
What is the importance of push back function.
So push back function inside the push back function what will do so there are checks implemented it
will check what is the current size of the vector if the vector is getting filled.
I have to double the size.
So all these things are implemented in push back function.
That's why for inserting the element we will always use push back function so if you will use this then
what will happen.
So the size of the vector will not increase.
So size increase class size of the vector will not increase.
So what are the elements.
Do I have.
So I have 10.
Then I have 20 and then I have 30.
Let's see what is happening here.
So suppose initially let's say the size of the vector is basically 2 Let's say initially it is.
Let us assume it is not 2 actually but let us assume the size of the initial vectors basically 2.
So that means this.
This line will create a vector of size 2.
So what is happening.
This is my back.
Push then I can push 10 then I am pushing 20 so I can push 20 then in order to push 30 what will happen.
The size then actively become double the size the vector is basically food.
So you can push the 30 element and you can push one more element you can push when what element the
size of the event base food.
Now what I'm doing here.
Change the second element so this is hundred now print okay.
Now what they are doing you are pushing the fourth element so fourth element will be 1 0 0 2 but you
are pushing the fifth element.
So this is the fifth element and add this memory you are pushing 1 2 3 4.
So basically my vector only has this much amount of memory so this memory does not belong to me.
So this memory does not belongs to us.
So basically you are pushing an element you are pushing an element at such a memory location and that
number location does not belong to us.
Okay so this is basically wrong and this is also wrong.
So let us experiment with something.
So let us experiment something so after pushing at fourth and fifth indexes let us push more elements
so we don't push back let's say twenty three and let's do push
okay.
So in order to spend everything so
okay.
So if I will run my program which would be output so ideally what should be our output.
So initially I have pushed 10 then 20 then 30.
Then I change its value 200 then I am pushing so 1 0 0 2 and then I am pushing 1 2 3 4.
And then we don't push back 23 and we don't push back to the default so if I will execute our code this
should be my output.
Ideally this should be an output Okay so please remember ten hundred thirty one 0 0 2 1 2 3 4 and then
this one.
So let's see where that output is correct or not.
Let's delete this one
so an order does allow program.
So this is the output.
So let's see whether our output was wrong or right.
So as discussed.
So ideally what should be our output.
So what I pushed I pushed pen then Bush 20 then pushed 30 then change its value 200.
Then I pushed using these square brackets 1 0 0 2 and 1 2 3 4 and then I used push back function to
insert 2 3 4 and 23 and 2 3 4.
So I leave my output should be this 1 but obviously this is our idle output is not coming out.
So what is happening.
So when you are using push malfunction to insert these elements what pushback function will do.
It will push the element at the last.
So for vector.
This is the last 30 is the last because we didn't use push back function to insert these two elements
we do not use push back function to insert these two elements somewhat push back function.
This is the last index.
So what will happen more should be output 10 then hundred then 30.
So basically 23 will come here and then 2 3 4 so 2 3 4 and then basically so these two are removed and
then what is happening so.
So these two are basically lost then I'm accessing this memory location and then I'm accessing this
memory location.
So these telemetry location are basically garbage values and analogous these two garbage values are
basically 0 1 0.
So these two garbage values are basically zero and zero.
It could have been anything but in our case they both had zero.
So that's why you should not you should never use brackets to insert.
You should always use pushback.
Function
now one more thing.
So basically so basically how this function is able to access the sixth element and similarly the fifth
element this is able to accept the sixth element because what it will do so it will say so underlying
this is Amanda Lang Eddy.
So it will go towards start address.
It will take six jumps ahead and it will print what now it is present at this garbage location.
So zero is present.
That's why as you're always getting printed simple now I have a benefit of using these grid backwards
and the alternate is basically add function the alternate is basically use of ad function.
So let's see how we can use ad function and what is the difference between the ad function and the square
brackets so one thing is quite clear here.
This is very wrong so never use square brackets to insert elements because the size of the area will
not increase the size of the vector will not increase the size will not become double.
So this is wrong.
Now let us use add function
so obviously let's remove this one.
Let's come on out everything so I want to use add function.
So currently I have three elements and then two elements here.
So the size of the vector is basically 5
so c out v don't at add function really big index.
So let us print what this present at the next full and let us also check what is present at index X
so basically index 4 is a valid index the size of the vector is currently 5.
So the last index is basically 4.
So this is actually a valid index but index X is not valid index 6 is not a valid index.
So if we will run our program let's see what will be our output
so see what is the output so at the next food duty for this peasant.
But at index 6 which is not a valid index you can see out of range the size of the vector is basically
5 and you are trying to access the sixth element.
So basically it is giving us.
Edit It is giving us added.
So what we can say so this ad function.
It basically checks whether the index is valid or invalid.
So basically we can see that using the add function is much much safer than using this square backward.
So add is much much safer because it will check the index if you will.
So if you will right we got at 6.
It will give us error that the size of the vector is basically 5 and you and you cannot access index
X but if we will use the square brackets 6 then what will happen.
It will go just like an array.
It will go to the starting position.
It will take six jumps ahead and it will print out everything whatever it is present.
So the output will be garbage now which one is better.
So obviously this memory does not belong to us.
So this is basically very bad.
Obviously this is much better.
So this is much safer using the add function is safer.
Okay.
So.
So now let us use for loop to I told our director so how we can use for loop so let us comment out everything
so let's use for loop so just like we used to isolate over the array.
We can also add later with the vector so i equals zero i.e. less than we thought size now Vidor size.
This is an build function or don't give me how many elements are present inside the vector.
So currently 5 elements are present so the value of your size is 5.
And here you can use vilify.
And here you can use the square brackets and you can use at.
So at this.
So inside the for loop both you can use both.
You can use Square Beckett and you can also use at the boathouse same way because index is always valued
because of the for loop Woodward size will be 5 here inside the for loop it is safe to use board square
brackets as well as the add function Mutassim so if you will run the program what will be our output.
It will print the content of the vector so this is the content then handed today 23 and 24.
So now we know how do I get over direct bed.
So just like the pushback function I also have Bob back.
So what Bob that function will do so v dot Bob back
So bob back function what it will lose.
So it will pop out the last element.
So what is the use of pushback Pushback will insert the element at last.
And Bob Beck will remove the last element.
So basically this was the last element to form.
So this element will be removed and the seismic activity become forward so four elements will be printed.
Let's see
so four elements are getting printed the size the vectors containing only four elements
normally just kind out everything.
And let's do some experiments
so let us use a for loop to insert the elements inside the vector so let's say I want to push a hundred
elements so when it does push hundred elements so we don't push back
a plus one so I am pushing hundred elements and let us also print out our vector so I equals zero.
I list and we out size and I plus plus
C outreach if I
so I am pushing hundred elements and then I am printing those hundred elements simple.
Let's see
so these are the elements inserted starting from when they landed.
So yes civil code is working fine.
Now let us see.
Now let us also print the size.
So when I am pushing the elements.
Let us also print the size of the vector.
So see out v dot size
now.
What should be our output what we what size will contain.
So this size function foster it will contain one element.
Then it will contain two then three then four.
So basically this Vidor size it will increase linearly.
And finally the size of the vector will become a hundred simple.
There should be an output 1 200 the size the Vidor size.
So the size of a vector will increase one by one.
Simple so let's remove this thing and now we are liberating the size so let us also right size it what
is the size of the vector
and let's give and line also so let's see
so you can see when you push it when you enter size will become one when you push the second element
the size becomes too.
When you push the third element the size becomes three.
And so on.
And finally when you will push the hundredth element the size of the vector will you become a hundred
simple.
Now we also have a function capacity so I have a function capacity.
So what this capacity function will do.
So it will give me what is the capacity what is the current capacity of my vector that will give me
what is the current capacity of my vector and the difference between capacity and sizes size will give
me how many elements at present.
So for example let's say three elements are present.
So size will give me that three elements that present me this capacity will give me how many elements
you can insert inside the vector for example for example six.
So the capacity will be six.
Now let us print but we will print capacity and size and I'll deliver it should happen.
So that capacity will become double capacity will become double as soon as the vector is full.
So let's check it out
so let's print the capacity
b don't capacity.
So this is also an build function function of basically vector and let's remove sides from here
okay so let's Renamo code and let's see what will happen
so let's try to understand what is happening here.
So initially the capacities 0 and the size is also 0.
Strange array of size 0.
So basically this means that at starting and it didn't really exist and is basically null it doesn't
exist when you push the first element the capacity becomes 1 and the size becomes 2 when you will push.
So this is the first element the capacity is 1 in the size is when so many will push the second element
that capacity will become place.
So this is the capacity capacity becomes two and size is also 2 when you will push the third element.
The capacity will become for the size will become double the capacity will become double the size the
vector is basically for now.
So you can push the fourth element and the capacity is still full but when you're pushing the fifth
element the capacity becomes double.
So the capacity it's basically the vector capacities it so you can safely push element six.
You can push the element seven you can push element date but when you are trying to push the ninth element
the capacity doubles so it will become sixteen so you can push that element girl 13 14 and so on.
So finally when you will try to push the 17th element capacity will become 32 and so on.
So it will keep going on.
So finally let's see so finally the capacity is undaunted.
And this is enough to hold a hundred elements.
So capacity 128 is enough to hold these main number of elements okay.
So we have seen how to create best and dynamically we know how to push elements.
We know the capacity function we know the size function we know the public back function.
So this when we dart ball back we know how to pop element we know how to push element we know how to
change the elements we know how do I create all the director.
So pretty much we know all the major functions of Director Okay.
So when lasting how to create a vector of a given size for example I want to create a vector let's say
of size 10 so you can give size here to giving size is optional.
If you want to give the size you can if you don't want to give then don't give okay.
So what if you want to initialize this vector so comma let's say I want to initialize this vector with
the minus two so this victory has been initialized all the 10 values are basically minus two.
You can also see.
So let us print this vector OK let's change the name B to so equals zero list and we do a plus plus
and see out
symbol
okay.
So we do large sites
so we'll see the vector has been interfaced or the 10 elements are minus two.
So that's how you initialize of the
so it is not necessary to initialize the vector.
So if you write like this vector of indigenous v V3 and you can only give size.
So this is also correct.
Okay so can we start the vector.
So yes we can start direct it.
I haven't ruled functions out.
So what do we do.
I will write like this for example I want to sort vector we to suicide we do not begin
we do dart and so just like Eddie we have to give the starting in the end items.
So sort of you got begin and we do not end and my vector will become sorted my vector will be sorted.
Okay so we know how to initialize we know how to give how to create a vector of a given size we know
how to sort of vector so pretty much we know all the functions all the major functions of the vector
okay.
So I think this is enough.
I will show you in the next one by.