 Welcome back. In the previous segment, we discussed the string class. In this segment, we will be discussing the vector class. So the class vector is should be thought of as a friendlier and a more versatile version of arrays. To use it, you have to include the header file vector. You can make vectors of any type by supplying the type as an argument to the template and we will see this in a minute. So like arrays indexing is possible but there are more things. You can extend the length and you can also insert in the middle of the arrays. And the implementation of the vector class is sort of like the string class in the sense that it will also allocate memory and memory from the heap. So there will be member classes but they will do the memory management and all that will happen automatically exactly like the way it happened for the string, for the string class that we developed. But of course the functionality of the vector class is different. So there will be differences also but this whole idea of having the memory management happen behind the scenes is of course exactly like that. So I am going to discuss the vector class also just by taking examples. And again this is not going to illustrate every feature. To do that you should look at the documentation on the web. So I can create a vector of integers by specifying int inside the angle braces or this is what is called the template argument. So if I write vector int v1, I get an object called v1 but it is an empty vector. So there are no elements yet but whenever elements come in they will be of type int or you are expected to put in elements of type int. So here is a vector of strings, you can have vector of anything, any type. So here is a vector of strings and this time the name has an argument. So this really is a constructor. So if you have a one argument constructor it automatically creates a 10 element vector of strings. The strings are initially empty, there is nothing, it is a 10 element vector of strings. Now this has a more interesting constructor, first of all it is a vector of shorts. So v3 is going to be a vector of shorts. It is going to have 10 elements but if you have this additional argument, this additional argument says that the values of all the elements will be 300. So v3 will store 10 copies of 300 once one per element. I can write an assignment and I can copy vectors directly just by specifying the entire name. So to that extent vectors are like structures and of course you know that vectors are classes. So they are indeed structures but they also have parts which are on the heap. So typically the data is all on the heap. They can do indexing, so v3 is this thing that we defined over here, this vector that we defined over here and I can set the sixth element to be 34 and I can also look at any element inside that, whatever I want. Here is a more interesting operation, I can push back an element. So this is going to append 22 to v3. So originally v3 had length 10 and it contained 10, 300s but now it is going to contain an extra element. So it is going to have elements now 0 through 9 plus the 10th element and that 10th element is going to be 22 or maybe this will require some memory allocation unless you allocated a large amount of memory to begin with but potentially this will require memory allocation and that will happen behind the scenes. You do not really need to even know about that. I can find the size of a vector or I can also use length here if I wish. So this will just print out the current length or the current size and in this case 11 will get printed out. I can change the length. So this says change the length to 9. So originally v1 had 0 length so this is going to make the length be 9. This is going to change the length of v2. Remember v2 was a vector of length 10. All the elements were empty. So this is going to have now a length of 15. The new values will be initialized and they will be initialized to a string consisting of 2 stars. So there is a rich range of functions, member functions and constructors so you can do lots of things with very small commands. As I said you should look at the online documentation to get the whole range of commands or operations that are available. Now pushback is an interesting operation because often you have to guess the length of an array and you have to guess it and you have to allocate an array. But with the pushback you can just keep adding elements into your vector so that guess work is gone. Yes, pushback will require increasing the space and after increasing the space the copying will also have to happen. So it is going to be a little bit more expensive than just assigning a value to an element. However it is done very cleverly. So it is done cleverly in the sense that suppose you exhaust the current allocation of space and you push back one more element. So what is done is a much larger space is given to you not just that extra element. So that space is allocated and you copy your current vector into this new space. But because you get a large space whenever you ask for it your copying and your reallocation operations do not happen that frequently. And there is a way to do this just right so that really the effort that you spend in reallocation over the entire course of the program turns out to be really quite negligible. So effectively pushback and indexing can be thought of as cheap operations. So this is one of the things which is really great with vectors that you can keep pushing back and it is a cheap operation. So do not really worry about it. On the other hand inserting elements in the middle of vector is possible but it is an expensive operation. And you should really not do this too commonly. I mean if you are doing this too commonly you had better have really good reason. So do not insert casually in the middle. Vectors can be passed to functions by value or by reference and if you pass by value things get copied. So if you make a modification in the called function then the vector in your calling function does not get modified. But of course if you have passed it by reference then the modification will be happening in your calling copy as well. Now a nice thing about vectors is that when you pass a vector either by reference or by value you do not have to pass the length. So this just reduces the clutter. So your functions look more, your function calls look more compact. So I would definitely advise you that now that you know about vectors just start using them, do not use arrays because they are just more elegant, more compact, more elegant, they can do lot more things. I want to make a technical remark about vectors. So there is a member function size which returns the size. Now the size is returned as a type size t. Size t is just an alias for a certain kind of unsigned integer type. And it is a type which is meant, it is a type or it is a type of variable which is meant specially for storing array indices. Now therefore when you go through array elements or rather vector elements you should use size type for the index variable because if you have code like this, here you are comparing the index with v dot size. If you had declared this i to be int then you would be comparing an integer with an unsigned integer. Now this comparison is something that C++ finds a little bit error prone because unsigned integers have that wider range and C++ compiler writers worry that look are you sure you want to do this comparison that kind of thing. And therefore instead of declaring this control variable to of int if you declare it of size t you will not get that warning about this comparison between an unsigned int and an int. So this is tricky as it was discussed in 6.8 and therefore the compiler puts a warning but when you compile you do not want any warnings, you do not want any clutter and therefore it is a good idea to just use a size t over here rather than an int. So if you use int it is not a big deal but it will just have clutter when you compile. Now the vector class can also be used to build multi-dimensional vectors or really I should say multi-dimensional arrays if you will. Here is how you do it. So you say vector of vector of ints that is what this variable vv is and note by the way that I need to put a space over here. If you put these two less than less than consecutively then C++ will interpret it as the output redirection operator. So what does this nominally say? It says nominally says that each element of vv will itself be a vector of integers because after all what you put over here is supposed to tell you the type of the elements of this vector. So each element over here will be a vector of integers. So that means to get to the integer itself we must supply two indices. So vv of 0 will get me to a vector of integers. If I add another index I will get to the actual integer. So we really have a two dimensional vector over here. So here for example is a definition of something which looks complicated but which can be easily understood. So I have a vector of vectors of integers and that is called v1. So this 5 tells me how many elements this vector has. Whatever follows is supposed to tell me the initial value of this element but this element itself is a vector of ints. So the initial value of that vector of ints is going to be a vector of int of size 10 and each element having a value 23. So what have we done? So vv1 has 5 elements each of which is a vector of ints and each has length 10 having initial value 23. So what do we get at the end of this? We are going to get a 5 by 10 array all elements 23. So quite cool. This syntax is not really new. I already introduced this syntax but I am just using it in a somewhat elaborate manner. Now if I define a vector like this I can get how many rows and columns are in it by just asking the size of vv1. So the size of vv1 is 5 and if I go to the 0th element then it is going to be something like this and its size will be 10. So if I am using this to store a matrix and if I want to get the size, the number of rows and the number of columns I can just do something like this. So by using vector of vectors I can create a 5 by 5 identity matrix and here is how I am going to do it. So first of all let us see what this does. So I am going to have a vector of vector of doubles and I am going to call it m. It is going to have 5 elements each of which is a vector of doubles of length 5 but all the elements 0. So at this point already I will have a 5 by 5 matrix but in this matrix every element will be 0 that is not quite what I want. So but I can access the individual elements. So what I do is I am just going to go over the diagonal and for that I am going to go through i equals 0 to 4 and this will give me the diagonal. So 0 0 1 1 2 2 3 3 all of these will be set to 1. So now I will have a 5 by 5 identity matrix. So here is an exercise, two simple exercises which I will definitely encourage you to do. So what have we discussed in this segment? So we have discussed vectors which are a more powerful version of arrays and here is a point that you should note, vectors are essentially as fast as arrays if you only use indexing and pushback operations. If you insert in the middle then things get slowed down but with just these two operations they are practically arrays. Using vectors to functions is more convenient because you do not have to specify the length you can get by doing V dot size. Then vectors can be used to build nice multidimensional arrays. So in particular you should be able to write matrix multiplication operations or all kinds of matrix operations which for all matrix sizes. This is the end of this segment and in the next segment I am going to talk about sorting vectors and arrays but let me take a quick break.