An iterator in C++ is a pointer-like object that points to an element of the STL container. They are generally used to loop through the contents of the STL container in C++. The main advantage of STL iterators is that they make the STL algorithms independent of the type of container used. We can just pass the iterator to the container elements instead of the container itself to the STL algorithms.
In this article, we will learn about iterators in C++, their types, and how to use them.
Iterator Declaration
Each container in C++ STL has its own iterator. So, we have to declare an iterator as:
<type>::iterator it;
where,
- type: Type of container for which the iterator is declared.
- it: Name assigned to iterator object.
We can then initialize it by assigning some valid iterator. If we already have an iterator to be assigned at the time of delcaration, then we can skip the type declaration using the auto keyword.
auto it = iter
where, iter is the iterator assigned to the newly created it iterator.
Our C++ Course covers the use of iterators in the STL, ensuring you understand how to traverse different container types.
Example of Iterators
The below program illustrates how to use the iterator to traverse the vector container:
C++
// C++ program to show how to use iterator to
// traverse vector container
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> arr = {1, 2, 3, 4, 5};
// Defining an iterator pointing to
// the beginning of the vector
vector<int>::iterator first =
arr.begin();
// Defining an iterator pointing to the
// end of the vector
vector<int>::iterator last =
arr.end();
// Iterating the whole vector
while(first != last) {
cout << *first << " ";
first++;
}
return 0;
}
As you may have noticed, we have used vector::begin() and vector::end() function. These functions are the member functions to std::vector that returns the iterator to the first and one element after the last element of the vector. We use the iterators return be these functions to iterate the vectors.
Container Iterator Functions
C++ STL provides some member functions in STL container that return the iterators to at least the first and the last element. These member methods are defined in almost all of the STL container (leaving some limited access containers like stack, queue) with the same name for consistency.
The following table lists all methods that returns the iterator to the containers:
Iterator Function
| Return Value
|
|---|
begin()
| Returns an iterator to the beginning of container.
|
end()
| Returns an iterator to the theoretical element just after the last element of the container.
|
cbegin()
| Returns a constant iterator to the beginning of container. A constant iterator cannot modify the value of the element it is pointing to.
|
cend()
| Returns a constant iterator to the theoretical element just after the last element of the container.
|
rbegin()
| Returns a reverse iterator to the beginning of container.
|
rend()
| Returns a reverse iterator to the theoretical element just after the last element of the container.
|
crbegin()
| Returns a constant reverse iterator to the beginning of container.
|
crend()
| Returns a constant reverse iterator to the theoretical element just after the last element of the container.
|
For Example, if vec is the name of the vector, then we can use the above methods as shown below:
vec.begin(), vec.rbegin(), vec.cbegin(), vec.crbegin()
vec.end(), vec.rend(), vec.cend(), vec.crend()
Iterators Operations
Just like pointer arithmetic, there are some operations that are allowed on C++ iterators. They are used to provide different functionalities that increases the importance of iterators. There are 5 valid iterator operations in C++:
- Dereferencing Iterators
- Incrementing/Decrementing Iterators
- Adding/Subtracting Integer to Iterators
- Subtracting Another Iterator
- Comparing Iterators
Dereferencing Iterators
Dereferencing operation allows the users to access or update the value of the element pointed by the iterator. We use the (*) indirection operator to dereference iterators just like pointers.
Syntax
*it; // Access
*it = new_val; // Update
where new_val is the new value assigned to the element pointed by iterator it.
Incrementing/Decrementing Iterators
We can increment or decrement the iterator by 1 using (++) or (–) operators respectively. Increment operation moves the iterator to the next element in the container while the decrement operation moves the iterator to the previous element.
Syntax
it++; // post-increment
++it; // pre-increment
it–; // post-decrement
—it; // pre-decrement
Adding/Subtracting Integer to Iterators
We can also add or subtract an integer value from the iterators. It will more the iterator next or previous position according to the added integer value.
Syntax
it + int_val; // Addition
it – int_val; // Subtraction
where int_val is the integer values that is being added or subtracted from the iterator it.
Subtracting Another Iterator
We can subtract one iterator from another to find the distance (or the number of elements) between the memory they are pointing to.
Syntax
it1 – it2
Comparing Iterators
We can also test the two iterators of the same type against each other to find the relationship between them. We can use the relational operators like (==) equality and (!=) inequality operators along with other relational operators such as <, >, <=, >=.
Syntax
it1 != it2 // Equal to
it1 == it2 // Not equal to
it1 > it2 // Greater than
it1 < it2 // Less than
it1 >= it2 // Greater than equal to
it1 <= it2 // Less than equal to
Types of Iterators in C++
STL iterators can be divided on the basis of the operations that can be performed on them. As such. there are 5 main types of iterators in C++ which as listed in the below table along with supported containers and supported iterator operations.
Iterator
| Description
| Supported Containers
| Supported Operations
|
|---|
Input Iterator
| It is a one-way iterator used to read the values.
| Input Stream
| Dereferencing, Increment, Equality
|
|---|
Output Iterator
| It is also a one-way iterator but used to assign the values. It cannot access the values.
| Output Stream
| Dereferencing (write only), Increment
|
|---|
Forward Iterators
| It can access and as well as assign the values. It is the combination of both input and output iterator.
| forward_list, unordered_map, unordered_set
| Dereferencing, Increment, Equality
|
|---|
Bidirectional Iterators
| It can move in both direction either in forward or backward. The containers like list, set, and multimap supports bidirectional iterators.
| list, map, set, multimap, multiset
| Dereferencing, Increment/Decrement, Equality
|
|---|
Random Access Iterators
| Random-access iterators are iterators that can be used to access elements at distance from the element they point to, offering the same functionality as pointers.
| vector, deque, array, string
| All
|
|---|
As we may have noticed from the above table, apart from the input and output iterators, as we go down the table, the iterator type contains the features of above iterator along with some new features.
Iterator Adaptors
Iterator adaptors in C++ are the special type of iterators that are built over traditional iterators to provide specialized functionality. There are many iterator adaptors in C++ some of which are given below:
Iterator Adaptors Type
| Description
|
|---|
Reverse Iterator
| The reverse iterator is built over bidirectional or above type of operator and allows users to traverse the container in the reverse direction.
|
Stream Iterators
| The stream iterators namely, istream and ostream iterators are built on the input and output iterators respectively. These iterators allow the users to use the streams as containers.
|
Move Iterators
| Move iterators are used to introduce the move semantics in STL algorithms. The move iterators move the ownership of the copied container data to the copying container without creating the extra copies.
|
Inserter Iterator
| The insertor iterators allows you to insert the given elements at some position in the container. There are three insertor iterators in C++:
- back_insert_iterator: Inserts at the back of the container.
- front_insert_iterator: Inserts at the front of the container.
- insert_iterator: Inserts at anywhere in the container.
These iterators can be created using back_inserter(), front_inserter(), inserter() functions in C++.
|
Iterator Utility Functions in C++
C++ STL provide the various function to simplify the working with iterators. They are listed in the below table:
| Function | Description | Syntax |
|---|
| std::advance | Advances an iterator by a specific number of positions. | advance(it, n) |
|---|
| std::next | Returns the iterator that is a specified number of positions ahead of the given iterator. | next(it, n) |
|---|
| std::prev | Returns the iterator that is a specified number of positions behind the given iterator. | prev(it, n) |
|---|
| std::distance | Returns the number of elements between two iterators. | distance(it1, it2) |
|---|
| std::begin | Returns an iterator to the first element of the given container. | begin(container) |
|---|
| std::end | Returns an iterator to the element following the last element of the given container. | end(container) |
|---|
| std::rbegin | Returns a reverse iterator to the last element of the given container. | rbegin(container) |
|---|
| std::rend | Returns a reverse iterator to the element preceding the first element of the given container. | rend(container) |
|---|
| std::inserter | Creates an insert iterator that inserts elements into a container at a specified position. | inserter(container, position) |
|---|
| std::back_inserter | Creates a back insert iterator that appends elements to the end of a container. | back_inserter(container) |
|---|
| std::front_inserter | Creates a front insert iterator that inserts elements at the front of a container. | front_inserter(container) |
|---|
Applications of Iterators with Examples
Iterators are extensively used in C++ for many different purposes while working with STL containers and algorithms. Following are some primary applications of iterators in C++ which their code examples:
Traversing Containers
Traversing STL containers is the most basic application of iterators. In this, we use the begin() and end() functions to get the begin and end iterators to traverse the whole container. Basically, we keep incrementing the begin iterator till it is not equal to the end.
Example
C++
// C++ program to show how to use iterator to
// traverse set container
#include <bits/stdc++.h>
using namespace std;
int main() {
set<int> s = {10, 20, 30, 40, 50};
// Iterator to the beginning of the set
auto it = s.begin();
// Iterating through the entire set
while (it != s.end()) {
// Dereferencing iterator to access value
cout << *it << " ";
// Incrementing the iterator
it++;
}
return 0;
}
Just like that, any container can be traversed.
Reversing a Container
Reverse iterators allow you to traverse a container from the end to the beginning without needing to manually handling the reversal.
Example
C++
// C++ program to show how to use reverse iterators
// to traverse a container from end to beginning
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> vec = {10, 20, 30, 40, 50};
// Defining reverse iterators pointing to the
// reverse beginning of vec
auto it = vec.rbegin();
// Iterating the whole vector in reverse
while (it != vec.rend()) {
cout << *it << " ";
it++;
}
return 0;
}
Container-Independent Algorithms
Iterators allow algorithms to work with any container type, making functions like std::sort(), std::find(), and std::for_each() more flexible. You can pass iterators instead of the actual container.
Example
C++
// C++ program to show how algorithms can work
// with iterators of any container type
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> vec = {30, 10, 40, 10, 50};
multiset<int> ms = {10, 30, 10, 20, 40, 10};
// Using the std::count() algorithm to count
// the number of occurences of 10 in vector
// and multiset using iterator
cout << "10s in Vector: " << count(vec.begin(),
vec.end(), 10) << endl;
cout << "10s in Multiset: " << count(ms.begin(),
ms.end(), 10);
return 0;
}
Output10s in Vector: 2
10s in Multiset: 3
Additional Applications of Iterators
There are more applications of STL iterators:
- Distance Calculation: Using std::distance(), iterators help calculate the number of elements between two positions in a container.
- Stream Iteration: Stream iterators allow you to treat input/output streams like containers, making it easier to read from and write to streams using STL algorithms.
- Move Semantics in STL Algorithms: Move iterators introduces the move semantics in STL algorithms which helps in increasing performance and efficiency by avoiding unnecessary copying. The data will be moved according to the rules of move semantics.
- Custom Iterators for Data Structures: Custom iterators can be implemented for non-STL data structures like trees or graphs to provide the support for STL algorithms and many other features. We may need to follow a few set of rules and conventions to provide proper incrementing, decrementing, and other operations.
Iterators in C++ STL – FAQs
What is the difference between iterators and pointers in C++?
Iterators works similarly to pointers but provide a more generalized way to access elements in a container. Unlike raw pointers, iterators are designed to work with a wide variety of data structures (e.g., vectors, lists) and provide additional operations, such as the ability to traverse and manipulate containers in specific ways (forward, reverse, or bidirectionally).
Why does C++ have both cbegin() and begin()?
cbegin() returns a constant iterator, ensuring that the container’s elements cannot be modified during iteration. This is useful when you need to ensure read-only access.
Why do we need the std::advance() function?
std::advance() abstracts the logic for advancing an iterator by a specific number of steps. This is especially helpful for iterator types that do not support random access, like bidirectional iterators where we have to manually increment the iterator given number of times.
Why use iterator adaptors like back_inserter()?
Iterator adaptors, such as back_inserter(), allow for specialized functionality, like appending elements to a container without manually managing the container’s size or position.
Similar Reads
C++ Standard Template Library (STL)
The C++ Standard Template Library (STL) is a set of template classes and functions that provides the implementation of common data structures and algorithms such as lists, stacks, arrays, sorting, searching, etc. It also provides the iterators and functors which makes it easier to work with algorith
9 min read
Containers in C++ STL (Standard Template Library)
A container is a holder object that stores a collection of other objects (its elements). They are implemented as class templates, which allows great flexibility in the types supported as elements. The container manages the storage space for its elements and provides member functions to access them,
2 min read
STL Containers
STD::array in C++
The array is a collection of homogeneous objects and this array container is defined for constant size arrays or (static size). This container wraps around fixed-size arrays and the information of its size are not lost when declared to a pointer. In order to utilize arrays, we need to include the ar
5 min read
Vector in C++ STL
In C++, vector is a dynamic array with the ability to resize itself automatically when an element is inserted or deleted. It is the part Standard Template Library (STL) and provide various useful functions for data manipulation. Let's take a look a simple example to demonstrate the use of vector con
9 min read
List in C++ Standard Template Library (STL)
Lists are sequence containers that allow non-contiguous memory allocation. As compared to the vector, the list has slow traversal, but once a position has been found, insertion and deletion are quick (constant time). Normally, when we say a List, we talk about a doubly linked list. For implementing
6 min read
Forward List in C++ | Set 1 (Introduction and Important Functions)
Forward list in STL implements singly linked list. Introduced from C++11, forward list are more useful than other containers in insertion, removal, and moving operations (like sort) and allow time constant insertion and removal of elements. It differs from the list by the fact that the forward list
9 min read
Stack in C++ STL
Stacks are a type of container adaptors with LIFO(Last In First Out) type of working, where a new element is added at one end (top) and an element is removed from that end only. Stack uses an encapsulated object of either vector or deque (by default) or list (sequential container class) as its under
4 min read
Queue in C++ Standard Template Library (STL)
Queues are a type of container adaptors that operate in a first in first out (FIFO) type of arrangement. Elements are inserted at the back (end) and are deleted from the front. Queues use an encapsulated object of deque or list (sequential container class) as its underlying container, providing a sp
3 min read
Deque in C++ Standard Template Library (STL)
Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements. Unlike vectors, contiguous storage allocation may not be guaranteed. Double Ended Queues are basi
4 min read
Set in C++ Standard Template Library (STL)
Sets are a type of associative container in which each element has to be unique because the value of the element identifies it. The values are stored in a specific sorted order i.e. either ascending or descending. The std::set class is the part of C++ Standard Template Library (STL) and it is define
7 min read
Map in C++ Standard Template Library (STL)
Maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have the same key values. std::map is the class template for map containers and it is defined inside the <map> header file. Basic std::map Member F
9 min read
Multiset in C++ Standard Template Library (STL)
Multisets are a type of associative containers similar to the set, with the exception that multiple elements can have the same values. Some Basic Functions associated with multiset: begin() - Returns an iterator to the first element in the multiset --> O(1)end() - Returns an iterator to the theor
6 min read
Multimap in C++ Standard Template Library (STL)
Multimap is similar to a map with the addition that multiple elements can have the same keys. Also, it is NOT required that the key-value and mapped value pair have to be unique in this case. One important thing to note about multimap is that multimap keeps all the keys in sorted order always. These
6 min read
Unordered Sets in C++ Standard Template Library
An unordered_set is an unordered associative container implemented using a hash table where keys are hashed into indices of a hash table so that the insertion is always randomized. All operations on the unordered_set take constant time O(1) on an average which can go up to linear time O(n) in the wo
6 min read
unordered_map in C++ STL
unordered_map is an associated container that stores elements formed by the combination of a key value and a mapped value. The key value is used to uniquely identify the element and the mapped value is the content associated with the key. Both key and value can be of any type predefined or user-defi
7 min read
C++ unordered_multiset
The unordered_multiset in C++ STL is an unordered associative container that works similarly to an unordered_set. The only difference is that we can store multiple copies of the same key in this container. It is also implemented using a hash table so the time complexity of the operations is O(1) on
7 min read
unordered_multimap and its application
Allows Duplicates: We have discussed unordered_map in our previous post, but there is a limitation, we can not store duplicates in unordered_map, that is if we have a key-value pair already in our unordered_multimap and another pair is inserted, then both will be there whereas in case of unordered_m
7 min read
Introduction to Iterators in C++
An iterator is an object (like a pointer) that points to an element inside the container. We can use iterators to move through the contents of the container. They can be visualized as something similar to a pointer pointing to some location and we can access the content at that particular location u
6 min read
STL Iterators
Forward Iterators in C++
After going through the template definition of various STL algorithms like std::search, std::search_n, std::lower_bound, you must have found their template definition consisting of objects of type Forward Iterator. So what are they and why are they used ? Forward iterators are one of the five main t
6 min read
Output Iterators in C++
After going through the template definition of various STL algorithms like std::copy, std::move, std::transform, you must have found their template definition consisting of objects of type Output Iterator. So what are they and why are they used ? Output iterators are one of the five main types of it
6 min read
Forward Iterators in C++
After going through the template definition of various STL algorithms like std::search, std::search_n, std::lower_bound, you must have found their template definition consisting of objects of type Forward Iterator. So what are they and why are they used ? Forward iterators are one of the five main t
6 min read
Bidirectional Iterators in C++
After going through the template definition of various STL algorithms like std::reverse, std::next_permutation and std::reverse_copy you must have found their template definition consisting of objects of type Bidirectional Iterator. So what are they and why are they used ? Bidirectional iterators ar
7 min read
Random Access Iterators in C++
After going through the template definition of various STL algorithms like std::nth_element, std::sort, you must have found their template definition consisting of objects of type Random-access Iterator. So what are they and why are they used?Random-access iterators are one of the five main types of
6 min read
Iterators in C++ STL
An iterator in C++ is a pointer-like object that points to an element of the STL container. They are generally used to loop through the contents of the STL container in C++. The main advantage of STL iterators is that they make the STL algorithms independent of the type of container used. We can jus
11 min read
Algorithm Library | C++ Magicians STL Algorithm
For all those who aspire to excel in competitive programming, only having a knowledge about containers of STL is of less use till one is not aware what all STL has to offer. STL has an ocean of algorithms, for all < algorithm > library functions : Refer here.Some of the most used algorithms on
7 min read
STL Algorithms
sort() in C++ STL
In C++, sort() is a built-in function used to sort the given range in desired order. It provides a simple and efficient way to sort the data in C++ but it only works on data structures that provide random access to its elements such as vectors and arrays. Let's take a look at an example: [GFGTABS] C
4 min read
Different methods to copy in C++ STL | std::copy(), copy_n(), copy_if(), copy_backward()
Various varieties of copy() exist in C++ STL that allows to perform the copy operations in different manners, all of them having their own use. These all are defined in header <algorithm>. This article introduces everyone to these functions for usage in day-to-day programming. 1. copy(strt_ite
5 min read
max_element in C++ STL
The std::max_element() in C++ is an STL algorithm that is used to find the maximum element in the given range. It is defined inside the <algorithm> header file. In this article, we will learn how to find the maximum element in the range using std::max_element() in C++. Example: [GFGTABS] C++ /
4 min read
find() in C++ STL
In C++, the find() is a built-in function used to find the first occurrence of an element in the given range. It works with any container that supports iterators, such as arrays, vectors, lists, and more. In this article, we will learn about find() function in C++. Letâs take a look at an example th
3 min read
for_each loop in C++
Apart from the generic looping techniques, such as "for, while and do-while", C++ in its language also allows us to use another functionality which solves the same purpose termed "for-each" loops. This loop accepts a function which executes over each of the container elements. This loop is defined i
5 min read
Algorithm Library Functions in C++ STL
Non-modifying sequence operations std :: all_of : Test condition on all elements in rangestd :: any_of : Test if any element in range fulfills conditionstd :: none_of : Test if no elements fulfill conditionstd :: for_each : Apply function to rangestd :: find : Find value in rangestd :: find_if : Fin
4 min read
Functors in C++
Please note that the title is Functors (Not Functions)!! Consider a function that takes only one argument. However, while calling this function we have a lot more information that we would like to pass to this function, but we cannot as it accepts only one parameter. What can be done? One obvious an
3 min read
C++ STL Cheat Sheet
The C++ STL Cheat Sheet provides short and concise notes on Standard Template Library (STL) in C++. Designed for programmers that want to quickly go through key STL concepts, the STL cheatsheet covers the concepts such as vectors and other containers, iterators, functors, etc., with their syntax and
15+ min read
Top C++ STL Interview Questions and Answers
The Standard Template Library (STL) is a set of C++ template classes that are used to implement widely popular algorithms and data structures such as vectors, lists, stacks, and queues. It is part of the C++ Language ISO standard. STL is a popular topic among interviewers, so it is useful for both f
15+ min read