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.

Random access iterators

Random access iterators - C++ Tutorial

From the course: C++ Templates and the STL

Random access iterators

- [Instructor] The random access iterator is the most complete iterator of 'em all. It may be used to access any element at any position in a container. This iterator offers all of the functionality of a C pointer. Here I have a working copy of ra-iterator.cpp where ra stands for random access. From chapter three of the exercise files, here we'll be using the vector container because it's a container that allows random access and uses a random access iterator. Here I construct a vector of ints with 10 elements, one through 10, and a iterator object from the vector of ints class. And of course, this iterator can do everything a forward iterator can do. It can do everything a bidirectional iterator can do. The range-based for loop works. All of that works plus we may access elements directly. So, here I take an iterator, I set it to begin and I add five to it and it will get us the element at zero plus five position. Which would be, if this is zero, one, two, three, four, five, it would…

Contents