codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚡

C++ Programming

73 / 87 topics
62Introduction to STL & Containers63std::array64Vectors65List & Forward List66Deque67Stack68Queue & Priority Queue69Map & Multimap70Set & Multiset71Unordered Map & Unordered Multimap72Unordered Set & Unordered Multiset73Iterators74Algorithms75Functors
Tutorials/C++ Programming/Iterators
⚡C++ Programming

Iterators

Updated 2026-05-12
30 min read

Iterators

In the world of programming, especially when dealing with collections of data, iterators are like the eyes that allow us to traverse through each element without directly accessing their indices. In C++, iterators provide a standardized way to access elements in containers such as vectors, lists, and maps. Understanding iterators is crucial for efficient and safe manipulation of STL (Standard Template Library) containers.

In this tutorial, we will explore various types of iterators, how to use them, and some useful functions that work with iterators. By the end of this section, you'll be comfortable using iterators in your C++ programs.

Iterator Categories

Iterators are categorized into five main types based on their capabilities:

  1. Input Iterators: Can only read from the container. They can move forward one element at a time.
  2. Output Iterators: Can only write to the container. They also move forward one element at a time.
  3. Forward Iterators: Support reading and writing, and can move forward one element at a time.
  4. Bidirectional Iterators: Support reading and writing, and can move both forward and backward one element at a time.
  5. Random Access Iterators: Support reading and writing, and can jump to any position in the container using arithmetic operations.

Understanding these categories helps in choosing the right iterator type for specific tasks.

begin() and end()

The begin() and end() functions are fundamental when working with iterators. They return iterators pointing to the first element and one past the last element of a container, respectively.

Example

iterator_begin_end.cpp
1#include <iostream>
2#include <vector>
3
4int main() {
5 std::vector<int> vec = {10, 20, 30, 40, 50};
6
7 for (auto it = vec.begin(); it != vec.end(); ++it) {
8 std::cout << *it << " ";
9 }
10
11 return 0;
12}
Output
10 20 30 40 50

In this example, vec.begin() returns an iterator pointing to the first element (10), and vec.end() returns an iterator pointing one past the last element. The loop iterates through each element until it reaches the end.

advance()

The std::advance() function is used to move an iterator a specified number of positions forward or backward.

Example

iterator_advance.cpp
1#include <iostream>
2#include <vector>
3#include <iterator>
4
5int main() {
6 std::vector<int> vec = {10, 20, 30, 40, 50};
7 auto it = vec.begin();
8
9 std::advance(it, 2); // Move iterator to the third element
10
11 std::cout << *it << std::endl; // Output: 30
12
13 return 0;
14}
Output
30

In this example, std::advance(it, 2) moves the iterator two positions forward from the beginning of the vector, pointing to the third element (30).

distance()

The std::distance() function calculates the number of elements between two iterators.

Example

iterator_distance.cpp
1#include <iostream>
2#include <vector>
3#include <iterator>
4
5int main() {
6 std::vector<int> vec = {10, 20, 30, 40, 50};
7 auto start = vec.begin();
8 auto end = vec.end();
9
10 int dist = std::distance(start, end);
11
12 std::cout << "Distance: " << dist << std::endl; // Output: Distance: 5
13
14 return 0;
15}
Output
Distance: 5

In this example, std::distance(start, end) calculates the number of elements between the beginning and the end of the vector, which is 5.

Reverse Iterators

Reverse iterators allow traversal through a container in reverse order. They are particularly useful for operations that need to process elements from the end to the beginning.

Example

iterator_reverse.cpp
1#include <iostream>
2#include <vector>
3
4int main() {
5 std::vector<int> vec = {10, 20, 30, 40, 50};
6
7 for (auto it = vec.rbegin(); it != vec.rend(); ++it) {
8 std::cout << *it << " ";
9 }
10
11 return 0;
12}
Output
50 40 30 20 10

In this example, vec.rbegin() returns a reverse iterator pointing to the last element (50), and vec.rend() returns a reverse iterator pointing one past the first element. The loop iterates through each element in reverse order.

Const Iterators

Const iterators prevent modification of the elements they point to. They are useful when you want to ensure that the container remains unchanged during iteration.

Example

iterator_const.cpp
1#include <iostream>
2#include <vector>
3
4int main() {
5 std::vector<int> vec = {10, 20, 30, 40, 50};
6
7 for (auto it = vec.cbegin(); it != vec.cend(); ++it) {
8 // *it = 100; // Error: cannot assign to const iterator
9 std::cout << *it << " ";
10 }
11
12 return 0;
13}
Output
10 20 30 40 50

In this example, vec.cbegin() and vec.cend() return const iterators. Attempting to modify the elements through these iterators would result in a compile-time error.

Practical Example

Let's create a complete program that demonstrates various iterator operations: using begin(), end(), advance(), distance(), reverse iterators, and const iterators.

iterator_practical.cpp
1#include <iostream>
2#include <vector>
3#include <iterator>
4
5int main() {
6 std::vector<int> vec = {10, 20, 30, 40, 50};
7
8 // Using begin() and end()
9 for (auto it = vec.begin(); it != vec.end(); ++it) {
10 std::cout << *it << " ";
11 }
12 std::cout << std::endl;
13
14 // Using advance()
15 auto it = vec.begin();
16 std::advance(it, 2);
17 std::cout << "Element at position 2: " << *it << std::endl;
18
19 // Using distance()
20 int dist = std::distance(vec.begin(), vec.end());
21 std::cout << "Distance between begin and end: " << dist << std::endl;
22
23 // Using reverse iterators
24 for (auto it = vec.rbegin(); it != vec.rend(); ++it) {
25 std::cout << *it << " ";
26 }
27 std::cout << std::endl;
28
29 // Using const iterators
30 for (auto it = vec.cbegin(); it != vec.cend(); ++it) {
31 // *it = 100; // Error: cannot assign to const iterator
32 std::cout << *it << " ";
33 }
34 std::cout << std::endl;
35
36 return 0;
37}
Output
10 20 30 40 50 
Element at position 2: 30
Distance between begin and end: 5
50 40 30 20 10 
10 20 30 40 50

This program demonstrates the use of various iterator operations, providing a comprehensive overview of how iterators can be utilized in C++.

Summary

ConceptDescription
Iterator CategoriesInput, Output, Forward, Bidirectional, Random Access
begin() and end()Return iterators to the first and one past the last element of a container
advance()Moves an iterator a specified number of positions
distance()Calculates the number of elements between two iterators
Reverse IteratorsAllow traversal in reverse order
Const IteratorsPrevent modification of the elements they point to

What's Next?

With a solid understanding of iterators, you are now ready to explore Algorithms. Algorithms provide a set of functions that operate on ranges defined by pairs of iterators. This combination of iterators and algorithms is what makes C++ so powerful for processing collections of data efficiently.

In the next section, we will delve into various algorithms available in the STL, how they work, and how to use them effectively. Stay tuned!


PreviousUnordered Set & Unordered MultisetNext Algorithms

Recommended Gear

Unordered Set & Unordered MultisetAlgorithms