 Hello and welcome back. In this lecture, we are going to study about the C++ standard library and specifically, we are going to look at a very useful class in this library, the string class. Here is a quick recap of some of the relevant topics we have already studied. We have studied about object oriented programming with structures and classes. Interestingly, all the classes that we have seen so far were custom designed by us. We designed their data members and their member functions. In the last lecture, we also studied about template classes and functions. In this lecture, we are going to look at the C++ standard library, which is actually a collection of predefined and very useful classes that come with all standard C++ distributions and we are going to look at in much more detail about a specific class in this library called the string class. 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 Hill Education in 2014. Now, some classes and functionalities are very commonly used across a wide variety of applications written by C++ programmers. For example, C++ programmers very commonly use strings. They use vectors of integers, characters or other data types. Similarly, they might use lists of different kinds of structures, queues, stacks, priority queues, sets, maps and so on and so forth. Now, instead of each user defining these structures individually and separately each time, C++ provides standard implementations of a lot of these classes and functionalities as part of every C++ distribution. So, this is called the C++ standard library. It is basically a collection of commonly used classes and functions. It is part of the C++ ISO standard and what is very significant is that the implementations of classes and functions in the C++ standard library use very often the best algorithms available. The implementations are extensively tested. They use good dynamic memory management and therefore, it is really worth using them in the programs that we write. Now, all features in the C++ standard library are declared within the STD namespace. So, if at the beginning of your program, you say using namespace STD semicolon, then you have basically taken care of this namespace issue. Subsequently, whenever you refer to a class or a function in the C++ standard library, it will automatically the compiler will automatically understand that you are referring the name of that class or to that name of that function in the STD namespace. Another important point is that when you are using the C++ standard library, you must include the appropriate header file in order to access the library features. So, for example, if you are using the string class, you must say hash include open angle bracket, string close angle bracket. If you are using the vector class, you must say hash include open angle bracket, vector close angle bracket. We will actually see lots of examples of these in the examples that we will study. Now, the complete C++ standard library is fairly large. We will not be able to study all of these in the lectures that we are going to deliver. We will only study a few features in the lectures. We are going to provide you a handout that contains a list of the other classes in the library and you are strongly encouraged to go through this handout and to use the standard library classes when you are writing your own programs. Now, this is a bird's eye view of the C++ standard library classes that we are going to study. We are going to study the string class and as you might expect, this class is used for storing and manipulating strings without worrying about the internal representation of strings. We are also going to study about a few container classes. What is a container? It is basically a holder of a collection of objects of another class or some other generic type. So, container classes are usually implemented as template classes. We will see some examples of these in our study. Specifically, we will see the vector class, the map class and the list class. However, there are many, many more interesting classes in the C++ standard library. We will not have the time to cover all of these in our lectures. You are encouraged to read these up on your own. Now, the string class is obviously used for representing and manipulating strings. As I just indicated, you must use hash include open angle bracket, string close angle bracket at the start of your program in order to access features of the string class. And this class actually has a large collection of member functions. However, we will only be able to study a small subset of these in this lecture. And there are also some non-member functions that relate to the string class. For example, operator plus and get line, we will actually get to see usages of both of these. And there are some other non-member functions also related to the string class, which are defined in the C++ standard library. And they are actually very useful. So, here is an example of a simple program that uses the string class. Note that I have said hash include IO stream and also hash include string. I have used using namespace std here. And here is the main function in which I have defined two objects s1 and s2 of class string. Both of these have been initialized by the constant strings hello space for s1 and world exclamation space for s2. And then I have declared a third object s3 of class string and assigned it the value of the string s2. So, here is a string assignment. And as a result, s3 will now represent the string world exclamation space. And after that, I say, please print out the string s1 followed by a space followed by string s3. So, what is going to get printed out over here? String s1 is hello. So, I will be basically printing hello. And after that string s1 also contains a space. So, I will print a space and then string s3 is world. So, I will print world followed by an exclamation followed by a space. And after that, you will be the end of line. So, that is what is going to get printed by this see out statement. How do we read input strings in this program? What I have done is here is a string s1 which is assigned this constant string, please type in your name colon space. And then I have a string s2 which is declared here. I print out the string s1. So, basically you will see this message, please type in your name on your screen, on your console. And then I am using c in to read in s2. And then I am going to print out the name that is read in by saying your name is colon space s2. Now, suppose when I am trying to read in this name s2, I provide the following as input. Suppose I say that the input that I want to give is ajith trivedi. So, note I have a space between ajith and trivedi. Now, what c in is going to do is, it is actually going to read the string ajith. And when it sees this white space over here, when it sees this blank space over here, it is going to stop reading and it is going to assign the string ajith to s2. So, you should be aware of this when you are using c in to read in strings that c in is actually going to read the string until it encounters the first white space character. It is going to stop there and it is going to assign the value ajith to the string variable s2. So, as a result, what is going to get printed out by the cout statement, it will only print your name is colon space s2, but s2 is ajith. So, it will basically print your name is colon space and then ajith. Note that it is not going to print your name is ajith trivedi. Here is another way of reading input strings. This is the get line function. So, here I have the same program as before, except that instead of saying c in greater than greater than s2, I have now used get line c in s2. What this is going to do is, it is actually going to read the entire line until it sees the end of line and put that entire line along with any white space characters that might be there in it in the string s2. So, suppose in this case, I provide the following input when I am trying to execute get line c in s2. So, suppose now I say ajith trivedi. Let us say this is what I type in. Now, get line is going to read this entire line ajith space trivedi and suppose I hit enter or basically have end of line here after trivedi. So, get line c in s2 is actually going to read in the entire line ajith space trivedi up to the end of line and it is going to assign that entire string including the blank space character to the object s2 of type string. So, as a result when I try to print the name out here, this is what it is going to print. Your name is colon space ajith space trivedi. So, you see the difference between reading in strings directly through c in and through get line. Here is another function that you can use with string objects. In this program, I have two string objects s1 and s2 which are initialized respectively to hello space and world and now I use the plus operator on these strings. What does the plus operator do? This is actually the concatenation operator for strings when you are using the string class. So, here this will concatenate s2 to s1. So, I will get hello space space world and then to that will get concatenated this constant string of three exclamation marks. So, when I try to print out the string, what will I see over here? I will basically see hello one space coming from string s1, another space coming from string s2 and then world and then three exclamation marks. So, you see that the plus operator is basically an overloaded operator and in the context of the string class, it is actually serving the purpose of the concatenation operator. Now, there is another way to append or concatenate one string to another. So, this is using the member function append of the string class and I have shown an illustration of that here. So, this is an object s of the string class and note that I have not initialized it to anything. So, by default, this will get initialized to the empty string. Here is a string s1 initialized to hello space string s2 is initialized to space my space world three exclamation marks and then I execute the statement s dot append s1. What this does is it takes the string s which is the empty string and it appends the string s1 to it. So, after I have done this, what is the string in s? It is simply hello followed by a space after that I execute this statement which is s dot append s2 comma 4 comma 5. So, what I am saying is take the string s which is hello space now and to that append the string s2, but do not append the whole of s2, start from the character in the fourth position of the string s2 and starting from there simply append 5 characters. So, this 4 is the starting position and this 5 is the length of the string to be appended. So, now if I look at the string s2, what is the fourth character here? The blank is the zeroth character. Note that a string is like an array of characters. So, the first character there is the zeroth character at the zeroth position m is at the first position, y is at the second position. This space is at the third position and at the fourth position we have w. So, we must start from here and then we must concatenate the next 5 characters. So, w, o, r, l, d. So, starting from w, I am going to concatenate the next 5 characters. So, that is world. So, what is the value of the string s over here after the statement is executed? It is hello space w, o, r, l, d. And finally, I am going to append the constant string 3 exclamations to it. So, s already has the value hello space world. So, after this append it will get the value hello space world followed by 3 exclamation marks. And finally, when I try to print out s, that is what I will print out hello space world 3 exclamation marks. Here is another operator of the string class. This is the square bracket operator used to access specific characters in the string. So, in this program s1 is the string hello followed by 4 spaces followed by w, o, r, l, d followed by 3 exclamation marks. And here I have a simple conditional statement which says if s16 is blank and s17 is blank. So, note that I am applying the square bracket operator just like accessing an element of an array on the string s1. And what this is going to do is it is going to give me access to the character at position 6 in the string s1 viewed as an character array. So, this is saying if the character at the 6th position and the 7th position are both blank, then I am going to replace the character at the 6th position with m. I am going to replace the character at the 7th position with y. So, in this case this is the character at the 0th position. This is the character at the 1st position, 2nd, 3rd, 4th. Then as I said there are 4 spaces here. So, the 5th character is blank, the 6th is a blank, the 7th is a blank and the 8th is a blank. So, both the 6th and 7th characters are blanks. So, they will get replaced by m and y respectively. And then when I print out s1 what will I see? I will now see hello space m y space w o r l d followed by 3 exclamation marks. So, this is one mechanism by which you can actually access the characters in a string just like a character array. Note however that the string class is not a character array, it is a class. This is an overloaded operator for the string class which allows you to access the character at the 6th position if you had to treat this as a character array. Here is another operator called the at operator which is very similar to the square bracket operator that we just saw. So, I could have written the previous program also in this manner saying if s1 dot at 6 which means the character at the 6th position in s1 viewed as a character array. So, if s1 dot at 6 is blank and s1 dot at 7 is blank then s1 dot at 6 should be assigned m and s1 dot at 7 should be assigned y. So, of course, this is also going to print the same string hello space my space world followed by 3 exclamation marks. However, note that there is a subtle difference between using the square bracket operator to access a particular character at a particular position in a string and using the at operator to do that. So, for example, in this string suppose I were to use somewhere in my program s1 square bracket 100 and let us say in another version of the program instead of using the square brackets I used s1 dot at 100. Now clearly this string does not have 100 characters. So, when I use s1 square bracket 100 it is actually going to try to access a memory location that does not belong to the string. So, this may result in an illegal memory access or it may result in accessing garbage and you will not know that if you are trying to access that directly using square brackets. For example, it may print out some garbage value and you would not know whether that was really part of the string or it was actually a garbage value. On the other hand if you use the at operator and if you try to access the 100 position in s1 treated as a character array then because the size of s1 is less than 100 when you try to access the character the 100 position using at at runtime you will get what is called an out of range exception. So, you will know immediately that you are trying to access an array beyond the legal range that is allowed for that array. In this case of course it is a string, but you are trying to access the string at a position which is beyond the length of the string. So, you see that there is a certain difference between these two using this you cannot make out whether you are accessing garbage or you are accessing some actual legal character within the string whereas using this at runtime you will immediately get to know whether you have gone out of range of the size of that string. Now here are some other useful member functions these are called find and are find. So, here is a simple program where I have initialized the string s1 to hello space world space hello followed by three exclamation marks. And now if I say s1.find hello what it will do is it will look at the string s1 and it will find the first occurrence of the string hello in the string s1. So, that occurs right here at the 0th character of s1. So, it will assign 0 to the integer variable i on the other hand if I try to say s1.find hi now this string hi does not appear anywhere in the string s1. So, then what this will return is also what is called an n pos or a no pos this is basically a value that can never be a valid index for example it could be a negative value. So, whenever you are invoking the member function find it is a good idea to check whether whatever has been returned is n pos which is of course defined in the string class. So, we have used the scope resolution operator here string colon colon n pos. So, whenever you use find please check after that whether the result is string n pos because if it is it means that the string that you are trying to find does not exist in s1 otherwise you will get the index of the string that you are trying to find within the string s1. So, for example here if I try to print out i comma j for i I will get 0 because that is the place where I find hello first, but for j i will probably get a negative number because this is supposed to be a value that can never be a valid index. So, depending on your compiler this j will get probably printed out as minus 1 or some other negative number here is another version of find this is called r find. So, this is basically finding from the string starting from the end of the string. So, r find hello is reverse find hello. So, it is going to find the first occurrence of hello coming from the end of the string. So, that is over here and that in this case turns out to be the 12th position. So, hello is 5 letters. So, this is position 4 then this is position 5, 6, 7, 8, 9, 10, 11 is the blank and this h is the 12th position. So, here I will get the value 12 and of course j will get the value n pos and so when you try to print it out you are likely to get a negative number here most likely minus 1. You can also extract substrings from a given object of the class string. So, for example, here is our same string hello world hello followed by 3 exclamation marks. Now, if I try to print this out what I am saying is find the substring of s 1 starting from position 6 until the end of the string. So, what is that position 6? This is position 0, 1, 2, 3, 4, 5 and that is position 6. So, basically starting from w print the string until the end of the string. So, it is first going to print. So, it is first going to print world space hello followed by 3 exclamation marks. That is because I have asked it to print the substring of s 1 starting from position 6 then it will print a space. So, there will be one space after this and after that I am asking it to print a substring of s 1 starting from the position 0, but this substring is only 5 characters long. So, it start from the position 0 and I will only print 5 characters. So, I will print hello. So, there will be a after the space here I will print again h e l l o and so that is the string that will get printed out when I execute c out over here. Now, when you are using the standard library in c plus plus you will very often have occasion talk about iterators and iterator is an object that points to an element in a collection of elements and it can be used to iterate through the elements in the collection. So, it is like a pointer because it points to an element, but it is not exactly the same and so we will not call it a pointer we will correctly call it an iterator and an iterator must support the plus plus or increment operator which can basically be used to iterate through the elements in the collection and it must also support the dereferencing operation. So, that we can go ahead and see what is the element that it is pointing to. So, as far as the string class is concerned you have some interesting iterators here. So, for example, you have this begin and end member functions and here in this program what I have done is I have declared an object s 1 which is a string and I have initialized it with this constant string high space there followed by exclamation mark and now in this for loop I have declared it to be an iterator of the string class and I have assigned it to s 1 dot begin. So, what this does is it basically gives me an iterator which points to the starting character of the string and what am I going to do in this for loop I am going to do it plus plus. So, that allows me to iterate through the different characters in the string and how long until I reach s 1 dot end. So, s 1 dot end gives me the iterator which is after the last character in the string. So, therefore, until I have reached s 1 dot end I can print out star it basically I am printing out the characters in the string 1 by 1 starting from h s 1 dot begin will make it point to h. So, when I print out star it I will basically get h then I will print out I when I do it plus plus the iterator points to i. So, when I print star it I will get i then I will get space then I will get t h e r e exclamation mark after that when I increase it by saying it plus plus it actually matches s 1 dot end. So, s 1 dot end will return an iterator which is pointing to just after this exclamation mark just after the last character in the string has been examined and therefore, at that point we stop and we come out of the for loop. So, what is this program going to print program is going to print exactly the string s 1 as we have seen here. So, I will basically get high space there now the string class also provides two other interesting iterators called r begin and r end. So, these are actually what are called reverse iterators r begin gives you an iterator that points to the end of the string and r end gives you an iterator that points to the beginning of the string. So, here is the same program that we had seen earlier, but now I am using a reverse iterator of the string class. So, I have called it r i t for reverse iterator and I initially assign it to s 1 dot r begin. So, this is actually going to point to the last character in the string which is the exclamation mark and then as I increment this reverse iterator it is actually going to come towards the beginning of the string. So, note that I am incrementing a reverse iterator that is why I am going in reverse and coming towards the beginning of the string and I will do that until I see s 1 dot r end s 1 dot r end will return an iterator that points just before the first character of the string. So, it means that I have now traversed the entire string in reverse direction and here I am of course printing out star i t I am printing out whatever character r i t points to. So, what is the string that we expect to be printed over here. So, here we expect to see first an exclamation then e then r then e then h then t then a space then i and then an h. So, we basically printed the string in reverse. So, in summary in this lecture we had a brief glimpse of the C plus plus standard library and we also studied in a bit more detail about a particular class in this library called the string class. Of course, many more member functions exist in the string class than what we have studied and you strongly encouraged to read them up on your own. It is a very useful class to use in your C plus plus programs. Thank you for your attention.