 Hello, and welcome back. In this lecture, we are going to continue our discussion of the template class vector. Here is a quick recap of some of the relevant topics we have already studied. We have been studying about object oriented programming with structures and classes. We have seen a brief glimpse of the C plus plus standard library. Specifically, we have looked at the string class. And in the last lecture, we studied some features of the vector class. In this lecture, we are going to study a few more features of the template class vector. Much of this lecture is motivated by the treatment in the book and introduction to programming through C plus plus by Abhiram G. Ranade published by McGraw Hill Education in 2014. Now, recall that the vector class is basically used for representing and manipulating one dimensional arrays of objects. And it uses dynamically allocated arrays to store the elements. So, the arrays can grow or shrink in size. We could resize a vector dynamically during execution of a program. And all of the dynamic memory management is built in. That is why it is very convenient to use the vector class as opposed to a user defined array. Now, we have also studied that vector objects are basically container objects. So, basically they contain a collection of objects of some other type. We also know that we must use hash include vector at the start of a program if we want to make use of the vector class. And truly, this class has a large number of very useful member functions. But in the limited time available for our lectures, we will be able to see only a small subset of these. We saw some of these in the last lecture. In this lecture, we are going to see some more usage of the vector class. And I encourage all of you to study more about the member functions of the vector class from the handout to be provided. Now, in the last lecture, we have basically seen vectors of integers or vectors of floats or vectors of characters, basically vectors of primitive data types. However, the vector is actually a template class. So, why cannot we have vectors of complex data types? Indeed, we can have. Recall that we had studied about a class V 3 for representing three dimensional vectors. And this had several data members which were private, some member functions which were private, some other member functions which were public and so on. So, basically this was a user defined complex data type. Now, if vector is a template class, I should be able to write a declaration like this. Vector, where the generic type is instantiated by this user defined class V 3. And then I could declare a variable V 3 vector. And here, I am saying that V 3 vector is going to be a vector of size 10. And each element of that vector will be an object of class V 3. So, here I am now defining vectors of more complex data types. And this is as simple as doing this because the vector is basically a template class as provided by the C plus plus standard library. We could also have vectors of pointers because indeed pointers are legitimate types. So, here I have int pointer veck. And each element of int pointer veck is an integer pointer. And here, I have another vector called V 3 pointer pointer veck. And each element of this vector is going to be of type V 3 star star. That is a pointer to a pointer to V 3. Recall that when I declare vectors like this and do not specify their sizes, this vector object gets created with default size 0. So, in both of these cases, I have basically created two vectors of size 0. However, this is a vector of integer pointers and this is a vector of V 3 pointer pointer. Now, well instead of having pointers or primitive data types or user defined classes here, why cannot I have vectors of vectors? Since vector with the generic type instantiated by int is itself a class, it is an instance of a template class. So, I could use that to define another vector of that class. So, here I am saying that x is an object which is a vector and each element of that vector is going to be an object of this class. And what is the object of this class? This is basically a vector of integers. So, here I have basically defined a vector of vectors once again making use of the template in the definition of the class vector. So, as I just said, each element of x, x is going to be a vector and each element of x is going to be a vector of integers. Now, one thing that you must note is that I have used a space between these two greater than signs and this is kind of important. Because, if I did not give this space, this would appear like the greater than greater than sign that we use when we are doing input from our standard input. So, it is important to put this little space here. So, that it is very clear that what I am talking about inside is a vector of integers. That is the class with which I am instantiating the template class vector over here. Now, since I can declare objects which are vectors of vectors, this basically suggests I can now talk about multi-dimensional vectors. Now, this is interesting because the template class vector is basically used for representing and manipulating one-dimensional arrays. However, by instantiating the generic type with a vector of another type, I am now able to get a multi-dimensional vector. So, let us see this example. Here, x is declared to be an object of a class vector where each element of that vector x is going to be a vector of integers. Now, recall also that we can specify some initial parameters to the vector constructor and if you recall from the previous lecture when I write something like this, it basically means that x is to be a vector of size 4 and each of the four elements of x are to be initialized with this value. So, here I am saying that x is a vector of size 4, each element of x is going to be a vector of integers and each of the four elements of x are going to be initialized by whatever this constructor function returns and what does this constructor function return? It returns a vector of integers of size 9. So, therefore, x is a vector of size 4 and each element of x is initialized with a vector of integers of size 9. So, that is what I have shown here. These four horizontal rectangles basically denote the four elements of the vector x. So, x is indeed a vector of size 4. However, each of these elements is in turn a vector of integers of size 9. So, each of these little blue boxes here corresponds to an integer and therefore, each of the elements of the vector x is a vector of integers of size 9. So, you can clearly see here that by defining a vector of vectors, I am able to now talk about a two-dimensional matrix. How do I access the elements of this two-dimensional matrix or vector of vectors? Well, it is as simple as this. Suppose I say x 2 put parenthesis around it and then say 3. I am really saying please access the element with index 2 in the vector x. So, this is the element with index 2. Now, that itself turns out to be a vector. So, please access the element with index 3 in that vector. So, this is the element with index 3 where I am assuming that this is the element with index 0, 1, 2 and 3. Now, this kind of access can also be simplified in C plus plus by writing like this and here you can clearly see that by defining x as a vector of vectors, I am getting to use x as a two-dimensional array like a two-dimensional matrix. Now, consider this very interesting piece of code where I have declared x to be a vector of integer vectors and I have said that x is going to have four elements. But now I am saying that instead of all the four elements being an integer vector of the same size, x 0 is going to be an integer vector of size 1. This is basically calling the constructor function of the vector class instantiated with integer for the generic type with the parameter 1. So, therefore, this is going to return a vector of integers of size 1. So, now I am saying that x 0 is going to be a vector of integers of size 1, x 1 is going to be a vector of integers of size 2 and so on for 3 and 4. So, therefore, here x is a vector of integer vectors. However, the different elements of x are not integer vectors of the same size. So, effectively what I have done here is I have created a lower triangular matrix. So, x 0 is an integer vector of size 1, x 1 is an integer vector of size 2, x 2 is of size 3 and x 3 is of size 4. So, you see here that using the features of the vector class I can also create lower triangular or upper triangular matrices like this. So, here I am showing a definition of a user defined class which uses the template class vectors that we have already studied. So, this is an example of real user programming that makes use of the template class vector. So, here I am trying to define a class called matrix 2 D for a two dimensional matrix of integers, a two dimensional array of integers. This class is a private data member called elements which is a vector of vector of integers. It has several public member functions. Let us look at the constructor function matrix 2 D it takes two parameters m and n both of type size underscore t. Well, what is the size underscore t? It is really an alias for unsigned integer and indeed this is the preferred type in C plus plus for sizes of objects in memory. So, whenever you are trying to refer to the size of an object in memory in C plus plus it is good practice to have the type of that whatever variable represents that size to have the type of that variable the size underscore t. Size underscore t is also the preferred type for indices when you are trying to refer to individual elements of a string or a vector or other data types. So, for example, the find or the r find member function in the class string that we have studied actually returns an index which is of type size underscore t. Of course, you must also remember that size underscore t is really an alias for unsigned integer. So, let us see what this constructor function does. It is actually using an initialization list like we have studied and in this initialization list it is initializing the data member elements with some value. What is the value? Remember the data member elements is itself an object of class vector. So, the data member elements is actually being initialized with whatever the constructor of the class vector is going to return when it is passed these two parameters. So, here what I am doing is I am initializing the data member elements with whatever the constructor of the class vector is going to return when it is passed these parameters. So, what is the constructor for vector going to return when it is passed these parameters? Recall that when I pass parameters like this when declaring a variable of type vector what this means is this is the size of the vector and this is what each element of the vector should be initialized to. So, here I am saying that elements should be initialized to a vector of size m where each element of that vector is an integer vector of size n. So, really I am initializing elements to a two-dimensional array or a two-dimensional matrix of size m by n. Here I have a public member function which is operator open parent close parent. So, if you recall our discussion on operator overloading. So, we have this member function operator open parent close parent and if you recall our discussion on operator overloading this is basically the overloaded open parent close parent operator which is also called the overloaded function call operator and what does it take as parameters? It takes i and j both of type size underscore p and what does it return? It returns the element in the ith row and jth column of our two-dimensional matrix. So, basically it accesses elements ij and returns it. Now, we have also two other member functions here n rows and n columns. So, if I want to view this vector as a two-dimensional matrix how many rows does it has? This is exactly the number of elements of this data member. So, I return elements dot size remember elements is a vector. So, elements dot size is going to return the number of elements in this vector and what is the number of columns? That is the number of elements in each element of this data member. This data member is a vector. So, I can pick up a particular element of this vector that itself is going to be a vector of integers and I can find out how many elements are there in that vector of integers. So, basically I can pick up element 0 and then find out the size of that vector which is element 0. Here I could also have used elements 1, elements 2 whatever, but here I have just chosen to use element 0 because as long as there is at least one row in this two-dimensional matrix I will be able to find the number of columns using element 0. So, here is an example of a simple user program that makes use of the matrix 2D class that we just saw. So, here in this program I have declared an object m of class matrix 2D and I have passed 5 and 5 as the parameters to the constructor function. So, if you recall the constructor function will take these two parameters here m is 5 and n is 5 and it will simply create a vector of size 5 where each element of that vector is again an integer vector of size 5. And then here I am basically initializing the elements of this vector. So, I am iterating from i 0 till i less than m dot n rows this was a public member function that we just saw and similarly I am iterating from j is 0 to j less than m dot n columns and m i j here is the overloaded function call operator that we saw in the previous slide I am passing it two parameters i and j and it is going to return me the j th element in the i th element of the vector m. So, basically that is the element in the i th row and the j th column of the two dimensional matrix m that I am representing and it is initializing that to some value in this particular case i squared plus j squared. And then I could have some other code here which makes use of these values or updates these values, but the purpose of this example is to show how we can use a user defined matrix class which internally uses vectors in a real user program. So, in summary in this lecture we studied about some additional features of the vector class we saw how to define multidimensional vectors and of course, there are many more member functions of the vector class we did not have time to study all of those in our lectures here I strongly encourage all of you to study those from the handout to be provided. Thank you.