Standard Template Library (STL) in C++ | Iterators

Getting started with STL in C++

Tanishq Vyas
4 min readJun 19, 2020
Photo by Safar Safarov on Unsplash

Iterators

Iterators are something which is used to point at the memory addresses of STL containers’ elements. Thus providing a means to access the data stored within the container and ability to modify them. They can be simply thought of as pointers.

Why Iterators ?

We need iterators in order to manipulate the containers. Containers on their own cannot be modified unless we make use of an iterator or a member function. The purpose is to isolate the user from the internal structure of the container thus helping with abstraction.

They provide a bridging or a connection between the algorithms and containers by allowing the algorithms to operate on the containers via them.

With that being said let’s proceed further to know what types of iterators do we have.

Iterators & their types

  • Input iterators: They can be used in sequential input operations, where each value pointed by the iterator is read only once and then the iterator is incremented. All forward, bidirectional and random-access iterators are also valid input iterators.
  • Output iterators: These iterators are iterators that can be used in sequential output operations, where each element pointed by the iterator is written a value only once and then the iterator is incremented.
  • Forward iterators: These are the iterators that can be used to access the sequence of elements in a range in the direction that goes from its beginning towards its end.
  • Bidirectional iterators: These are iterators that can be used to access the sequence of elements in a range in either directions i.e. forward as well as reverse direction.
  • Random access iterators: These type of iterators provides us with an access to an element at an arbitrary offset from the location the iterator is currently pointing to. This type of iterators are the most complete type of iterators.

More can be read about these iterators on this page.

Syntax

Let’s see how do we declare an iterator

Assuming that we have a container of a given data-type, we create an iterator for the same.

Syntax : container <data-type> :: iterator iterator_name;

Eg: list <int> :: iterator it;

This, what we declared above is a forward iterator. Let’s see how do we declare a reverse iterator.

Syntax : container <data-type> :: reverse_iterator iterator_name;

Eg: list <int> :: reverse_iterator it;

We just need to change iterator to reverse_iterator.

Iterator Member Functions

Let’s have a look at different iterator functions that are available for us to play around with in C++ for all different containers.

  • begin(): This function returns an iterator pointing to the first element of the given container.
list <int> v{1, 2, 3, 4};list <int> :: iterator it = v.begin();cout<< *it<<"\n";Output:
1
  • end(): This function returns an iterator pointing to the block one after the last element in the given container.
vector <int> v{10, 20, 30, 40, 50, 60};vector <int>::iterator it;it = v.begin() // Assigning the iterator to itcout<<*it <<" "<< *(it -1)<<" "<<endl;Output:82792 60  // notice that we get some junk value for position it but  //the last element's value i.e. 60 for it-1
  • rbegin(): This is similar to begin but the only catch is that it returns a reference to the last element of the container, while treating the container as if it has been reversed in order. When the value of it is incremented, it keeps on shifting to the next element in the sequence considered in reverse order.
list <int> v{1, 2, 3, 4};list <int> :: reverse_iterator it = v.rbegin();cout<< *(it+1)<<"\n";Output:
3
  • rend(): This is similar to end but the only catch is that it points to the block one after the last element and upon incrementing it keeps pointing to the element which is next in the sequence which is considered in reverse order.
list <int> v{1, 2, 3, 4};list <int> :: reverse_iterator it = v.rbegin();cout<< *(it+1)<<"\n";Output:
4

We have 4 more functions, namely cbegin(), cend(), crbegin() and crend(). These functions return an iterator which does the same job as the previous corresponding iterators but with a restriction that it cannot be used to modify the contents of the container in any way possible. For more information one can refer this page.

AUTO KEYWORD

Instead of typing out the whole syntax we can make use of auto keyword

list <int> :: iterator it = v.begin();

This can also be written as ( for both iterator and reverse_iterator)

auto it = v.begin();

With that we have come to an end of this introduction to iterators in C++. More about these iterators can be read on this page. There are many other functions one can explore in context of iterators. We will have a look at other concepts of STL in upcoming articles. Till then keep learning : )

--

--