Menu
Coddy logo textTech

Iterators

Lesson 13 of 23 in Coddy's C++ - Standard Template Library course.

In this chapter you will learn about the different types of iterators available in the STL. 
Iterators are used to access data in the containers, it represents a generalized pointer identifying a position in a container. More precisely, an iterator is an object that points to an element inside the container.

They can be incremented and decremented as per choice and help in the manipulation of data in the container.

We can use iterators to move through the contents of the container, they can be visualized as a pointer pointing to a location in the container and we can access the content at that particular location using them. 
Iterators have a huge role in connecting the containers of the STL with the algorithms that you'll learn later in this course.


The C++ STL implements five different types of iterators:

  • Input Iterators (used to read a sequence of values)
  • Output Iterators (used to write a sequence of values)
  • Forward Iterators (the can be read, written to and move forward)
  • Bidirectional Iterators (like forward iterators, but can move backward)
  • Random-Access Iterators (can move freely any number of steps)

Iterators are most often returned from a function or a method. The function or the method has a return value which is an iterator.

Iterator functions are:

  • begin() - Points the iterator to the first element of the container
  • end() - Points the iterator to the last element of the container

Try it yourself

This lesson doesn't include a code challenge.

All lessons in C++ - Standard Template Library