String Iterator.

Shibaji Paul
A free video tutorial from Shibaji Paul
Programming Instructor with 16+ years of experience
4.4 instructor rating • 7 courses • 25,199 students

Lecture description

You will get the concept of Iterator and how to use the iterator to traverse the string.

Learn more from the full course

C++ programming step-by-step: From Beginner to Advanced

Start with basics of C++ (v11 and v14) then master File I/O, Object Oriented Programming or OOP, STL, Functors, Lambda

27:29:05 of on-demand video • Updated June 2020

  • On completing the course you will have firm grip on C++ language. You will understand the object oriented programming OOPs concept in depth.
  • You will understand the basic programming structures like branching and looping.
  • You will understand details about pointers and dynamic memory allocation along with memory management techniques.
  • You will understand all about functions, parameter passing by value and by reference.
  • All about user defined types using struct and class. Constructor function, method overloading and dynamic polymorphism.
  • You will learn operator overloading, friend functions.
  • In depth understanding of Inheritance and dynamic polymorphism, how runtime polymorphism helps to write more flexible and dynamic code.
English [Auto] OK so far we have understood how to traverse that string using a for loop. In the last tutorial I showed you that in the tutorial Let me show you how we can use the Aitu letters for traversing the string. Now I iterators having a special place in C++ that the objects that helps us to iterate through a collection are ranges of elements. For example there's a string that's a collection of characters like that we will have several collection classes going through like a stack. Q Are vectors. I'll be actually demonstrating each of them later on. This string is also a collection that's the collection of characters. We say that ranges of characters are there. Right. So whenever you need to traverse through ranges of elements you can use Twitter for that collection object in order to iterate through the elements of the collection. Let's go in and see how we can do that using iterator for the strings. Let's go ahead and see. OK. First of all we need to declare the item later and that we do in this way string column called on Twitter. And when you do give a name for this I Twitter and naming it as I see. So this iterator is actually belonging to the string type. So this is the I created for the string. Now that column column is typically called scope resolution operator that results S-Corp. So for this what we are telling the compiler this iterator ID is actually the eiter for String objects. Now if you want to get an iterator for vector objects obviously you are going to do vector column called an iterator. So that is actually the scope resolution operator that builds for which scope this particular element exist are belongs to. Now we need to get the I treated forta string SDR. So far that we can call your dog to begin with and begin with what is going to get you to igitur which is right now placed at the beginning of the string. Let me show you by drawing that image there. So we have that halo for string as you are. And that is inside the object is to your right. So when you are actually calling the begin method is returning an iterator that is placed at the beginning. After that is the first character of the street so he is right not placed here. The first character. Now when you call that a start but any method that is the other method that we use with the trickers. Now that is geotagged end is going to return us and I Twitter that is past the last character. That is just after the last character. So if you're calling ISDE or dot and that's what DO. Tony you and I Twitter that is placed here after the last character of the string. So if you want to traverse the string using an iterator you need to go on checking whether the current actuator is at the end or not. So if you go ahead and copy the code we do in this way. I just write a while loop there and we need to iterate as long as I use not equals pastry are not. And so that's where the test where the idea is passed the last character or not. Because as you are taught it is going to return again. And I Twitter. And that is that is going to be placed after the last character. So as long as I-T are cut and dry to return it's not at that position after the last character we have which I tripped through this loop and we are interested of printing individual characters. They're using the iterators so we write in this way Star of I-T and that's that put in sync operator used with iterators to get the content on the element where I read this placed right now don't mix up this with pointers. Now traders are not pointers but is still dependent seeing operator works with I Tritter because that has been overloaded. The of the star is Judaea financing operator has been overloaded for the I treated class. Now what is overloading of operator that we will get to know when we actually do the operator overloading later on. Just leave it for the time being just this information is going to be sufficient. I guess that using the dereferencing operator with the creator is going to get us that element where the item is placed right now. So now we're going to get individual characters printed they're separated by commas for this string here which is actually containing. Hello there. Let's go in and see this and how I'm sorry it's actually stuck in infinite loop because we have not actually moved to the i director right now. When I Twitter we get the idea that we're here is actually placed at the beginning of the stream. Then we rented the first character there but we have not moved it to the next character and that we can do using the plus plus operator C++. ID is going to move to Twitter to the next location. So now when we apply that plus plus that ID which was placed at age now will moved will be moved here at the now when it comes here. Again this condition will be tested. ID is not at the end. It can even go inside the loop and it's going to print. And then Ill Oh and after that when it comes here the loop is going to terminate. So that is how it is going to work is going and see once more I'll run the program and here it is you can see that h e l l are separated by almost the last Halo is actually printed here. Now if you want to convert all those Lorqess letters off the string to corresponding uppercase you can use it you can use the I treat it as well for doing that. Let me show you that as well. So I'll just comment on this. Now we need to decide whether the current character is a lock it's character or not. So if a star is at the Nichol's a and star I-T is less than equal said then obviously if Salukis character had what we need to do is to substract 32 from the current character there. So T minus equals 32 is going to do good. Now each and individual characters which have in Lorqess will be converted to equivalent uppercase. Now when you read this last year here you're going to get all the characters in the string hello in uppercase as quite insidious characters. Now instead of using these two letter which starts from the beginning you can use the reverse iterator and reverse idolator actually going to get you the last character when you call the R begin. Let me show you that I've just come into this and I'll show you how the Riverside Theater Works. And if you want to traverse the stream from the starting from end until you come to the beginning of the string that means that the reverse direction. Then you can use the reverse trigger. And you can declare the Riverside Theater in this very strange called on Column V verse. I took it. Let me guess R I T. And in order to get the verse I'd read it or you can call it here. Dot r picking. Now the R begin is going to get you the reader which is placed at the last character of the string. OK. So RB King will place Riverside later here. You are right he is right now here. Now when we will increase the R I T is going to be moved in the reverse tradition it'll come here when it'll do plus plus I. Now please note that it's not minus minus. Still we are going to use the same plus plus operator in order to increase because the trigger itself is Riverside's. So when you increase the iterator it's going to move the forward in the reverse direction in the opposite direction. So we are going to use that plus plus OPERATOR Now when you apply that it will be moved in this way in this reverse direction it'll come here. Then here then here then here then it will come here. Now when you are going to call our. I'm sorry when you are going to call it. Here are the screen are in and that's going to be turn. And I Twitter that is placed previous to the first character. OK. So the idea that get by calling this our method using the string object is going to be placed prior to the first character just one position pract the first character. OK let's go out and see that how we can use that Veevers I created for traversing the entire string in the opposite direction. So we get the reverse I created here by calling our picking. Now we need to treat our id not equals as you are not are and reverse and as long as the Riverside theatre is not at first and we were saying is that previous to the first character as I say. So we need to print that. Got it. Got it. They're separated by common OK. And then we need to increase the rate at their plus plus. That's going to move the reader into reverse tradition. As I said let's go out and run this so see that is versus string. So that's all the Riverside Theater Works. Now if you want to use the Riverside editor reader in order to convert each of the Lucas literate to abacus literally you can do in the same way as I did here. Now the logic is going to even say that. So that's all for this I hope you have understood how we can use the traitors for traversing this tree or any collection. Please note that for the other collections that begin method and method order and beaking or odd it works in the same way that name and amateur's to sane same for the other collections when we'll be doing the pictures. Then also we are going to use the begin method in order to get the I created for the big vector list at the very beginning of the vector. I mean the first element of the vector. So that's all for this. Thank you for watching.