 What's up guys? My name is Michael and welcome to my YouTube channel today We are going to go over all of the functions of vector. So I know this is like a thing I used to have a series of you know Learn data structures in I don't know 14 days on On my channel, right? And that was at that time I didn't actually go over like I didn't actually code code any of the data structures And that was not actually a good thing So today I'm actually going to go over every single member function for each data structure in C++ for every of the All of the templates so everything from STL that we could go over So these are just gonna be the basic data structures You could probably learn it a lot yourself. You could Google the rest, but I'm gonna go over all the values. Okay Okay, so in this video, I'm just gonna go over the vector data structure because it's a pretty pretty basic data structure It's kind of like arrays but in that the difference between a vector and arrays that you could resize it and Resizing is pretty efficient. It's very efficient And you could change it as much as you want and you could shrink it as much as you want And that's what's the best part about vectors. So how do you create a vector? Let's open up the visual studio Simply put vector and put a lesson sign and then do whatever whatever data type you want. So it could be Boolean It could be a char. It could be string. It could be Anything could be long long Me I'm gonna just use integer for now and let's just call it a I don't know data Okay, and what this does is it just creates initializes a vector So this is just gonna be a vector We're basically like an array but that we could resize the size as much as we want and we don't have to you know We don't have to do as much as we think we don't have to create like a specific size Okay, now, let's say you want to want it to create a specific size. So let's say I wanted to have only 10 I want to have 10 elements Right, what you would do is you would just put parentheses This is the less the left parentheses, right parentheses, and then you put 10 and I'll create data with have 10 elements And it's gonna be all initialized to 0 and if you want to see if it is all in initialized to 0 All you have to do is just loop We're gonna start from 0 and we'll go to up to So all vectors always have a function called size Dot size and you could always use that to find the size of a vector So you would use the name of the vector that we created which is data And we do that size and now it's just going to Loop over all the values in data and it's just gonna print out every one of them So here we're gonna do see out data at I and we save now if you run this It's just going to print out all the values of data and it should all have value 0 Ideally, I should have all values here. Let's see wait. Yeah, see it has all values here and it's 10 elements. So yeah So that's a good thing about Vectors is that if you want to create a specific size you could just put a parentheses and pass in your size in there Now what if I want to change your value in data? So let's say I want to change a value and I'm at my vector Well, it's the same thing as you could access this the value in array All you have to do is do data and use the name of your vector right data is the name of our vector, right? It's data, but you could change this name to anything that you want and use what you would do is you put Whatever index you want it to be so you would have your left bracket and right bracket Whatever index you want to be so let's say I want to change the initial index of zero I just change it to I don't know five, right? So what this does is it's going to change the first value of our data At the index of zero, right and it's gonna change it to five. So then when we loop through everything It's gonna have the first value of five and the rest are gonna be zeros See first value five rest are zeros. So yeah, and the reason why it's all zero the rest are zeros because Remember whenever you initialize something with a number here with your size initial values of a vector are always gonna be zero Now let's say we don't want to Initialize all the initial values to be zero We could do is we could just put a comma and then it's gonna tell us what value you actually want it to be So let's say we I want to change it to all have I don't know value five. Okay Now if you now if you are not on five, let's say one, okay Now if you were to run this It's gonna have all the values are gonna be one and it's gonna have the first value of data is gonna be five Yep, see all the values are gonna be one and the first value is gonna be five, right? So that's what this good part about the vector is that the vector tells you the first value of the vector The first thing is just the size of our vector and the second one is the Number number to initialize all the values of our vector what it would be That's the first good parts about vectors Now let's talk about Let's talk about the function. So we already know the functions of a Vector is that we have dot size which gives us the size of the vector But there's also a ton of functions that you could use One of the things you could use is actually you could use a push pushback. This is something I use a lot Basically what a pushback does it's just gonna add a new value to the end of your vector So what we're gonna do now is we are going to actually take this Take this Take this value data, right and we're gonna just add Let's push back to zero So at the end of this if I were just to loop through That's actually coming out this comment Coming up actually, let's just move this prints to prints to the end So what I did was I just uh, I just pushed zero to the end Of our vector. So what that means is it's going to add it attach it to the end of the vector The value zero to the end of the vector. So then when we loop through the size I zero to data dot size, it's just going to print out all the values of the vector so See Yeah, so so you have five all ones and then we have zero Which is the value that we just pushed to the back of our vector Now um, now let's actually talk about What we could do the other functions that you could do on a vector. So here, um I already went over push back, right the modifiers push back um Now what you also could do is you could pop back So what this does is deletes the last element of the vector So if you go on the c++ website, it actually gives you all the functions of the vector and let's actually just go over all them Okay, so first of all, um We could pop back also. So this is going to remove the last element. So what if I want to remove the last element? Uh, so currently our we have zero at attached to the last element of our vector So now if I want to remove this, I'll just remove pop back And it's going to remove that zero in our last element of our vector And then if I were to print it again So let's actually Let's actually print out a statement before pop back it's going to print out So I'm going to have to print out the statement that says before the pop back I just print out my array and then I'm going to print out this again after the pop back The pop back and we're going to print out the array again. So now if we were to run this Print out the vector again. Yeah, my bad. If you run run this again Let's actually look So we would have five all ones and then a zero, right? And then after the pop back, we just have five in all ones because it removed the zero at the end our Vector, so yeah, that's how pop out works. Uh, it just removes the last element Now let's go over the other Functions of the the vector. Okay, so we went over push back and we over pop back You could actually do an insert. So if you want to see what the insert function does, let's actually open this Okay, so if you want to insert something you have to insert the specific position and then The value Okay, so you have to insert the specific position and the value Now, how do you do that? Um, there's actually some there are some, uh There are some Some, uh Hints that you could do examples that you could go look on the website But basically you would have to have the position of what your iterator is and then you have to Insert the specific value at that position So, um, let's actually not go over insert you just yet quite a moment because we should actually go over the iterator functions And these iterators actually give you the are actually positions Right there iterator positions and they actually helped you Um use these values, right? So let's actually go over iterators first because uh You is if if you don't know if you can't use an iterator then you can't really insert because the position is an iterator So let's actually go over iterator. So, um In c++ For vectors, uh iterators are just like, uh, it's kind of like a it's kind of like a pointer pointing to a specific location So there's always an iterator. Um There's the first iterator you could use is the beginning So it's just literally just something that it's an iterator that points to the beginning and then you could use it So when normally people do is that they actually create an iterator They start at the beginning and then while it's not equal to the end they increase it So, yeah, we're going to do that. So here what we could do instead of Using indexing from zero to the size. Well, if you want to print it using iterators. Here's what we would use um, so Normally you can use auto But uh for our sake purposes, we're not going to use that for now. We're actually going to use the actual uh standard template play a library of iterators. So we're going to do this Right, and let's actually call in the our iterator. Um, I don't know my It yeah, and it's going to what we're going to do is we're going to create our iterator and then we're going to point it to Um, the beginning Okay, so what this does is that it basically just creates a Object called iterator and it's going to point it to the beginning of our Our our vector, right? It's going to point it to the beginning of our vector And um, what we're this beginning function is just an iterator That basically for every vector, they always have something that you could get from the beginning Right, so now we're we're what we're doing is that we're just replacing this Instead of indexing we're going to use iterators. So if you go back to the example Uh, what you could do is uh, you could use a for loop And then you point your iterator to the beginning and then while it's not equal to the end of our vector Our vector dot end. We just Preincremented by it So let's actually take this Put it over here So this is going to create an iterator from the beginning and then while we're we're going to do is we're going to do it Is not equal to data dot end And then we're going to do plus plus i Uh, plus plus it, okay, so Um It is not equal to end. We're going to plus plus it. Um, I think my My ties soon. Yeah, it's okay. Uh now what we're going to do is we are going to Do something called Because an iterator class is basically a pointer of pointing to the beginning of the Pointing to the whatever position it is in the vector. Um, we you could actually you dereference this, right? So we're what we're going to do is dereference the it and then this is just going to dereferencing means that we Get the data that it's pointing to so that's what the star does So once we have the star it it's just going to get the data that's pointing to so it's just go It's just literally just going to print out all the values here Right, and then uh, we could do the same thing Just copy this and do the same thing after the pop-axe because literally just printing out the values And you can run it so we're going to run this And um, yeah, it should have exactly the same Output It's wait. Okay. Yeah. So five five one one one one zero and then five one one one. Okay So before the pop-back We had a zero at the end after pop-back We don't have it anymore. Okay. So that's basically the first part of iterator You have a function for in your vector basically it has a iterator pointing to the ending and it has one to the end And you could also increment the iterator and then every time you increment it you could print out whatever value it is Okay Now, uh, let's go over the next the next, um Next iterator, uh, let's go back Okay, so we have beginning and we have the end. Um, you could also print reverse um from backwards to forwards And uh, to do that you have something called our begin So let's actually go over our begin Um, so what this does is it's like a reverse iterator. You go from back backwards to forwards and um Yeah, you just You literally just go backwards to forwards. So If I want to print from backwards I'm going to do, um iterator to our Our begin. So we're just going to go from the end Uh Oh, yeah, it has to be called reverse iterator. So it's weird because it has to be called reverse iterator And then this is going to be, um Rn Is what this does now is just going to print backwards So, yeah, uh, it's going to increment the iterator and just going to print backwards. Uh, we could also do this Or begin our end It's going to be called reverse iterator Yep So now, um The first part is going to have the the last element is going to be zero all ones five And the second one after the pop back, it's going to be just all ones in the five So, yeah, here printing it backwards. We have zero all ones five After the pop back, it's going to be all ones and then five The reason why it's like this because we're printing backwards for both of them. Okay, so that's your reverse iterator All right, uh, let's go over the next function. So we're here today. We're going to go over all the functions So we add our begin our end. We have begin end Now we need, uh, let's look at c c begin. So this is just a constant iterator Um, what this does is that you can't really change it. I believe, uh Constant iterator points beginning of it. Yeah, okay. Um Yeah, so what this does is it just means that you can't change any of it. Um, I believe that To the first that points to a constant constant this iterator can be increased or decreased unless itself is constant So iterator term but it cannot be used to modify the contents. Okay So what this means is that uh, I can't actually use this iterator and change modify the array Indeed the vector modify the vector. I could only use this to print things. So, um The good part about iterators that you could actually change whatever Value you want it to be. So like if I want to change the beginning value Um, let's go back. Let's go back to here. That's actually created an iterator and point it to the begin value. So the first element, um Right, I could actually change this by dereferencing it The first element and I just change it to I don't know Seven or something right? So then now I remember, uh, we had five in the first value Right now then after this change, we could actually change it to seven. So, um Remember, uh, here actually that's not used seven. Uh, it's actually five. So Remember here in the in the initial part, I changed the first value to become five Now let's actually use it in iterator to change that So I'm going to actually take this, uh, it's and I just replace it So this is how you would change it. You create an iterator object You point to the beginning and then you just dereference it You get the data that's pointing to and you just set equal to five And that's basically the same thing as, uh, doing it before. So right now It's just going to be print out the same thing except we didn't use our brackets to change our value. We use iterators So if you look at this, it's going to be Wait for it. Yeah, so because we last time we were printing out backwards Yeah, uh, yeah, but anyway, we have zeros all ones and then five at the end because we're printing out backwards And then here we have all ones and then five at the end because we're printing out backwards But yeah, um, so yeah, that's that's the thing that you could do with iterators You could change the specific content at it that it's pointing to That's a good part about iterators. Um, if you want to change like, I don't know the fifth position of your iterator Which uh, or seventh position, I don't know What you would do is you would have your iterator point it to the beginning and you just add it by Whatever position you want it to be five plus seven that's going to change the seventh element to become five So, uh, let's actually not print backwards anymore because that's this is actually kind of confusing. So let's remove r begin and r and And let's instead of reverse iterator, we're just going to use regular iterator. It was too confusing. I don't like printing backwards Yeah, okay, so Here what we're gonna have is it's uh We have all values of ones except the seventh position is going to have a five. Yeah So here we have all values of ones and then one two three four five six seven Oh the eighth position my bad eighth position. It's gonna have a five Oh, actually, yeah, no the seventh position is going to have five but then because we added a zero There's it's the eighth position. Yeah, we have the seventh position is going to have five and then In the end we added a zero. So that's why it's there's a there's zero there. So yeah And then here the seventh position is going to have five. Let's see one two three four five six seven Actually, maybe it's eighth position. It might be the eighth position. Actually, let's actually get rid of the Pushing a zero back to the end. So let's actually get rid of I'll get rid of this stuff You guys don't understand push back and pop back already. So let's actually get rid of that Um, I think let's just see what this prints out I believe it should change the seventh position or it could be the eighth position. I'll see Okay, um Hold up Zero one two three four five six seven. Okay, so it's the index at the seventh position Becomes changes to five. Okay. Yeah. So what this does is it changes the index at the seventh position is going to become five That's what it does. Um, so it's the same thing I was doing like data at index at seven changes to five That's what That's uh, this is basically essentially the same thing as this Okay So that's that. Um, so, uh, that was like the whole Spiel of iterators, right? Um, I'm changing an element. So going back to constant iterators. These you can't change them at all So I can't actually do like a star iterator and change whatever position to become Five six or seven or negative. So let's actually use a constant iterator. So here we have, um I believe like a constant. Yeah here constant iterator And then we're going to call this c begin and then c end And it's just going to just print out the same elements. So Yeah, let's just Uh, yeah, so it's just the same thing. We're just one two three four five six one two three four five six zero one two three four five six seven Yeah, but just just prints out all the values. Yeah, that's what a constant iterator does. So this is pretty pretty cool It just means that like you can't change any of it. Uh, there's also a constant reverse iterator. Yeah so this means that you can't change any of the contents of your your vector but It's constant and it's the same thing. Um, so it just allows you to print backwards. So it's going to be like, uh constant underscore reverse iterator So it's uh, I think it's what do they call it? cr begin cr begin cr begin cr begin So this is just going to print out all the values backwards. Okay so, um Hold up hold up Let's run it. It's just going to print out all the values for backwards. So we're going to have a bunch of ones and then five Yeah, two ones and all fives One five and then all ones So that's what it does. Uh, so yeah, that means this constant So use this constant iterators if you're not actually going to change any specific value So that's a good part of it. Um, yeah, we we went over all these already. So we have our begin our end in okay, so went over all the iterators Okay, so now because we went over all the iterators. Let's actually use uh Let's actually use Yeah, okay. Um, that's actually you now do insert. Okay, so we have insert So now now that we talked about iterators, um If I want to insert at something at a specific position, you just have to Just uh, insert it at the iterators position plus A certain position. So, um, let's see Uh, let's see. Okay. So if we look at this template if I want to insert at specific position Um here what you could do is you could there's three, uh, let's see one two three four. Okay, there's There's three functions you functions you could use. Uh, oh wow now you could actually use more than that C plus plus 11 you could use more than that You know one two three four five. There's five functions you could use. Okay. Um Yeah, uh Okay, so the first one you could use that you insert is you insert at a certain position And then the certain value you want to insert at so Let's say, uh So like so here remember we had an iterator pointing at Data.begin plus seven right so this is at the seventh position We want to change it to five right now instead of changing the The position seventh position to five. Let's actually just insert a number At the third position or something so what this does it might it's gonna like Insert the element before at a certain position And then it's going to increase the size by How many elements are inserting so currently we have, um What this did with what currently what we have is like a vector with all ones And then we just change the seventh position to five. So if I'm going to insert a something, let's say I don't know Yeah, okay, let's say let's say I'm going to insert something at the Seventh position right I don't know nine What this does is it's going to in increase Insert whatever value before the seventh position so it's going to insert nine Before the seventh position. So now what we're what we're going to have is we're going to have 11 elements in our data vector So then when we print this out, let's not actually print reverse Because that's this is super annoying. Um, so what you could do in c++ new c++ if you don't want to specify The vector const iterate you could use auto So that's a good cool thing. So let's actually not print out Backwards so yeah here we're going to use const iterator and we're just not going to print out backwards So what this does is it's going to insert at the insert nine at right before the seventh position So now if we go back here And we print out and we print out run it, um Here right so we initially we have one two three four five six seven Zero one, okay, we have it's indexed about zero. So we have zero one two three four five six seven So it inserted nine right at the Before the seventh position. So the seventh position originally was five, right? See this five, but then it inserted a nine before it Right, so remember in our previous code above here, we changed the seventh position That value five. So now what here it does is it's going to insert the value nine Right before the seventh position. So data dot begin plus seven. So remember data dot begin is just the starting starting position of our vector data. So we plus seven. That's the seventh position indexed Okay, so now we have this part Uh, my bad, okay, so we have insert You could also insert a certain number of elements. That's a we could go over that also So we could actually fill up Let's say I want to fill up like five I'm a three nines, okay So what I could do is I could insert three nines and then it's going to insert three nines before the seventh position So if we run it again Boom, now we have three nines. That's inserted before the seventh position So in our vector Three nines will be inserted before the seventh position. So that's this section second function of fill Um Okay, you also could do a range So, uh, so what this does is it's going to um Let's see, uh, you could fill out a range and it's going to copy the The specific range of elements in the range between first and last At your other positions and it's going to insert that into your current uh current Current, um array of your position that you want to insert So it this is like only useful this third function is only useful if you want to copy some other element from another Um, another I don't know another array. So, uh, another vector So here what we could do is if you want to do like a copy data I'd say I have like, I don't know Sixes right Say I have I don't know Three three sixes, okay So here the first part remember the first part is the size and then the second part is the Number that you want to insert as so we have copy data Excuse me Copy data is going to have Three sixes. That's what's going to have three sixes. Okay Now, um, what we're going to do is we are going to basically just point Um, those three sixes Of copy data and we're going to replace that insert it at Our old value of data So here if I want to I don't know, um Insert data dot insert, uh What we're going to do is we're going to uh, let's say we insert at the seventh position, right data insert We're going to insert at the seventh position and we are going to basically just copy all the values of our Our copy data vector our new vector of copy data of sixes. So copy data end So what you do here is you pass in the, um, you input the the, uh You input the the to begin and the end Yeah, so you input the begin and the end, which is like these are the positions of your old Uh, your old Vector that you want to copy the the contents of the old the contents of the old values you want to copy into your Into the new ones. So here the old for contents when copy is the three sixes We're going to copy into Our the seventh position of our data array. So in here, it's just going to have three six is going to be copied at the seventh position So now we have three nines and then three sixes At the seventh position. So it's going to look kind of weird But uh, yeah So now we should have Yeah, so here we have all ones, right Um, the seventh position had a five, but then we inserted three nines before it and then after that we copied Six, uh, we copied the three sixes from our Other vector of copy data into our initial vector of data. So that's why we have three sixes before the three nines Okay, so that's what that does. Um, I think that's all of that Uh, should we go over the new ones? Okay. So now here you have constant iterators. So it's basically just This is this These new functions are just the same thing. Uh, so the two new two other new functions Um, yeah, it's just literally the same thing except this is constant. So you could pass in like a constant iterator So that a position that you don't want to change and then here you got a code also pass an initializer list. So um And we could do that. Okay. So let's say I wanted to pass in like a list of elements So Yeah, I'm going to get rid of this copy data And I'm going to get rid of this also, uh, inserting the three nines So let's say I want to insert like, uh A list of elements At the seventh position. So here remember we changed the seventh position to have five But now I'm going to insert A certain number of a new list into the right before the seventh position So to do that, I'm going to do data.insert And then I'm going to do Data.c begin which is like a constant position a constant iterator constant iterator position Which is this which this points to the first Considered to the first element. So that's what it does. And then um, I could just pass in a list Of values I want to insert before the seventh position. So I could do like a 123456 And then it's going to insert 123456 before the seventh position And uh, yeah, let's see what it prints out Yeah, so here. Oh, wow. It's actually kind of strange Uh, oh, no. Yeah. Oh, what's my bad? I didn't I didn't do it at the seventh position I insert at the beginning So the data.c begin is a constant iterator point to the bayonet. Let's actually insert at the seventh position. So plus seven So now what it should do is it should insert 123456 at the seventh right before the seventh position And you have a bunch of ones Then we right before the seventh position of That has a very five inserts 123456 And then the seventh position that we changed to have five Is still there. So that's five. Okay, so that's what it does So yeah, that's inserting a list of elements. So that's this fourth function fifth function. So we went over all the inserts. Um There's also more inserts you could go over Uh, no, actually, I think that that's it. Okay. So you could do the exact same thing with with erase also um I don't want to like go over that part also, uh, oh wait, there's actually fewer Fewer Yeah, there's actually fewer things you could do. Okay. So what you could do is you could erase at a constant iterator position So let's say I want to erase the seventh element So here we change the seven seventh element to have five right this iterator and let's say I just want to remove that So let's actually go here. What we're going to do is we're going to do data erase and what we're going to do is we're currently use Data dot c begin Right to consider it and let's remove the seventh position And then what this does is it's going to change. So here we change the seventh position to have five Now we just removed it. So now it's just going to have all ones It should have only be nine elements So Let's see One two three four five six seven eight nine. Yeah nine elements and they just have all ones So that's what the erase does. That's this function of erase. We just pass in a position. You want to root Race um, you also could do remove a certain like two positions of your starting or last But it's not including the last Right. So we have first and last You could pass in the two positions that you want to remove The range that you want to remove but it doesn't it's not including the last so let's say I want to remove I don't know the second position up to Up to the fifth position but not including the fifth position. So what this does is it's going to remove all all the Elements between two to four. It's not going to remove the fifth position So this is going to remove all the elements at the position of two to four and not Going to remove the fifth So here what we're going to have is we're going to have uh, let's actually see what it does It's just going to remove all the ones so Okay, so initially we changed the seventh position at five. So we have one two three four five Right, but then now we removed all the values. Let's actually print before the remove Okay, so we have here Before the race Here we're going to print out after the race after the race So we have before the race and after the race Um, so before the race we have one uh one all ones in the seventh position We zero one two three four five six seven Uh had five right and then After your race it removed the values between two and four. So zero one two three four so these Three ones were gone So then all we have left is one one one one five one one so one one one five one one So yeah, that's what it does Um Yeah, so that's what it does. That's what this race does Let's see. Do I have how much better do I have left? Oh, yeah 10 minutes racing. Okay. Um, yeah, uh, you also could swap So It exchanges the contents of x with another content sizes may differ. Um So what this does is it swaps the Well, actually wait wait. What does this have? Oh, okay. Okay, so It just swaps the two two contents. So like let's say I have Okay, let's actually use this example. Let's say I have, uh, three 100 value uh three three values of 100 and then five values of 200 if I just swap them then What we're gonna have is five values of 200 and then three values of 100 so Let's just actually use this example So let's create both of these Let's copy both of these. So let's get rid of these. Let's get rid of all this So here we have, um Yeah, grab my battery's low uh Yeah, okay. So here we have three 100s and five 200s and if I were just to print out both of them Um, uh, yeah, this is a way to print out print out Way faster way to print out values, uh, so we have foo and then we have, uh bar Right. So here we're going to print out foo R These are two elements these two vectors. So we have one that has three 100s and the second one has Five 200s, right? So you have three 100s So one two three 100s, right? And the second one is has five 200s So now if we were to do foo dot swap bar and we So now if you were to do foo dot swap bar And then we reprint all the values again You should be able to have uh, it should just be swapped So now, um Yeah, okay So Let's actually print out new lines Because this is too crowded Okay, so Yeah, so before foo had Three 100s and bar had five 200s now that we swapped both of the contents foo has Five 200s and bar has three 100s. So basically the two contents of the vectors were swapped So that's what this swap function does So that's that. Um, I think we went over Okay, so we did erase we did insert we did swap. Uh, you could clear. Okay. So here to actually just clear removes all elements From the vector leaving with the vector with the size zero So Let's actually go back to foo and bar. So here we print out a foo Let's actually get rid of bar. So I'm gonna hear get rid of bar Okay, so Let's so here we have right now we have three 100s, right? Three 100s in there a vector foo Now let's do foo.clear This is just going to remove all values of the in our vector foo. So now it's just going to be all have nothing So here we have three 100s, right in foo. Now it's just all of nothing. So that's what clear does It just completely removes everything Okay, um Yeah, okay, now let's look at, um Construct an insert. Okay, so we could do emplace Um, so this is a way that inserts the new element at the position. Um, so it's it's not the same thing as in place is different than insert in that, uh in place actually Inserts it at the position. It doesn't insert it before it So that's the thing. Um Yeah, so let's actually use their example here. Um, if I have like 20 30 So let's create a vector. Let's call it data Okay, so, uh, I forgot to mention this if you could also also initialize your vectors In a list format. So we have like we could do 10 20 30 So what you would use is you would use these, uh braces kind of like how we use these braces Normally and you just you set your data equal to this Uh braces and then whatever element you want it to be so that initializes it to 10 20 30 So now let's say I want to insert it at the first position 100 I'm going to do a emplace so Here's what I'll do data dot in place And then let's do data dot Again plus first position and then I insert 100 So here let's actually do a It's actually print all the values of data so Okay, so we're going to print all the values before and then print all the values after So here's before printing After printing, okay, so now if we were to print it um Yeah, okay, so here Before we had 10 20 30 Now, uh, after we do an emplace we insert it at the first position. So here the first position was, um Uh zero one Is the first position at the index to zero one is 20, right? So then what it does what it did was it actually Uh, it actually inserted at the position of zero one So here at 20, it's uh, it just inserted 100 at 20. So, uh, yeah Right at this position of 20. So then it has 10 120 30. So that's what it does. Um I believe Yeah, so it inserts a new element at the position this element is constructed in place using ours Okay, so it's different than the insert in places different than insert in that insert, uh Inserts new elements before the position. Yeah, so that's the difference In place is different because it inserts it at the position a new element at the position All right. Um, there's also in place back So this inserts a new element at the end of the reactor right after its current last element um Yeah, this is just going to I think it's the same thing as pushback I think so Oh, but you could have had some arguments Okay, so that's a good thing. Um, I think in pushback you can't do that. Let's see Yeah, okay, so pushback you only could add one element at a time in place back you could actually Add multiple so, uh, let's actually go to our vector So remember you could let's get rid of uh, actually, let's keep this. Um, So we have 10 20 30 and then now what we're going to do is we're going to do in place back So let's say I want to add 40 at the end. We do in place back 40, right? If we print it, we should have 10 20 30 40 Yeah, so inserted 40 at the end So what you also could do is you could do this Insert multiple values good to enter 40 50 60 70 so this is the difference then pushback because oh wait wait Well, you can't do it Well, it says you could Arcs I guess you can't That's weird Okay, I think it's the same thing as pushback Insert element at the end inserts a new element right right after its current Last position this is going to show you in place using ours. Okay, so I guess you can't Um, that's kind of weird. Oh, okay Okay, um, yeah, so you can't you can't actually do this my fault. Um So it's basically like pushback the only difference between so you only could in place back you only could insert like One element at a time you can't just insert all them but um My fault. Uh, basically it's the same thing as pushback, but pushback copies or moves an existing element into the container and place back just does Reallocation I think Uh, I don't think it copies anything. I don't know Uh, let's actually look up the difference between between pushback and place back I'm sure there's like some time differences Okay, so Okay, so you could actually make oh you could take a Yeah, so here you could definitely pass arguments. That's what I thought Place back. Okay, so Yeah differences that one you one copies the other one just One of the one actually moves it So call in place back a our car call pushback just both of them. Okay, so yeah um, that's the difference So I think in place back might be faster actually Okay, so we finished all of this, uh Let's actually look at this sign and I've actually looked at this before Okay, so Oh, this is a weird function to never look at this before you can actually assign values um At an iterator. Okay, so here you could You could actually assign your input copy that you want to copy values to and just assign them into your new New uh vector um So it actually replaces its current contents and modifies its size. Okay, so like That's kind of cool. Um, so so let's let's actually do that. Uh, just go to data dot assign And let's actually let's go over. Let's actually use all these functions. Okay, so the first one is the Copies the first position the last position of another vector. So we could actually do that so we could do in copy data Let's create like 40 50 So let's actually assigned Uh, what do I want to do? Yeah, let's just see what it does. Okay, uh We're gonna assign copy data of 40 the values in copy data from the first beginning to the end And it's just gonna sign it to our data vector So it should give us uh, let's actually see what it gives us I'm gonna kind of oh wow, which actually replaces it. So Yeah, I just replaced it with 40 and 50 Oh, that's weird. So yeah, this just copies the values over That from your older rates to your new one Oh, that's cool. Um Yeah, oh, yeah, you could also assign like a new list. I guess so let's say So that was this first function just passing in like the two positions iterator positions that you wanted to pass From your old vector to your new one You also could pass in the size You could fill this up. So like Uh, that's okay. Let's actually do before and then after um Okay, so we have before after I just assign it with all values like let's say Five values and then have all 100s or something So this new vector should just have five values and all values 100 Um, yeah, so we initially have 10 20 30 afterwards. It's just five values all values 100. So that's what it does. Um You also assign a list that's cool. Um, okay, so here what we're gonna do I'd say I want to sign like a list of values. I could do this Put a list And do like one two three four five And then what this does is before it had 10 20 30 now it has one two three four five So the function data dot assign So 10 20 30 now it's one two three four five. So that's that's cool. I think that was that's it. Yeah, so we the first one first Function assign just passes in Your two positions at your old array that you want to copy to your new one The second one just once you replace The size of the number of elements you want to put and then replace it with like the new bad contents you want it to have And the third way you do it is just assign it with like a list of elements that you want to change it to So that's what this vector assign does. So now we have went over assign push back pop back insert Ray swap clear and place and place back Um, I think we went over most of them. Okay, uh Is your Constructor, okay. So, okay, uh, we could go over all the constructors I think we actually went over that Basically, you could actually just um So, yeah, let's actually go over all the constructors Uh Of our vector. So by default if we just created our vector before Of data, right? So let's actually I'll I'll delete everything so Let's say I have data and I put nothing it's just going to initialize data with like, uh The default constructor is just going to have nothing in here, right? Um, that's this first thing. Uh, the second one is if it's, uh I want to fill it in with certain values with the size size of n Or with uh, some allocator um So that's the one that when we went over like if I want to fill this Vector like five with like 100s, right? So now I would have five elements It's all values of 100. So that's the second one. Um, You could have to const type. Okay. Uh, you okay. So here one you could actually pass in the range of your old old um old Old vector that you want to copy over. So let's say I have like Old Let's have one two three four five one two three four And then I want to copy I don't know this previous the next into it. So here what I'm going to do is I'm going to do like old dot begin old dot end And then if I were to print out my data it should have one two three four, okay Uh, let's just wait. This is wait. Let's wait Yeah, so now it just has like one two three four. So I just copied All the positions of your old vector begin and end Copies into your new value of data. You also could do like, I don't know if you want to start at like a fifth position I mean the first position The end so you're skipping The one so I have two three four Yeah, you could do that also. So there's just positions of your old So you now you have two three four. These are just like positions of your old Olds after you want to copy over so we have that. Um, I think that's it. So here we have so that was this Um, okay, so that's this one the range Uh, okay, you could also just copy You literally could just copy the old vector. So here here if I just want to copy the old vector I just passing the old vector and it's just copies it to have One two three four So now my data is just gonna have one two three four. Yeah So you can do that also just passing the old vector. Um Cons allocator type, you could actually explicitly put the allocator type. That's cool Uh, yeah, okay, so here you could actually pass in like the size of how many you want to copy Uh, I don't want to go over that. I don't see like what's the point of that? Um This is just a bit. Oh, you could actually move Oh, no, okay. So we have copy just passing it. Uh, here you could also just pass on like the type you want to put Uh, that's pretty much just a bit. Oh, you could also like just copy like a list So like if I want to do like Data I have like, I don't know one two three four five six seven Seven eight nine ten Right and now the data is just gonna have seven eight nine ten It has put in a list format Seven eight nine ten. Yeah, that's cool. You could do that. Oh, yeah, that's pretty much just a bit. It's all the constructors Okay, so we went over constructors Um Equal sign we could use we could talk about the equal sign. This just it's just literally just copies all the values into the equal side Um, yeah, so like if I have like Old values of one two three four, you know and my data has like seven eight nine ten and I want to copy data I want to just copy all the old values the new ones I would do is that I create my new one. I just set it equal to it now when I print it out So here, let's see before copy With an equal sign, let's do After copy So here we have one two three four Oh, it's seven eight nine. So we have seven eight nine ten And then after copy has one two three four. So if you really want to copy easy way just use equal sign Um, yeah, so that's that uh, I think that's it All right, so we went over equals a destructor doesn't really matter um Yeah, you can't really if you really want to destroy it you can But This calls the destroyer. Okay. I mean if you really want to Uh, that it's just like a way of just clearing everything Of data. So let's say I want to instead of copy Let me get rid of before after I just want to kill it. So like clear everything. I just do this calling data I think that's what did Oh, yeah, okay kill that data and put this I can't even call it. Yeah I don't even think you could use this Destroyer on each of the delegates. I don't even think they allow you to do this. Oh, okay. No. Yeah, I don't even think you can't Wait, oh, okay Yeah, they don't even let you call it Okay, whatever. It's basically just like clearing. I you can't use it for some reason Actually, can you okay? Let's actually look that up Calling destructor No, yeah, you can't you can't even do it. It's better just to like use clear so Yeah, okay, so yeah, I don't know. I don't know why that's there. All right. So one over constructor equals begin and or begin all the iterators Okay, um went over all the modifiers Okay, we can do capacity that's fine. Okay. Uh, remember we did dot size that gets the size of it Uh, we could also do max underscore size, which literally just returns the max size I don't even know that because the max size is like In the compile excuse me in the compiler So, um, we have before We're gonna print out old dot That's just okay. Let's just have Data let's just print out data dot underscore size. Let's see what this does. I don't even know what this is So the maximum size We have seven eight nine ten maximum size might be uh Oh, wow. Okay. It's like a Maximum size you have in your vector could be like 10 million or something. That's good to know Okay. Yeah. That just prints it out. Um You don't even need it technically Oh, you can resize it That's cool. Um Yeah, okay, so let's say I have like an empty vector And then I have like values of one to ten Uh, I could resize it to have five So let's actually do that. Okay, so let's say I have this vector Data and I'm just gonna Do this push back data So now I have values of one to One to ten Now if I want to resize it Let's do resize five and then I print out it should have um Only only five values you know, so Yeah, so here before I had One to ten now it has only one to five. So this just resizes just shrinks the vector to the first few elements um Yeah, uh, you also could Yeah, so that if it and it's smaller than the current size it reduces it to the first and elements So removes the ones by element and is greater than it it expands it by inserting as many elements reach to size Okay, so Okay, if it's greater than capacity reallocates. Okay, so Um, yeah, so like let's say I want to resize it to like So here initially we had 10 elements of one two three five six seventy nine ten So we can actually print that so here we have before and here we have after resize Okay, so let's say Let's say it was greater than 10. Let's say we have 20. Let's say we'll reach to a size 20 elements have 60 now So what this does is it's going to keep adding 60 to the end until we reach 20 elements see So initially add One to 10 elements before after I resize it to 20 It's going to add keep adding 60 to the end of the The uh the vector until I reach 20 elements. So that's what it does So yeah, that's what this the first value is the The size that you want to resize to the second value is the value you want to keep adding it to be So yeah, so if your value is greater than it, it's going to just keep adding So you reach the size Okay, so we had resize capacity Is that the same thing as dot size? Yeah, so this is like an internal storage type of thing Okay, uh So after 10, let's actually print out the data capacity. I don't know what this is actually So I push back 10 elements Outsides would give me 10. Let's look at capacity. Okay, so it's 13. So it's just like a bit of over the Dot size the number of actual elements So yeah, um Yeah, you probably never need this. It's it's not the actual like real size. Um Okay, uh, you could do empty which checks if it's empty or not All right, this is good I want to see like if it's empty. So if I want to print out, uh So if I say if data dot if it's empty Then I say yes empty Otherwise Okay, so here we have Uh Yeah, it should be no not. Yeah, it's not empty Not empty. Okay. So what so here we had 10 elements added into the vector data Uh, then we just checked if it's empty Then we say yes empty. Otherwise not empty So because it already has 10 elements that we push back. It's definitely not empty. So So that's why I print out not empty. So this function empty just checks if it's actually empty or not Okay, um Is that all the fun guys Okay, uh reserve you don't want to use this actually Okay, so reserve is just saying that hey Give me make sure that I have 10 like you could fit A certain number of elements in this capacity So like in our vector of data If I do like data dot reserve 100 this just means that hey make sure I could put 100 Elements in the data. It's kind of the same thing as doing this except it's not assigning zeros It's 100 elements So yeah, that's what reserve does um Did we go over Shrink to fit. Okay. I think that's the same thing as resize Okay, so this just reduces the capacity to fit size. Um Ideally you don't want to use this Okay, so Okay, that's super weird. Um Yeah, okay, you I don't know Um Yeah, okay, so this just changes the capacity Okay, so Let's just type this up. Let's actually copy this So here what they had was they initially have a vector And they initialize it with 100 elements, right? So then it has 100 elements and they're all zeros So the capacity they print out is just going to be the capacity of how many which is like the It's not the actual size. It's like just a little bit greater than the actual size dot size function So it's like in a vector they Under the HUD they under the HUD they want to make sure it's not like You don't get too over bounds out of bounds, right? So they just have a little bit Their capacity is just a little bit above the size so here If we print it out, let's see what happens Okay, so here the capacity of our vector is 100, right because we initialize to Our the size of our vector is going to have 100 So now what they do is they resize the 10 But the capacity is still at 100, right because Um resize just changes of the size to be 10 elements, right, but it doesn't change like the actual capacity What's under the hood like the storage that you can actually have in your vector So like what it's saying is like hey, I resize the 10 elements But the capacity of my vector is still 100, right? So it just means like hey Because you have 90 more you could use right Now if you do shrink to fit it's just going to change the capacity variable to now have Only 10 so that's what the shrink to fit does It actually shrinks the capacity of a vector to be have The after resizing So that's pretty good if you want to use capacity. I generally just use size so I don't really care um Okay, so we went over construction destruction equals went over iterators um whenever capacity Uh Element axis, okay, so yeah, we could do that. Um, yeah, okay That's super weird. Okay All right, so you know how to access an uh a vector already Like let's say I have vector data one two three four Um, so if I want to get something out of position I use these brackets, right Zero and I just set it to like Three, uh, I don't know 100 so what this does is it's just going to Let me just print out Set the first elements, but you have 100, right? So the first element is 100 and the rest is 2v4 now. Um, yeah So the brackets the operator brackets just allows you to access whatever at the current position So the current index so here at the zeroth index zero So vectors are the same as raised It and it starts at like index zero All right, so the first position is zero But um, yeah, so at the position zero it has changes 100. So that's a brackets You could you instead of using the brackets operator. You could also use uh at This is super weird. I don't know why people would do this, but If you really want to you could do dot at At certain position equals it. So it just returns a reference to the element At a certain position, um Yeah, uh, can I actually change it returns element? Yeah, okay, so just access it So I could do like date instead of doing at zero I could do dot at Zero So equal to 100 it should do the same thing if I don't want to use the brackets Yeah, that's the same thing 100 200. So yeah, that's what the at is. Um There's also two functions called front and back So if I push back uh dot front is just uh So unlike the actual It's basically the same thing as begin vector dot begin But the difference is that begin is an iterator This one actually returns a reference. So like I could do like I could do data dot front And then like minus 100 So I don't have to do like the star pointer thing Right where it's pointing at so this is going to take one minus 100. So it's negative 99 So yeah, I could actually legit change that um And I think that concludes it and there's also back So it's return of reference. Yeah, this returns the direct reference. So I could do back and it actually just minus equals 100 will remove subtract four minus 100 And minus 96. So yeah, that's what it does But yeah, um Minus back Uh, there's also data. Oh, what's so so weird. Oh what? That's pretty cool. Okay. Um So vectors are actually have Are implemented using an array also so You could actually get the point the date the array reference if you really want to So here Data is one two three four, right? Uh, I could do Okay, let's actually change it to my vector not Be super crazy. I could do so what I could do is I could do my vector dot data And this is going to get the array form. So I could do like my array And then I could actually just give it to the equal to this. Uh I have to be a pointer Damn, so, okay, so I could do this. Yeah, so this gives me the Okay, so this actually gives me the sorry, there's people outside screening. There's actually people This actually gives me the vector form of the The array form so it converts the vector to an array form Does a pointer getting so here I could actually Yeah, I don't want to okay, uh, so what I could do is I could go through zero to four and just print out my array And then I could do Uh, yeah, I could actually just print out And it's just gonna print out one two zero zero, uh, one two three four, but this is an array form now So it adds a pointer to the array I should print out. Yeah, one two three four. So that's pretty crazy. That's pretty cool So, yeah, um, I think those that's that's all we have for Everything you need to know about vectors. So those are all the functions. Um, I hope you guys enjoyed this video Recom subscribe I'll check you guys later. Peace out You