 Hello and welcome back. In this lecture, we are continuing our study of the C++ standard library. And specifically, we will study about the template class vector in this lecture. Here is a quick recap of some of the relevant topics that we have already studied. We have studied about object oriented programming with structures and classes. We have studied about template classes and functions. And in the last lecture, we also saw an introduction to the C++ standard library. And we studied about the string class. In this lecture, we are going to study about the template class vector. Much of this lecture is motivated by the treatment in the book and introduction to programming through C++ by Abhiram G. Ranade published by McGraw-Hell Education in 2014. Now, the vector class is a very useful class in the C++ standard library. And it is actually used for representing and manipulating one-dimensional arrays of objects. We will later see that we can actually build even multi-dimensional arrays of objects also using the vector class by basically treating a multi-dimensional array as an array of arrays of arrays and so on. We will study this in greater detail in a subsequent lecture. For the time being, let us just consider the vector class as a mechanism for representing and manipulating one-dimensional arrays of objects. This is a template class which means that it can be instantiated with specific type to represent one-dimensional array of objects of that type. It uses dynamically allocated array to store the elements. So, depending on how many elements you are storing, the array can grow or shrink in size. And a very nice feature of this class is that dynamic memory management is built in. So, you as a user of this class do not need to worry about dynamic memory allocation or deallocation. Vector objects are basically container objects. We have studied about container objects in the last lecture. These are basically objects that hold a collection of objects of some other type. So, vector objects are really container objects. We must use hash include vector at the start of a program if we are trying to use the vector class from the C plus plus standard library. And this class actually has a large collection of member functions. However, we will get to see only a small subset of these. I strongly encourage all of you to read about the complete set of member functions of the vector class that we will provide in a handout and try to use these when you write your programs. Now, here is a simple program that uses the vector class. Remember, it is a template class. So, when I declare a variable of this class, I have to instantiate the generic type in the template. So, here intvec is a vector of integers. This is indeed a vector of integers of size 0. It has not been specified what should be the size of this vector. So, by default, it will consider it to be a vector of size 0. Here is a vector of floats of size 20. And here is a vector of characters of size 5, where each element of that vector is initialized with the value a. This is the ASCII code of the character a. So, you see that there are multiple ways by which we can declare an object of a vector class instantiated with a specific type. If I do not specify how many elements are there in the vector, the vector will be of size 0. Otherwise, the vector will be of the specified size or I can even initialize all elements of the vector with a particular value. Now, the vector class provides you this member function called size, which will actually give you the number of elements in the vector. So, in this case, if I print out intvec.size and floatvec.size, I will get 0 and 20 respectively. And of course, here if I print out the elements of carvec and note how I am accessing the elements of carvec just like I would access a normal array. So, this is another interesting feature of the vector class. Once you have declared a vector of a certain type, you can actually access elements of the vector just like a normal array. So, if I print out the elements of the carvec array, I will get 5 a's because I have initialized all the elements of this array with the constant which is the ASCII code of the character a. So, we have just seen that we can access elements of a vector using the square bracket operator just like a normal array. Also access elements of a vector using the at member function. So, here in the simple program, I have allocated a vector of integers of size 5. I have stored 0, 1, 2, 3, 4 in the 5 elements of that vector. And then I am asking for an index and whatever index is provided as input, I am going to read the value of the vector at that index using the at member function. So, what this will do is it will get the element of intvec at the index given by the value of index and it will print it out. So, for example, if I give the value of index is 3, this will print out value at index 3 is 3. Now, what would happen if I had instead given the value of index as 100 which is beyond the size of the vector that I have allocated here. Well, the at member function actually checks whether the index that you are using is out of range given the size of the vector and if so it is going to throw an out of range exception. However, if instead of intvec dot at index, if you had just written in vex square bracket index, then no check would be done whether the index is out of range and it will simply try to go and access the memory at index positions away from whatever intvec is whatever the starting address for intvec is and this might lead to accessing a bad memory which might cause a program to crash or it might actually also lead to reading of garbage value. So, the difference between these two ways of accessing elements of a vector that is using the square bracket and using at is that if I am trying to access an element of the vector outside the legal range for that vector, then if I use the square bracket operator, I may end up doing illegal memory access or I might even access garbage. However, if I am using the at member function, then it will detect that the index I am using is outside the is larger than the size of the vector that I have allocated and therefore there is an out of range exception. So, this is the difference between using the square bracket operator to access an element of a vector and using the at member function to access an element of the vector. So, clearly using the at function is preferred. Now, I can also access special elements of a vector using these two member functions front and back. So, here I have the same integer vector of size 5 initialize to 0 1 2 3 4 and then I use intvec dot front to access the first element and I use intvec dot back to access the last element. So, if I execute this program I will find that front element is 0 and back element is 4. I can also append an element to a vector. So, here is a simple program where I have declared a variable intvec without specifying a size. So, this is a vector of size 0. So, when I try to print out the initial size, I will get initial size 0 and then I can use the member function push back to actually append an element to a vector. So, I start off with a vector of size 0 and then inside this loop I first push back 0. So, then it becomes a vector of size 1 containing the element 0 then I push back in that vector the value 1. So, now it becomes a vector of size 2 with element 0 and 1 and so on as I iterate around this loop. So, after I have done this I have basically done 5 push backs. So, it is going to append 5 elements at the end of the array. So, finally final size will turn out to be 5 and if I actually try to print out the elements of the vector after doing these 5 push backs I will get 0, 1, 2, 3, 4. I could also delete an element from the end of a vector and the function to do that is called pop back. So, here this is the same code that I had originally the initial size of intvec is 0 I pushed in 5 elements. So, the final size is 5 and then I pop back 1 element. So, the size after pop back is going to be 1 less because this pop back is going to delete the last element of the vector size after pop back is going to be 4. And now if I try to print out the elements of the vector I will not see the last element. So, I will simply see 0, 1, 2, 3 recall that the after these 5 push backs the vector had 0, 1, 2, 3, 4 pop back deleted or erased the last element which is 4 and therefore I am now going to only see 0, 1, 2, 3. Now recall from our earlier lecture that we had also talked about iterators in C++ and iterator is like a pointer, but it is not exactly a pointer. It is used to point to an element in a collection of elements and it can be used to iterate through the elements in the collection. And we had said that the iterator must support 2 operations the plus plus of the increment operation to allow us to iterate through the elements in the collection and also the dereference operation. So, here are some iterator related functions in the vector class. So, just like for the string class I have this begin and end member functions and here in this code I have an integer vector in which I have pushed back or appended 5 elements 0, 1, 2, 3, 4. At the end of it I have also pushed back minus 1. So, now there are really 6 elements and then I am using an iterator of the vector int class. This iterator is called it. I am initializing it with intvec.begin. So, that iterator is now going to point to the first element of the vector and I am going to use plus plus to iterate through the elements of that vector and I am going to continue until I have gone past the last element in the vector. intvec.n is basically an abstract pointer to the element after the last element of the vector. So, basically this will iterate through all elements of the vector and I am simply going to print them out. So, what I am going to print out here is 0, 1, 2, 3, 4 obtained from these 5 pushbacks and also minus 1 obtained from this last pushback. We also have reverse iterators in the vector class like we had in the string class and the rbegin and rn member functions are used to obtain reverse iterators. So, here I have the same main function which basically creates an integer vector containing 0, 1, 2, 3, 4 and then minus 1 and here I am using a reverse iterator of the vector int class. I have called that rit. I initialize it to intvec.rbegin. So, this will give me basically an abstract pointer to the last element of the vector and then as I increment rit I will be basically stepping through the vector from its end towards its beginning and I will continue to do that until I reach the beginning of the vector and then go past it. So, intvec.rn is basically an abstract pointer and iterator pointing to the element just before the first element of the vector. So, therefore, until I reach that point I am going to iterate and I am going to print out the elements in the vector. So, on executing this I will basically print out the vector in reverse order minus 1, 4, 3, 2, 1, 0. Now, I can also insert and delete elements in the middle of a vector. So, in this program I have created a vector which contains 0, 1, 2, 3, 4 here and then I have printed those out here. So, this will print out 0, 1, 2, 3, 4 and now I get an iterator of the vector of integers and this iterator is intvec.begin plus 2. So, it is basically pointing here and then suppose I say that insert at the position pointed to by that iterator the value 0. So, it is going to insert 0 here push 2, 3, 4 after that and the size of the vectors of course going to increase it will become 6 now. So, the size after insert will become 6 and now if I try to print out the vector remember I have inserted 0 here. So, I will basically get 0, 1, 0, 2, 3, 4. Now, after that I can once again find the iterator which is intvec.begin plus 3. So, that is this position, but now I am actually looking at this vector remember intvec at this point is this vector. So, intvec.begin plus 3. So, this is plus 1 plus 2 plus 3. So, this iterator which is intvec.begin plus 3 now points to this element of the vector and I could also ask intvec.erase it. So, it will erase this 2. So, the size after the delete is going to become 5 again and if I try to print it out that 2 would have been deleted. So, it will print out 0, 1, 0, 3, 4. Now, we have seen that push back and pop back automatically resize a vector c plus plus also allows explicit resizing of vectors. So, for example, here in the main function I create a vector called intvec of size 5 all initialized to 0 and then I resize it to size 10 and all the additional elements are initialized to minus 1. So, this is the new size after resizing and since additional elements are being created I can specify what they should be initialized with. So, the size after resizing in this case will be 10 and now if I try to print out this vector I will see the 5 initial zeros followed by 5 minus 1's and after that I could take this vector and I could say intvec resize 7. So, now the size of the vector would become 7 and it would just keep the first 7 elements. So, now if I try to print it out I will get 5 zeros followed by 2 minus 1's. So, in summary in this lecture we looked at the vector class and its usage. However, we were able to study only some features in this lecture we will study more features of this class in the next lecture. Thank you.