From the course: C++ Templates and the STL
Unlock this course with a free trial
Join today to access over 22,600 courses taught by industry experts.
Bidirectional iterators - C++ Tutorial
From the course: C++ Templates and the STL
Bidirectional iterators
- [Instructor] The bidirectional iterator may be used to access the sequence of a container in either direction. Here I have a working copy of bidi-iterator.cpp, where bidi stands for bidirectional, from chapter three of the exercise files. The set container type uses a bidirectional iterator. It can be iterated either forward or backward. And so here we have a set, we've initialized it with integers from one through 10, and we have an iterator object for the set, and you'll notice I have a simple for loop. An interesting thing about the end member function in all of these containers that have an end member function is that it returns an iterator that points past the end, and it does that specifically so that these for loops work the way that you expect. But when we're iterating backward, it does not do what we expect, because this end is past the end, and begin is not past the beginning, it's right at the beginning, and so what we need to do here, we can't put our decrement in that…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.