Last Updated : 23 Jul, 2025
Iterators are pointer like objects that are used to refer iterator the STL containers. Just like pointer arithmetic, there are some operations that are allowed on iterators in C++. They are used to provide different functionalities to make iterator more powerful and versatile. In this article, we will learn about iterator operations (also called iterator arithmetic) in C++.
There are basically 5 operations allowed on C++ iterators:
Dereferencing IteratorsDereferencing 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*itr; // Access
*itr = new_val; // Update
where new_val is the value to be assigned to the element pointed by itr.
Example C++
// C++ program to demonstrate dereferencing an iterator
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<char> vec = {'a', 'b', 'c', 'd'};
// Create an iterator pointing to the second
// element (index 1)
auto itr = vec.begin() + 1;
// Dereference the iterator to get the value
// at the second position
cout << "Second Element: " << *itr << endl;
// Modify the value of the second element using
// the iterator
*itr = 'x';
cout << "Second Element After Update: " << *itr;
return 0;
}
Second Element: b Second Element After Update: x
We first access the value of iterator itr by dereferencing it. Then we update the value of the element pointed by itr assigning a new value after dereferencing.
Incrementing/Decrementing IteratorsWe 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.
Syntaxitr++; // post-increment
++itr; // pre-incrementitr--; // post-decrement
--itr; // pre-decrement
pre and post increment/decrement basically does the same thing with small difference.
Example C++
// C++ program to demonstrate incrementing and
// decrementing an iterator
#include <bits/stdc++.h>
using namespace std;
int main() {
// Initialize a vector of characters
vector<char> vec = {'a', 'b', 'c', 'd'};
// Create an iterator pointing to the second
// element (index 1)
auto itr = vec.begin() + 1;
// Dereference the iterator to get the value
// at the second position
cout << "Second Element: " << *itr << endl;
// Increment the iterator to point to the next
// element
itr++;
cout << "After Increment, Element: " << *itr
<< endl;
// Decrement the iterator to point back to the
// previous element
itr--;
cout << "After Decrement, Element: " << *itr;
return 0;
}
Second Element: b After Increment, Element: c After Decrement, Element: b
Explanation: We have a vector vec = {'a', 'b', 'c', 'd'} and iterator itr pointing to its second element 'b'. We incremented the iterator itr to point it to the next character 'c'. Then we decremented it, and it began to point to the second character again.
Adding/Subtracting Integer to IteratorsWe can also add or subtracts an integer value from the iterator using (+) and (-) operators. It will more the iterator next or previous position according to the added integer value.
Syntaxitr + int_val; // Addition
itr - int_val; // Subtraction
where int_val is some integer value and itr is the iterator.
Example C++
// C++ program to demonstrate adding and subtracting
// with an iterator
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<char> vec = {'a', 'b', 'c', 'd'};
// Iterator pointing to the second element
auto itr = vec.begin() + 1;
cout << "Second Element: " << *itr << endl;
// Add 2 to the iterator, moving it two positions
// ahead
itr = itr + 2;
cout << "After Adding 2, Element: " << *itr
<< endl;
// Subtract 3 from the iterator, moving it three
// positions back
itr = itr - 3;
cout << "After Subtracting 3, Element: " << *itr;
return 0;
}
Second Element: b After Adding 2, Element: d After Subtracting 3, Element: a
Explanation: We added 2 to the iterator itr which was pointing to the second element 'b' of the vector vec. It moved two places forward and start pointing to the fourth element 'd'. Then we decremented 3 and it moved three places backwards and pointed to the first element 'a'.
Subtraction of Another IteratorWe can subtract one iterator from another to find the distance (or the number of elements) between the memory they are pointing to.
Syntaxitr1 - itr2
where itr1 and itr2 are the iterators to the same container as subtracting iterator of different containers is not meaningful is most cases.
Example C++
// C++ program to demonstrate iterator subtraction
#include <bits/stdc++.h>
using namespace std;
int main() {
// Initialize a vector of characters
vector<char> vec = {'a', 'b', 'c', 'd'};
// Iterator pointing to the first element
auto itr1 = vec.begin();
// Iterator pointing to the last element
auto itr2 = vec.end() - 1;
// Subtract itr1 from itr2 to get the number of
// elements between them
cout << "Elements between itr1 and itr2: " <<
itr2 - itr1;;
return 0;
}
Elements between itr1 and itr2: 3
Explanation: The iterator itr1 points to the first element 'a' and iterator itr2 points to the last element 'd'. Subtracting itr1 from itr2, we get 3 which is the number of elements between itr1 and itr2 inclusive itr2.
Comparing IteratorsWe can also test two iterators of the same type against each other to find the relationship between them. We can use the equality operators like (==) and (!=) along with relational operators such as <, >, <=, >=.
Syntaxitr1 != itr2 // Equal to
itr1 == itr2 // Not equal toitr1 > itr2 // Greater than
itr1 < itr2 // Less than
itr1 >= itr2 // Greater than equal to
itr1 <= itr2 // Less than equal to
The equality operators generally have variety of use cases, but relational operators are generally used for iterators belonging to same container.
Example C++
// C++ program to demonstrate iterator equality and
// relational comparison
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<char> vec = {'a', 'b', 'c', 'd'};
// Iterator pointing to the first element
auto itr1 = vec.begin();
// Iterator pointing to the last element
auto itr2 = vec.end() - 1;
// Check equality using == operator
if (itr1 == itr2)
cout << "itr1 and itr2 are equal." << endl;
else
cout << "itr1 and itr2 are not equal." << endl;
// Check relational comparison using <, >, <=, >= operators
if (itr1 < itr2)
cout << "itr1 is less than itr2." << endl;
if (itr2 > itr1)
cout << "itr2 is greater than itr1." << endl;
// Iterator pointing to the same position as itr1
auto itr3 = vec.begin();
// Check equality with itr3
if (itr1 == itr3)
cout << "itr1 and itr3 are equal." << endl;
else
cout << "itr1 and itr3 are not equal." << endl;
return 0;
}
itr1 and itr2 are not equal. itr1 is less than itr2. itr2 is greater than itr1. itr1 and itr3 are equal.
Explanation: Iterator itr1 points to the first element and iterator itr2 points to the last element. Another iterator itr3 again points to the first element. While comparing itr1 and itr2 for equality, we get false as they point to different memory location. But while comparing itr1 and itr3 for equality, we get true as they point to same memory location.
Iterator Types and Supported OperationsNote: All these operation does not perform any bound checking, so it may lead point to invalid memory location if not handled properly.
One main thing to note is that not all types of iterators support all iterator arithmetic/operations. Different iterator types from different containers support a few or more iterator operations. The below table lists the iterator types, their associated containers and their supported operations:
Iterator
Supported Containers
Supported Operations
Input Stream
Dereferencing, Increment, Equality
Output Stream
Dereferencing (write only), Increment
forward_list, unordered_map, unordered_set
Dereferencing, Increment, Equality
list, map, set, multimap, multiset
Dereferencing, Increment/Decrement, Equality, Subtraction of Same Type Iterator
vector, deque, array, string
Dereferencing, Addition/Subtraction of Integer, Increment/Decrement, Equality, Relational, Subtraction of Same Type Iterator
Iterator Operations Utility FunctionsC++ STL provides a few functions to complement the use of iterator operations. Few such functions are listed in the below table:
Function
Description
Syntax
Moves an iterator forward or backward by a specified number of positions, modifying the original iterator.
std::advance(itr, n);
Returns an iterator advanced by a specified number of positions without modifying the original iterator.
temp_itr = std::next(itr, n);
Returns an iterator moved backward by a specified number of positions without modifying the original iterator.
temp_itr = std::prev(itr, n);
Returns the number of steps(elements) between two iterators.
diff = std::distance(itr1, itr2);
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4