Map In C++ | STL C++

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello friends is Rupesh anyone watching CBB done video series in C++ and this topic is map in C++ so this video is a part of an STL CDs so let's understand this topic the syntax is like this we have math t1 and t2 these two are type and this is going to be your object name so this is the typical syntax for your map where t1 is key and t2 is a value type we'll see all these things here but let me just go through these points quickly so that we will be able to understand all these things little better and the second point is this is an associating container that store element in key value combination so this is first time I am talking about the key value combination in STL series so assume some data structure or some tree for that sake and think that if you have some object and you want to store that object in such a way that retrieval should be faster so you have this key and value pair and key can be anything like it can be a name it can be a number so here as I can show you the example that I have taken key as name and this value is as number so this example is of a dictionary example so on this key you are storing this value okay so if we will go through another point and yeah I just forgot to mention one thing that this key is unique if you are keeping this on it as a key and assigning this value then you cannot have another key with the same name it should be different and values can be similar so if you have key one and here you are keeping two then you cannot keep one and some another value something like 3 no this is not allowed okay but what is possible is let's suppose you have 5 here and this is the key then you can again have 2 so values can be similar across keys but keys should be unique okay whatever key you are using and depending on the keys only your tree will be formed and next point is it is implemented using self-balancing binary search tree like AVL and a red black tree so if you remember your BST was something like this right correct so this is your root and all the left-hand side of this root are lesser than this root and all the right-hand side are greater than this root okay and this property applies to each and every element in this so if you will see this 3 is less than but 4 is less than 5 but greater than 3 that's why it has come to right-hand side and 2 is less than 5 and less than 3 so it has come here so this is the proper binary search tree representation and if we are saying that internally map is using red black tree or AVL tree which is a binary search tree they have just little bit tweaked it and they called red black tree and AVL tree the basic funding is this it is like this so that if you want or you are saying that I want 7 number so we will start from here and then will be knowing that okay 7 so 5 so we have to go right hand side correct because 7 we are looking and 7 is greater than 5 so we'll go inside towards right hand side and we will get that 7 or if you are looking for 6 then obviously 6 is greater than 5 and we are reaching here and then we will see that ok 7 so 6 is less than 7 so we'll go left hand side so we'll get this 6 so in order to achieve this behavior whatever data you're inserting should be comparable then only we can take the decision where should we keep them in this tree ok so if it is number then it is very simple for map to take the decision and come to know that ok this is greater than this is less than and in my case we are using string so as this is string this string class have all the comparison functions I mean you can compare one string with another so again I didn't tell you that which one is greater than which one is smaller so then there is no problem but problem comes when there is a user-defined data type like person I took this similar example yesterday so in case of person class you have age as a data member and another one is name so this is going to be your string data time and this is going to be your numeric data type or you can take a float or double okay but as this is an object and when you will try to insert this object as a key then it is a problem because this map doesn't know how to compare two different objects obj one is equal to equal to obj - I don't know how to compare this this is what it is saying so we have to give compare function for user-defined data types but we don't have to give any compare function for integral types or inbuilt types and this string have already the compare function so there is no problem here so I'll take the example of user-defined data type and will be very clear so I think I have covered all the points here it is using self balancing tree it is store key value pair in sorted order yes I just could not see this yeah see I have written it it stored them in sorted order this is also very important see this is a sorted right so if you apply in order traversal like if you forgot that I don't know I should talk all these things here or not but if you will apply in order traversal this will come out as two three four five six seven eight okay so this is actually in sorted order now you might be thinking in what order ascending or descending that we can decide after giving this type one and type two will apply comma and will give we want greater than comparison operator or lesser than comparison operator so last point is sed map is generally used in dictionary type of problem what is dictionary type of problem dictionary is mainly like you have one key and it can have multiple values or at least one value should be there okay that is kind of a dictionary problem so if you remember a dictionary and you are searching for some name sorry some world like Hello then there will be only one hello in entire dictionary okay so the similar case applies here if you are making something as key then that key would be unique key in entire map okay so having said that let's jump into this and see how it works so before that wait a minute how you will create a map I already said you will be giving two datatypes one it will be the key datatype first one is for key data type so I am keeping key as string so I am giving you string here and the value data type is integer so I am giving integer there are different ways to do this and one of the way is you just have a map object and you use this syntax to actually insert the value which is this one at this key location okay and similarly you can do this as many times you want okay and there is another way of doing this map dot insert STD make pair so this will also do the similar job but in some different way and let's suppose you have updated three values one two and three here and let's suppose you want to iterate over this map so this is the way of doing that you can keep using loop and this is arranged by is loop and we can just have this element one and dot first would be key and dot second would be element so if we are looping through this full map here then first value or the first element which is coming out would be omit if we have used ascending order sorting otherwise true too will come first then bought then omit okay it will be in reverse order so we'll see that and obviously if you want to access some value then you can just simply give map and that key and it will give you that and this one returns the reference of that value so that you can change it so what I'm saying is if you have this map show two equal to let's suppose you are saying zero then it will change this keys value before it was something and now it will become zero okay so you can actually update it but the important point to notice is you cannot change key once this key has gone into the system it will remain like this forever you can only change the value part okay don't miss this information it is really important so let's look at the compiler and run this code so if I'll execute this see we are getting emit bart and cho to see um it starts from a so it is smaller than B then C okay so this is in ascending order and after this I am getting this just because lastly I am printing this so let me just comment this one and then it will be easier for us to see this let's compile this again and run this C now it is amid board and show two so this is the default behavior this is ascending order what if you want to store that in descending order so you have to use greater for the comparison okay before it was less so if you will use greater here and then you have to give these two brackets here you have to say that on what basis you will decide oh sorry what is the type you will decide on so we have integer here sorry string here then it will use the string and it is optional you can omit this so let me just have this here and will compile it will check that and this is so to board and emit so now it is reversed okay and as I said we don't have to give this string I mean the comparison type you just have to give this greater here and let's compile this again and see the result see it is same okay and let me just quickly write this less also so if we have less here then this is the ascending order let me compile this and run this again so see I mean board and show too okay so we have seen everything but wait a minute I told you that this is a dictionary and key will be one and there we multiple of values and multiple values we can store in vector for our demonstration so now the pattern will change now this initialization will not work we will be using pushback for this some number okay and here this will change we need another loop to iterate over this second part because our second in this case would be a vector so let's have another loop here and bring that here we can say that see out element two dot sorry no dot it is simple thing we just need this so now when I will compile this I'm sorry it should work let me just recompile this and execute this whoa there is some issue in here oh let me remove this and compile this again and execute this correct sorry for this I just forgot to remove this because this insert part will not work here because we have this is a vector okay so this will not work but wait a minute this is not looking good right let me just give one see out in DL let's compile this and read on this see this time it is omit this one and show to this one but this is not looking like a magic let me do the magic here we will take this one and change this value by one number okay and save this and then recompile this execute this see this time we are getting two values for Emmet because we have done push back two times for this particular key so this particular key is having two number so as I said value is type of a vector and you can push any number of elements inside this and you should be fine so this is what I wanted to show you people I know this is fun and let me tell you this if you have dictionary cat type of problem and your key is a non integral number like it is not one two five eight or something it is some name then you cannot have vector or array which is fastest so the second fastest is this map okay you can have key and the respective values and this is fastest because internally we are using binary search tree to implement this so remember this if you have key other than number sorry integral number then you should go for map if your problem is suited for dictionary type of problems so this is the time to say goodbye but I'll ask if you liked the video don't forget to hit the like button guys and subscribe to my channel so that you can get the notification for upcoming videos like this I'll see in the next videos bye-bye
Info
Channel: undefined
Views: 54,115
Rating: 4.8629909 out of 5
Keywords: map in c++, c++, c++ tutorial, programming, coding, std::map, stl, standard template library, container, code, how to use map in c++, what is the use of map in c++, map
Id: nPSDR5nZzHA
Channel Id: undefined
Length: 13min 53sec (833 seconds)
Published: Wed Oct 02 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.