Computer Science | Data Structures #4 | Iterators

Опубликовано: 03 Июль 2026
на канале: IDrumsey
389
7

Transcript -
welcome back to episode 4 of our data structure sub series so in this video we're going to talk about iterators and iterators are a programming concept that are used with data structures so we need to look at at least at at least one data structure before we could talk about iterators so an iterator is pretty much just a variable that iterates through each position in a container which whatever data structure you're using or container anyways an iterator basically takes on the value of each position in that container and we can use that in our loops so anyways what we have been doing is creating it for loops creating a counter setting that's to 0 and then checking whether that counter was less than the size of the array and by the way and we're working in C++ right now and C++ we don't have anything like Java where you can say it my array that length or whatever because it's not an object of a class but we do have a way to get the size so we can just use the size of method and then just pass in that array like that and that'll do the same thing and then we can go ahead and increase the I for each iteration through the loop and now we can go ahead and print out a my array at that counter whatever the counter is so there we go and we can end that and there we go so that just print out each letter now with iterators in a row like I said is just pretty much a variable so we use it in the for loop you can probably use it in the other loops I'm not really sure on that but anyways we can say the type so we're looping through char value so when you are itter to be a char because it's taking on the value of those positions so I usually just call it the first letter of that data types of C in this case and then you put the colon and then whatever container you or data structure you want to be looping through so we're looping through the array and now we can go ahead and do the same thing we're just going to print out instead of doing my array at that counter we're just going to say see because it's taking on any value at each of those positions and then we'll just print out the new line like that and now we should see both of these things doing the same thing so let's go ahead and compile this with g plus plus C and then main.cpp and then we'll go ahead and link it up by saying run me DT for executable and then made the O and we'll go ahead and run it here and there you can see it's running out the same thing so let's look talk about the advantages and disadvantages so with the actual for loop with the counter you can actually use this counter to do arithmetic so for whatever reason if you wanted to like add numbers in here or whatever you could do that and get different positions within the array with the iterator you're just going sequentially through each position one by one and until there's no more positions and it's taking on those values so you can't really use a counter I mean you could do something like this you could say a counter outside of the actual for loop and say set to zero and then increase it in here by saying like I plus plus you could do that but that's kind of troublesome it's just more code anyways another advantage with the counter is you can actually go backwards so we could say this is equal to the size of the my array and then we could say wow I is let's see we'd be decrementing this so it would be zero while I is well it'd be greater than negative one because we start counting from zero so maybe one and now that we could actually just go backwards through there through the array so let's just get rid of that and now if we run this we should see boys keep being printed out backwards so let's recompile we're and rerun and you can see it's being printed out backwards and then forwards so with the iterator you can't do that it just goes forwards through an array and then the Vantage's for using iterators basically it comes down to not having to write as much code with the actual counters you had to write out like my array at the counter index so that was kind of it's kind of like I know it's not a lot of code but over time when you're writing a lot of for loops the iterate just make your life a lot easier it is a disadvantage not being able to change what index you're at but if you're just trying to print through sequentially through our container then yeah the editors are definitely a easy way to do that and it comes out to less code okay and then one more thing we can do with iterators is if we don't if we want if we want to make this kind of generic we can just use this keyword called Auto and this will just detect what type of iterator who are going to be using for that container so in this case we're iterating through this array and this array stores char value so this is going to be a chart iterator so that's pretty cool it's basically just Auto detects what type of iterator you're using