Hi, everyone.
So in this session, we are going to learn something which is called Vector's.
So if you remember the U.S. Suenos, we cover the problem.
We implemented a class and the name of the class was dynamic class.
Now it is a vector.
So vector is basically inbuilt dynamic.
Very.
So whenever we have a feeling that I want to use that imagery, what we will use, we will use Vector
because it is and it is provided by C++ so it isn't built and we can directly use vectors.
Now, what are the main two properties of vector, so just like the Democratic class, that is exactly
the same as dynamic class or what will happen to the first property is basically you do not have to
give the size of the vector add friend.
You don't have to give.
So if you are creating an area, how do we create a net without end?
And then we will give the site, for example, the existing word 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 that already is, is basically you can
insert as much number of elements as you want.
If we consider the day you cannot push, you cannot insert more than 10 elements, you cannot insert
11 elements inside this area.
But if you are using vector since the vectors dynamic, so what will happen?
We all know the property of vectors.
Basically, it will double the size of this field.
So, for example, if the workplace initially decides to.
If you will push the two elements, basically, if the victories failed, then what will happen next?
So now the current size of the vector is basically four and you can insert the elements here.
Similarly, if this computer that is filled, then what will happen?
That will again the size.
So the size will become it.
If this factor 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 that, 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 don't have to give any size to create development vector.
So these are the two main properties.
Why use vector?
So now let's see Vector in action.
Let us see how we can use vector.
So first, let's let's see how do we create a vector?
So I have to right the key word, Victor.
So I will write Victor Victory.
So is this right or wrong?
This is wrong.
Why?
Because Victor is implemented using template.
So basically you have to give the dinner table.
So.
So I want to create vector of integers.
So you have to write what you want, the story in your vector.
I want the story, A.J., so you have to give in here.
Now, as discussed many times, there are two ways to create victory.
One is using this tactical location and the other one is basically dynamic location.
So now see this index for creating vector dynamically.
So Star V.P. Victor Point, this is New Keever, then Victor.
Indigence and this Syntex, so this is actually.
Dynamic and this one.
It is actually static and we all know the difference between the static in the dynamic dynamic means
we have to dilute it ourselves.
It will not be deleted automatically.
We have to delete the structure.
But if we are allocating 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 push back so we don't push back
and let's say I want to insert them.
So as the name suggests, what is the use of push back?
So suppose this is my vector now push back means, as the name suggests, I will push the element at
the last.
So 10 will be pushed at the last.
But since at this point of time a vector is empty.
So 10 will be pushed at different because the effect is empty.
But if you will push more elements, for example, you will push twenty and then you, if you will push
thirty, then what will happen.
So first 20 will be pushed, then 30 will be pushed.
So this will be the state of the vector 10, 20 and then 30.
OK, so let's push back function in action.
So let us push more elements.
So let us copy this.
And we don't have to worry about sales, if their vehicle gets filled, it size will become double.
So push as much a number of elements as you want.
So currently, the size of my vatos, basically three days containing three elements.
Now, how to access those elements, so we have square brackets, so just like in any.
We can access the elements of the vector using the square brackets.
So let us copy this and let us access the elements.
V0, we went and we do so I'm pushing these three elements and then I am printing these three elements
simple.
So let's run our program.
So basically, we are getting at it at this line and what is the reality?
The error is basically V was not declared in this scope.
So if you want to use Vector, what we have to do, we have to include a file, has to include vector.
So after including this file has concluded that it will not give us error.
So let's turn our program.
And this is my output, so that's how that is working.
Now, can I change the elements once I have inserted those elements, obviously.
So what I will use, I will use square backwards to change the elements.
So suppose I want to change the element presented the next one.
So what I will do while one is, let's say and so initially it was 20, now I change its value 200.
So let's see.
So then hundred, and then today, initially this hundred, it was 20.
So this is great, because just like it used to work in that, it would also work for Victus.
So since Victor also has square records, 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 us push the fourth element.
So can I push the elements using this great bracket just like we used to do in Eddies?
Can we do this?
So are these two lines valid, so let's try to understand.
So if you will think logically, this should not work, because if these two lines worked, then what
is the use of push back function?
So that means these two lines will not work.
So why do we need to push back 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 on 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 way 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 two.
Let's say initially it is to let us assume it is not to actually but let us assume the size of the initial
vector is basically two.
So that means this, that this line will create a vector size two.
So what is happening?
This is my vector.
Bush, then I can push down, then I'm pushing 20 so I can push 20, then in order to push 30, what
will happen the size that will become double the size, the vector is basically four.
So you can push the 30 element and you can push one more element.
You can push one more element, the size of the vector is four.
Now what I'm doing here change the second element.
So this is 100.
Now print.
OK, now what they are doing, you are pushing the fourth element, so fourth element will be one zero
zero two, but you are pushing the fifth element.
So this is the fifth element.
And at this memory, you are pushing one, two, three, four.
So basically, my vector only has this much amount of memory, so this memory.
Does not belong to me.
So this memory does not belong to us, so basically you are pushing an element, you are pushing an
element at such a memory location and that location does not belong to us.
OK, so this is basically wrong.
And this is also wrong, so let us experiment or something, so let us experiment something.
So after pushing at fourth and fifth in Texas, let us push more elements so we don't push back.
That said, 23 and let's see.
Bush.
OK, so now let us pretend everything's so.
OK, so if I will run my program, which would be my output.
So ideally, what should be our output?
So initially I have pushed 10, then 20, then 30, then I change its value to 100, then I am pushing.
So one zero zero two and then I'm pushing one, two, three, four, and then we don't push back 23
and we don't push back to 34.
So if I will execute our gold, there should be more output, ideally, there should be an output.
OK, so please remember ten hundred thirty one zero zero two one, two, three, four, and then this
one.
So let's see where their output is, correct or not.
Let's delete this one.
So order to start our 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 at output?
So when I pushed, I pushed Ben, then pushed, wanted and pushed hard, didn't change its value.
Two hundred.
Then I pushed using the square brackets, one zero zero two and one, two, three, four.
And then I use push back function to insert two, three, four and so twenty three and two, three,
four.
So I leave my output with this one, but obviously this is, our idle output is not coming out.
So what is happening.
So when you are using function to insert these elements, what push back function will do, it will
push the element at the last.
So far 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, so forth, push back function.
This is the last index.
So what will happen.
Merchantmen output, Ben, then hundred, then 30.
So basically twenty three will come here and then two, three, four, two, two, three, four.
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 memory locations are basically garbage values.
And in our case, these two garbage values are basically 090.
So these two garbage values are basically zero and zero.
It could have been anything but in our case, they both are zero.
So that's why you should not you should never use brackets to insult.
You should always use push back 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 access the sixth element because what it will do so it will say.
So underlying this is my underlying Eddie, so it will go to its start address, it will take six jumps
ahead and it will print whatever is present at this garbage location.
So zero is present.
That's why zero is getting printed.
Simple, now I have itinerate of using this square because the alternate is basically afunction, the
alternators basically Yusoff add function, so let's see how we can use add function and what is the
difference between the add 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 array 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 comment out everything.
So I want to add function, so currently I have three elements and then two elements here, so the size
of the vector is basically five.
So, Scott, we don't at all function will take index, so let us print what is present at the index
for.
And let us also check what is present at next week's.
So basically, index four is a valid index.
The size of the event is currently five, so the last index is basically four, so this is actually
a valid index, but index six is not valid.
Index six is not a valid index.
So if we will run our program, let's see what will be our output.
So we'll see what is the output.
So at the next four to three for this president, but at the next six, which is not a valid index,
you can see out of range the size of the is basically five and you are trying to access the sixth element.
So basically it is giving us added.
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 bracket.
So it is much, much safer because it will check the index.
If you will, so if you will, right without attic's, it will give us error that the size of the vector
is basically five and you and you cannot access indexes.
But if we will use the square, because then what will happen?
It will go just like an error.
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 that function is safer.
OK, so.
So now let us use for Loop who our director.
So how we can use for Loop, so let us comment about everything.
So let's use for loop.
So just like we used to, outraged over the area, we can also treat all the victims Y equals zero aliens,
then we Nazis.
Now, Vidaurre says this is inbuilt function or do you give me how many elements are present inside
the vector?
So currently five elements are present, so the value of your size is five.
And here you can use Wi-Fi and here you can use the square brackets and you can use add.
So at this so inside the for loop both.
You can use both, you can use square beckert and you can also use and they both have same way because
index is always valid because of the broad size will be five here inside the loop.
It is safe to use both square because as well as the function Mutassim.
So if you run the program where we are 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 to edit it, the vector, so just like the push back function, I also have back.
So what Bob function will do, so we get Bob back.
So function what it will do, so it will pop out the last element.
So what is the use of push back?
Bush will insert the element at last and Bob will remove the last element.
So basically this was the last element two, three, four.
So this element will be removed and the director will become four.
So four elements will be printed.
Let's see.
So what elements are getting printed the size, the effect is containing only four elements.
Now, let's go find out everything and let's do some experiments.
So let us use a follow up to insert the elements inside director.
So let's say I want to push hundred elements.
So let us push a hundred elements.
So we not push back.
Iblis one.
So I am pushing a hundred elements and let us also print out our vector.
So I was zero, I lived in brasses.
And I placeless.
CLV OWFI.
So I am pushing a hundred elements and then I am printing those 100 elements simple, see?
So these are the elements inserted starting from one 800, so, yes, it is working fine.
Now, let us see.
Now, let us also print the size, so when I'm pushing the limits, let us also print the size of the
vector so we don't size.
Now, what should be our output?
What we don't say is will contain so the site function first, it will contain one element, then it
will contain two, then three, then four.
So basically this report says it will increase linearly.
And finally, the size of the vector will become simple.
There should be allowed output one 200.
The size, the door size, so the size the vector will increase one by one simple.
So let's remove this thing.
And now we are only printing the size.
So let us also right here.
What is the size of the vector?
And let's give and also.
So let's see.
So you can see.
When Bush Winnellie undersize 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 limit, the size of the vector will become simple.
Now, we also have a functioning capacity.
So our 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?
It 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 are present.
So, for example, let's say three elements are present.
All sides will give me that.
Three elements that present various 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 ideally what 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 us bring the capacity.
Don't.
Capacity, so this is also a function function of basically Victor.
And let's remove sales from here.
OK, so let's go and let's see what will happen.
So let us try to understand what is happening here.
So initially the capacity is zero and the size is also zero.
Strange area of size zero.
So basically this means that starting added, we don't really exist and is basically does not exist.
When you push the first element, the capacity becomes one and the size becomes two when you will push.
So this is the first element.
The capacity is one and the size is one, so when you will push the second element, the capacity will
become twice.
So this is the capacity.
Capacity becomes too, and size is also, too.
When you will push the third element, the capacity will become for the size will become double, the
capacity will become double the size of actors basically follow.
So you can push the fourth element and the capacity is still four.
But when you are pushing the fifth element, the capacity becomes available.
So the capacity.
It's basically the vector capacities it, so you can safely push element six, you can push element
seven, you can push limited.
But when you're trying to push the ninth element, the capacity doubles.
So it will become 16.
So you can push 10, 11, 12, 13, 14 and so on.
So finally, when you trying 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 128 and this is enough to hold 100 elements, so capacity 128 is enough
to hold these many number of elements.
OK, so we have seen how to create bestriding and dynamically we know how to push elements, we know
the capacity function, we know the same function.
We know the pop back function, so this one Vidarbha back, we know how to pop element.
We know how to push element.
We know how to change the elements.
We know how to get rid of a director, so pretty much we know all the major functions of director.
OK, so one last thing, how to create a better offer given, say, for example.
I want to create a vector, let's say, of size 10, so you can give size here to giving sizes optional
if you want to give the size you can.
If you don't want to give, then don't give.
OK, so what if you want to initialize this vector, so comma, let's say I want to ask this vector
with minus two.
So this victory has been neutralized all the time, values are basically minus two.
You can also see so let Sprent distractor, OK, let's change the name to.
So equals zero, iList, and we do.
I placeless.
And to see out.
Symbol.
OK, so we do our site.
Solice.
The vector has been analyzed, all the elements are minus two, so that's how you initialize the vector.
So it is not necessary to initiate the victor, so if you're right like this.
Rector of Indigenous Vetri, and you can only give size, so this is also correct.
OK, so.
Can we start the victor, so, yes, we can, SA Director.
I haven't malfunctions, Art, so what we will do.
I will write like this, for example, I want to start victory to.
We do not begin.
We do not and so just like Eddie, we have to give the starting in the end outrageous.
So I thought we would not begin and we do not end and my victory will become sorted.
My victory will be sorted, OK, so we know how to initialize, we know how to give, how to create
a bit of a given size.
We know how to sort of act so pretty much we know all the functions, all the major functions of the
vector.
OK, so I think this is enough, I will see you in the next one, bye bye.