 Welcome back, in the previous segment we talked about maps and unordered maps. In this segment we are going to talk about iterators and we will also conclude this lecture sequence. Let me remind you that a map is a set of pairs of the form index and value. And so for example the population map is this as we defined is this sequence. I guess the numbers were slightly different but anyway this could be a population map. Now it turns out that C++ stores these pairs as a struct. Each pair is a struct in some structure, in some container as it is called and the struct has members first and second and first holds the index and second holds the value. So using iterators you can access all the elements or all these pairs in the map one after another do whatever you want with them. So an iterator for a map going from say type index to type value is an object of type map index value colon colon iterator. So this iterator also has a type and this type is this complicated looking thing. So iterators can be created and they can be set to point the first point to the first element in the map. So I said earlier that an iterator is like a pointer. So in that sense when I create an iterator I can set it to point to the first element in the map. And then the dereferencing operator, the star operator is defined and you can use it to get the element that this iterator is actually pointing to. And then the plus operator is also defined and that allows you to go to the next element in the map. So the analogy here is I guess to indices in arrays. So if you do plus plus to an index you get to the next element of the array. So similarly over here if you do plus plus to an iterator you get to the next element of the map. So let us do this by an example. So our population map, so let us say I store 1.37 as the population of India and now let me create an iterator. So mi is a variable that I create but of type iterator. It does not have any, it is not assigned any value yet. So I have to put a value into it. So I can write mi equal to population.begin. So now population.begin is what might be called a constant iterator. It is like writing mi equal to 0. So this iterator points in a metaphorical sense to the first element or the beginning element in this map. So this is a constant operator and it points to the first element of population. Well in this case the first there is only one element so it will point to this element itself. So if I write c out mi arrow first, so remember that we have to think of this as a pointer. So we are first dereferencing it and we are getting to the member first of the dereferenced object. So of course that can be written as by the arrow or the minus greater than operator or the arrow operator. And this will print out India and this will print out 1.37. But you can do more with this. So again I have our population map but let us say we put in all our 3 countries into it. Now I have a for loop and the control variable for the for loop is our iterator and it has been initialized to population.begin. Next we are supposed to supply the termination condition for the loop and this is another constant iterator. So we are going to do this until so long as this does not equal the end. So you can think of this as an iterator which is just outside the valid iterator ranges. So again if you go back to the array this is kind of the length of the array. So you do not want your index to become as large as the length. So that is what this is doing and you are going to increment that iterator. So at the end of the loop you are going to increment the iterator so you go on to the next element of the iterator. So to start with MI points to the first iterator and then you check whether your iterator is pointing to a valid element. If so you execute the body and at the end of the body you advance the iterator. So maybe inside the body we are just going to print things out. So whatever the iterator is pointing to the first element or the country we will print and then we will put colon and we will print the second value. That is it. So what should this do? Well you would think that they should print out the countries and their population and that is indeed what happens. But there is something nice, further nice that happens. So this iterator, in this case it will print the countries and populations in the alphabetical order or the lexicographic order. And I said earlier that maps require a technical, there is a technical condition which is that the less than operator should be defined on the index types and the index types are strings and the less than operator on the strings is the lexicographic order and therefore when you advance using the iterator the elements are accessed or visited in the lexicographic order and so things will get printed in lexicographic order. This happens for maps but not for unordered maps. As the name says, unordered maps have no order. So you will get the elements in some order but there may be nothing interesting as far as that order is concerned. That order is something that the implementation decides. It may not even be the element that you inserted first, it could be some arbitrary order. Again there will be reasons for why it is that order but the reasons are not interesting from our point right now. So we said that there was a technical requirement and the less than operation and when we step through the map using an iterator we visit the pairs in increasing order and the less than operator need not be defined but the printing will not happen in any interesting order. So we will do a demo of this just to see what happens with ordered unordered maps. So let me get out of the presentation and so let me open this file. So here is our map, oh by the way I can initialize it in this manner as well. So what does this mean? This is a pair that I want to put in, this is another pair that I want to put in, this is another pair that I want to put in. But the order over here is not the order in which the pairs actually go in. This is I can just put it in the order that I remember to put in but C++ will store them in the right order so if you need the order it will be there. Now what I am going to do over here is I am going to do that going through all the elements but I am going to do this for, so I am going to do it twice, I am going to do this first for a map and then later I am going to do this same thing for an unordered map. So for this map I am going to print out all these things, so exactly the same thing, it is the same loop going through all the elements in order and here, oh I guess I should put this see out statement before I, after I define the map it does not really matter. I am putting in the same elements and I am going through the same kind of a loop but this loop is for an unordered map. So the iterator is for an unordered map and my original map is an unordered map. So let us see what happens. So see what happened. So for the map the contents were printed in alphabetical order or lexicographic order. For the unordered map they seem to have been printed in some strange way, who knows what way they have been printed. But anyway so the lookups and indexing will work exactly the same for both. So let us get back to our presentation. So I want to talk about a few shortcuts which are of somewhat recent origin. So instead of writing that whole complicated type expression map colon colon iterator all of that I can just put an auto over here and then I can just say m i equal to population dot begin. So this is an interesting statement because I am not actually giving the type I am telling C++ that look I am assigning this to this and so now you infer the type. So C++ knows that this has type all that map something something colon colon iterator. So therefore it will itself decide that this should be that type. So saves us a lot of typing. The rest of the code is the same but there is even more dramatic improvement possible. Another C++ feature 11 feature. So this is what is called a range for loop. So here I am not even really using an iterator explicitly. I am saying after you iterate you will so tell me go directly to the objects that I am iterating through. So do not even get me the pointers tell me what they are pointing to. So c and p is really are really the pairs the country and population pairs which are stored in this iterator. So this says that for all possible pairs do whatever is in the body. And again since this is a map not an unordered map we will go over these pairs in increasing order of the index. So I can just write over here c and p dot first by the way this is not arrow because c and p is the pair itself not a pointer to the pair and here again c and p dot second. That is it. So this really makes it much nicer to deal with maps because all those huge iterator references are not there at all. So iterators can work with vectors and arrays. So for that please see the book. Iterators can be used to find and delete elements from maps and vectors. Declaring an iterator is verbose because the type is usually long. So as we just saw you can specify the type as auto and c++ will infer the type and occasionally it might be useful to define to use a type def statement. This is also defined in the book. So in fact instead of writing say something like vector of vectors vector of vector of int I can call it a matrix. So wherever I want to type vector of vector of int I just need to say matrix. So type def statement allows me to do just that. Again for the syntax of it please look at the book. So that brings us to the end of this lecture. So I just want to make a few conclusions. So a few remarks. The standard library contains some classes which we discussed but it also contains some other classes. So there is a Q class, there is a priority Q class which is discussed in the book in chapter 27 and several other classes. And even the classes that we have discussed have a lot more features and for that see the online documentation on the web. The standard library classes use heap memory however this happens behind the scenes and you really do not even have to know about it and that is the beauty of it. It is reducing your thought overhead. You can think of these as just as abstract mathematical objects. So a map, there is a mathematical object called a map. So the c++ object called a map is very very similar to the mathematical object called a map. So it is a nicer way of thinking about things rather than saying that oh there is memory allocation happening. It is happening but you do not have to think about it. These classes are extremely useful so definitely get lots of practice with them and solve the problems at the end of the chapter and we conclude this lecture over here. Thank you very much.