 What's up guys, my name is Michael and welcome to my YouTube channel today We are going to go over maps because we went over sets last time and I didn't have I just didn't have that much time this This week in order to go over things but now I do so today. We're just gonna go over maps So this is like the website for the maps basically maps are you used to? Since we're using regular maps now, so they're sorted and they're sorted you're basically mapping a key with a value okay, so if you have like a Funny get a certain value and a key We're gonna map if we have like two relations between two either objects or numbers variables or primitives or different types Any of two relationships right and we want to just map one to another, you know, like let's say I had like I don't know a Bunch of car names and I want to map the car names with the license plate, right? Then I would just you know for every car name. I'm gonna have map it with a unique license plate So that's basically what maps are basically Maps store elements by a combination of a key and a value pair So in a map the keys are sorted uniquely so If you you if you insert things into a map, it's sorted by the key Okay, so like if I had like I don't know I'm Adding like a car name like Toyota and then like I don't know with this license plate and then I had like Tesla right Because T Tesla TE is Before Toyota TO The Tesla would go first because it's sorted by alphabetical order sorted by the key All right, that's what maps are While the map values store the content associated with the key the types of key and map may differ So the that's just saying like you could have different types You go to you could to have a string and you map with an object You could have an object map it with like integer you have a I don't know I have a double or float or something and you map it with another value they may differ They don't have to be the same type Okay, so that's what they're saying. All right internally. They are sorted by its key in weak order Criteria, so that's basically saying Increasing order. That's what they are Map containers are generally slower than unordered map to access it by their key because they allowed But they allow direct iteration on subsets on their order So map iterations because it's sorted all the time It's a little slower than the unordered map to get values because it's always sorted by the key The map values in a map can be do access directly by the corresponding key using the bracket operator. So using the bracket operator Okay, and they are also implemented by using binary search trees. So in a map if you remember binary search trees Maps are generally implemented using binary search trees on to get values to get values and to You know retrieve values or remove values. They're implemented as binary search trees. Okay, so here are some properties Associative the reference by their key and not by their absolute position So if I want to get a certain value in a map It's referenced by the key. So it's not like I need to get like third position or like the fifth position right in an array or like in a In a link list or something like I have to get like fifth position from the head Like that's not what a map is maps are referenced by their key and not by a position in the container Okay, so they're ordered. So they're they have to follow strict order all the time so that means that if you insert the elements in this order, it's going to be sorted and It's strictly by this order. Okay Each element associates a key to a map value Keys are meant to identify the values whose main content is the map value. So the keys are meant to identify the elements. Okay, so if I want to get some value I have to get the key like when I insert Some new like a string or like a like a license plate with a car name, right? I'm using the license plate as a key Then I have to use a license plate to get the car name I can't use the car name to get the license plate. Do you know what I mean? Your key is meant to identify the elements not the value Okay, so you have to use the key to identify the elements whose content is the value Okay, you need keys of no two elements can have the same key. So that what that means is like I can't have like The same key for two elements So like let's say I have like a license plate and I'm mapping it with the car name, right? Like I have a Toyota license plate. I'm having it with a car name. I can't have another Of the same license plate going to two different car just names Hope you guys know what I mean. So like I can't have like Let's say I like one two three four five and I map that as a as the first Toyota car name, right? I can't have one two three four five again as a key To map it to another thing. It'll just replace it, right? So no two elements in container can have the same key Um, it's allocator aware so uses an allocated object to handle its storage needs, okay All right, so this is just saying Okay, so this compare this in is just saying that a I need to return a Sorted by increasing order if you want to change the comparator you could probably pass it in So I think it's less than operator and then it would actually change the order for you So you know, then it's increase its largest instead of smallest first if you want to do that, okay? So now let's actually go over every single method and it's not that big of a deal So let's go over the first constructor Okay, so the first there's four types five types of constructors if you can all right first one Is just like a regular old constructor. So just empty with no elements. So if I want to do Map and let's say I want to do like string the integer, right? And I just call it my map This is the first constructor. All right, so we have the map Which is the the type that we want we didn't want to create a map We have the brackets let's integrate a sign and then here we have the the key the key type and then the value type So we have the key type here on the first is the key type and the value type. So this is like Like mapping a string to an integer, but I can map like I don't know If I have an integer to a string See you can do that or you can do like you can map like Boolean to a string like it doesn't matter at the first first value is going to be your key and I'm going to do a string to integer because I Think it's better. Actually, maybe you should do integer to string so car car license plate to Car name. Yeah, let's do car license plate to car See and then let's just call this map car Car, I don't know. Yeah, so we're mapping a car license plate, which is like a bunch of which is a number And then we're mapping it to a car name That's what we're doing. So that's the first empty constructor. Okay All right. Now you could also do You can initialize a container with as many values if you want so the first one this map is has just has nothing in it Right, it's just map car. It's just the empty now. Let's say we want to do Want to pass in a range? Okay, so let's say I want to pass in a range So what I could do is I could if I want to create a constructive the range I could pass in the the First out values and the last value in the range of a previous map So this is like copying values from the previous map into our new map Okay, so if I want to do that, I could do this first. Let's actually create values For a map so let's say if I want to map Certain value to another one. We use the bracket operator So we use brackets right here and then let's say I wanted a map one two three So this is a license plate and I'm gonna map it. I don't know Toyota Toyota Okay, so that's about it's one license plate, right? And let's say I have like another license plate 234 or something and I map it to like Tesla and So on and so forth right you can map whatever value one so like this one two three two three four so like I said before you cannot have Two two values with the same key so I cannot do map 234 car 231 23 again like I cannot do that I cannot do that. I cannot do this This is not possible. You don't want to do this because what's gonna happen The map is just gonna replace the old Toyota with the Tesla. So you cannot have the same key key types Okay, so back to the constructor we could have passed a copy constructor passing an arrange So this means is that I could initialize my new map Like another map by passing in the range of elements. So I could do like a map Copy map. Let's call it copy map What I could do is I could actually pass in the range of the original value. So from beginning to map cars end What this does is just gonna copy all the values from the beginning to the end of our old map of Map car into the new map of copy map and that's what it will initialize our values for so that's the range constructor Okay, another thing you can use is a copy constructor and that's just You just copy the whole thing. So what that means is that if I don't want to pass in a range of The values of I want from beginning to end. I could just copy the whole thing So if I don't want to do this, I could just pass in map car And we're just copy all the values of my old map a map car into my new map of copy map So that's what you could do. All right. Oh, let's see. What is the third fourth? Okay, the move one which is just like moving from one construct to another you can do this but Yeah, I'm just not I Don't I don't know I don't know why anyone would you do this but uh? Yeah, you could do this Basically, you would just pass in I Think it's the same thing actually think it's the same thing. Oh, okay. Well, yeah, it is the same thing Yeah Moving with it creates that requires elements of it. Alloc is specified as different actually, okay So if you have to pass you have to show alloc. So what that is means is that? You just have to do Um, you just have to show make sure alloc is specified. So to do that Let's see, I actually don't know how to do this Hold up. Um, let's see. I've actually seen this before map alloc alloc Example I want to see this example. I haven't actually seen this before Moving with an allocator. Um, yeah, I don't know Initialized list did it did it did it did it did that move? Um, do we specify true? I don't know. I have to search this up allocator type Are you in this is a typical class of the allocator the argument to the contrary instance of that type Yeah, there's a way to do it, but I don't see a reason why you want to move it Well, let's just skip this constructor because like I don't see if you want to move something might as well just I Don't know. There's a way to do it, but you have to specify alloc The fifth one is just initializer list and I think to do that Constructs a container or the copy of elements and initialize this so for that you just need to do Class comp. Okay, so it's like this weird Let's see initialize list. I think it's I think it's the same thing initializer Okay, yeah, so here's what you do. You just have If you want to initialize The map car differently You'll just use brackets. So instead of doing one two three and then Toyota and two three four Tesla If I don't want to use the brackets here. I just want to put them here in here. It's very easy We just put more brackets here. So we would do Yeah, we would do this. Oh, we don't need an equal sign. We'll put more braces one two three and then Toyota and then two three four and then Tesla and I'll get our thing right there. And if you want to check this we could just run this and see what it gives us Ideally it should give us the same thing as what we had before But this time we just pair it up, you know, so we're pairing up Using an initializer list so we don't have to you know do the thing We don't have to explicitly type out the values. So yeah, you see one two three is Toyota now map to Toyota two three four maps of Tesla Yeah, that's how you initialize it using a list. So yeah So that's that Let's go to the next one Okay, so we have a constructor destructors The this thing is the inside implementation against called when you do delete. You don't have to worry about that. Oh the equals Okay, so if you use equals it just replaces the contents. So like if I have a I don't know second map Just cop. Let's just copy the same thing We're gonna copy this and put it in here and call the second map And this time we have like two three four Toyota and then one two three Tesla Let's just switch these values, right? And if I do second map Equals map car It's gonna replace all the values of Map car and put into second map. Okay, so like second map is gonna become map car And if you want don't believe me We look look inside here And if we go next Something wrong. Okay. I need to print something out I don't know let's print high and then open this Okay, okay, okay second map maps the map car and then now what happens is now second map See before second map had two three four goes to Toyota now It's one two three goes to Toyota and that's because we replace all the values of second map with map car using the equal sign So that's what we did Okay, so that's equal sign Okay, we could iterate from begin to end our begin our end see begin see end Or see begin see our end Okay, so if you want to loop through from beginning to end you could use something called iterator if I want to do that It's pretty easy I'm gonna use auto because Actually Yeah, I'll see I'll use auto because I don't want it. I'm too lazy. So I'll suit it Yes, so you do you do map dot begin and it is not equal to map That car let's I'll get rid of the second map Let's use map car now because we don't need to take one not equal to map car and It was plus Right. So this is basically creating looping through using dot begin dot end Basically what we're doing is we're creating iterator and we're starting from beginning and then While it's not equal to the end. We're just gonna keep increasing it. So that's what the begin and end does So this is just looping from the beginning to the end and what we could do is we could also just print out Map car dot first Star car Dot first. Oh what let's see how to print out using Curator and I Think you're gonna need to use. Yeah, you have to do it this way. It's really weird Yeah, I have to specify this iterator So yeah, I can't use auto for some reason. So just do I Can't use const It has to just be iterator Okay, here and then this is this integer and then goes to parent to string Okay, so yeah, this is what you do you grant iterator call it It has to be the same type as your original map and then You say equal to the beginning of your map maps the maps name that begin and while it's not equal to the maps name And we're gonna iterate a plus plus and what we're gonna do is we're just gonna print out the first value And the second one So this is gonna print out this dot first this arrow first is the key and this it second is the value So in this case our key is the integer, which is a license plate Number and then the the value of its second is the name of the car like Toyota car name So if we print this out, it should give us one two three Toyota and the two three four Tesla See one two three Toyota two three four Tesla. So that's what it does Yeah, so that's iterating from beginning to end I Think there's there's more things we had to go over when over constructors Okay, so that's begin to end up. You could go reverse way by our begin. So if you want to do that, just do our begin our end And you have to instead of this iterator of you reverse iterator I think Yeah, and then it would just loop through backwards. So instead of one two three Toyota. It'll be two three four Tesla Two three four Tesla and I want to do Toyota because it's looping backwards Okay, and then see begin is just a constant iterator It's about it's basically the same thing and then CR begin is the same thing I it's if you don't want to change any of the values, then you use a constant iterator Okay, empty just checks if it's empty. So if I want to check if this map is empty I just do check my map car, which is like the maps name empty. So if it's empty, I'll just print out empty It's empty Otherwise, it's not empty Okay, so in this case It should print out and not empty Because it's actually not empty. Yeah, so yeah, that's what the empty does Okay, size just tells you the size You just print it out Use your map name dot size and then just print out the size so we could do like this size of my map is And It should tell you the size and it should be the number of keys. I believe Should be two. Yeah, so the size of my map is two because it's the number of keys see one and two Okay, that size a max size just like the maximum possible size you can have for your map Typically, this is like something that the code like deep down underneath the code what it would do Yeah, it's just like a million. That's the maximum maximum size of your map to do I Think after you get that past you just like, I don't know Just get bad There was an exception. I think all right. How do I access a map? You use your key. So if you want to use use the bracket operator. So if I want to access Toyota here I have to use Out my map car at one two three See this would print out the one two three Use the license plate on one two three and would get you Toyota So this would this this is how you access it you put a brackets here and then you put the key The specific key you want to access. So in this case, I want to access the key The I want to access the value Toyota. So I pass in the key one two three So this should give me Toyota Yeah, Toyota C first one's Toyota. I don't need to print out the map anymore. So I'll get rid of that and you if you want to do it with The same thing here for Tesla you just passing the key of 234 and I'll do that If you don't want to use the brackets you could use at this is pretty really old but They allow you to I think this is like I don't know You want to so if you want to do dot at 234 it should print it out Yeah, see Tesla 234. So if you want to you don't want to use the brackets go ahead use the is at You can also change the values if you use use the spec Specify the key and then use an equal sign. So if I want to change, I don't know One two three the this The key of one two three its value to instead of Toyota. I want to change it to I don't know general motors I could do that and this would change it to general motors when I print it out So let's print out this value out And It should give me general motors Yeah, general motors So yeah, you could do that You could easily do that. Um Usually equal sign will change the specific value. All right. Now, let's use modifiers. Um insert is just It's basically the same thing as inserting um If you want to insert a specific like position you could also do that Uh, I could show you guys how to do that. Like if I want to insert at like Like the first position here I need to create an iterator actually first Okay, string iterator in string Okay, uh, you gotta do it Oh my gosh, okay Uh Oh wait my fault Take it like this Yeah, um, you could do this Uh I believe you can In iterator it might begin. Oh wait. Hey, you can't do a plus one. That's weird But um, what you can do is if I want to insert at a at the iterator I would just do my map part I insert And then insert whatever new pair that I want So let's say I would just do uh I'm gonna part up again. So at this certain position And I could insert whatever new pair I want. So if I want to do like, I don't know five six seven It's gonna have like general motors It'll insert that into that position All right, and app begin it'll do that Um, yeah, so that's insert. Um Wait, you could also insert things. It's just like it's if you don't want to pass a position You could also insert things like this. It'll just insert it And uh, we could just I could show you what it looks like I have to put a break point here So here it would insert five six seven and that'll be your general motors So here. Yeah, we just inserted five six seven And it has general motors. So that's what it has But yeah, uh, I think that's it Yeah, that's it insert position. You could also insert. Yeah, that's it. That's it. So that's all of insert. Um You could erase If I want to erase um, you need to find the specific key and then erase it Um, you could also just erase the key. So like if I want to get rid of general motors Or do my map dot erase And then the certain key. So I would have to Passing the key which is five six seven And then it would erase it So if we were to print Out of all the values in our car All the key value pairs In our map We would not have general motors Yeah, general motors on there So yeah, if you want to erase you just pass in the specific key that you want to erase um Yeah, you could also erase by a range. So if I want to erase from beginning to end I could do that also So if I want to erase Actually, let's keep this erase it dot begin My car dot begin My car dot end Map car It would completely erase it So this should just be empty because I erased all the values from beginning to end That's what my map card dot erase would do. So yeah And by the way, I just named this map car. It could you can name your map as whatever you want it to be It doesn't have to be like that Uh, you can swap Yeah, you're just like I don't know why anyone want to do this But yeah, if you created two maps and you want to swap it just do First map dot swap second one. So if I want to do like second map, I don't know 567 Has john. I don't know I think it really doesn't matter 8 9 10 Has cat And I want to swap The first with the second map car dot swap map Let me just print out map car again Okay, um, it's gonna have 567 john 8 9 10 cat See the reason why is because it swapped the first map with the second map. So now all the values are swapped see All the values in the second map is now in the first map Okay, um, whoops, let's look at another one I believe Do we go Okay, swap clear clear just just gets rid of everything Like you it really doesn't matter you want to clear it just do dot clear It just gets rid of everything like if I want to do map car dot clear It just removes all the values in your map So then if I run it There's nothing there There's a look is just nothing there. Yeah, so now it's empty Okay in place is the same thing as insert um Yeah Yeah, pretty much if insert this increase decided by one Okay, so so in place constructs and inserts an element Inserts a new element in the map if the key is unique this new key new new element is constructed in place using arcs Okay, so Yeah It's just the same thing um As e as insert There's like no no difference Uh constructed Yeah, it's there's no difference. You just do in place If you want to, um Yeah map car got insert, uh place uh Five six seven john They'll insert it Okay, what's wrong? Oh, I have to it's not places. It's just Okay, so then yeah in places would basically insert and then now you would have five six seven john inside of your map Yeah, five six seven john is inside your map. Okay, so yeah, there's that um In place in place hint. I don't know what anyone would want to use this I don't know what this is doing Value in position is used as a hint in the insertion point Oh, okay, so if I pass in Huh wait pass in 10 Will be used as a hint I have no idea. I don't I don't want to know no no one cares about this all right, um I think that's it Key value comparator. Yeah, you could pass in a comparator if you want Um, they'll change the way it's sorted Uh, you want to pass in a comparator value here you could change how that's sorted also so, yeah, um Find okay, so you could actually find a an element with a key equivalent to it so, um yeah Like if I want to find What what is the element that has one two three? right Like in my map car like if I don't know what that this map has one two three and it points the toryota I could do Up card I find one two three And it would get me the iterator of where it's pointing to which would be toryota So I could do like auto it And it'll give me the position and where it's pointing to No, you normally use this to like erase values But once I have this position, I actually actually I could actually print it out I had to print out the corresponding value That I found which is toryota. So it's going to print out I search this for the key of one two three and it prints out toryota Yeah, see, okay. So we have that um Okay, fine. Okay, we count. Okay. So this counts how many elements with a certain key So this is useful if you want to see, um If it contains an element So like if I want to find if this map of map car Does it have Uh Does it have the key of five six seven, right? So if if it does if it's greater than zero, it'll tell me It does So it says, okay It contains this key of five six seven So yes, it does And no, it does not It's print out. No, it does not This is very useful to see if uh your map contains a value So if we do dot count five six seven, it does not No, it doesn't yet. It doesn't have the key five six seven So now if we did have a key five six seven or let's actually use a one two three Now now we do have a key of one two three adds to oryota Now we're going to do a dot count to see if it does contain it in this case. It does So it should do yes, it does so it does contain it Okay, so that's a good part of count Helps you if it contains a value a lower bound is like binary search you could uh use the binary search To find the lowest bound of where it is at and you would print it So if I want to do a lower bound on b I just searched do using binary search On it. So if I wanted to do like if I want to find something I use a lower bound Use lower bound on a whatever value particular value the binary search it and that's lower bound Upper bound is the same thing, but it's the the top most binary searched value. It's one on the right So yeah, um equal range I don't know what this is Returns the bound of ranges. Okay. So this is just saying like It gives you a range of all the elements to have the key that is equivalent to it. So if I want to find like abc or something Like give me all the elements that are like equivalent to abc or something But yeah, um Yeah, they give you a range of the positions where it's at That's it. That's pretty much all the values of uh all the Specific functions of maps. I hope you guys enjoyed this video rate com subscribe. I'll see you guys later. Peace