Iterators in C++
Container iterators in the C++ programming language are user-defined program data type objects that provide a variety of methods and options for managing access to and traversing elements of C++ container data types, such as vectors, lists, sets, maps, and so on. These pointers function similarly to data objects, but are more flexible and reliable, allowing for a variety of uses with multiple individual data type containers in a C++ program. Iterators in C++ container data types are an important feature of the Standard Template Library (STL) and allow C++ users to isolate the special features of container data types. They also help access and modify data elements of container data types.

Types of Container Data Type Iterators in C++ Programming.
C++ programming provides a variety of iterator data container options. Which completely depends on the types of operations that can be performed with it.
Input C++ Iterator.
- The input C++ iterator helps us read data and information in one direction from user-defined container data types like vector, list, set, map, etc.
- It supports the ++ (increment) and * (dereference) operators.
Output C++ Iterator.
- The output C++ iterator helps us create or write data and information in one direction from user-defined container data types like vector, list, set, map, etc.
- It supports the ++ (increment) and * (dereference) C++ program operators.
Forward C++ Iterator.
- This C++ iterator helps us read and write data and information in one direction (forward) from user-defined container data types like list, etc.
- It also supports container data operators like ++, *, and !=.
Bidirectional C++ Iterator.
- This C++ iterator allows for both forward and backward movement in user-defined container data types like vectors, lists, sets, maps, etc.
- It also supports the ++, –, *, and != operators within the container.
Random Access C++ Iterator.
- It allows for constant-time direct access to any element in a container data type, such as an array.
- It also supports the ++, –, *, !=, [], and +/- operators for arithmetic container data type operations.
Most C++ STL container data types like vector, deque, map, and set support random access iterator or bidirectional iterator methods, depending on the container.
Basic Container Data Type Operations with Iterators.
Iterators in C++ container data types support a variety of data operations, which are very similar to pointer data types.
Some basic C++ container data types include iterator operations.
- Dereferencing (*iter) – Accesses the container data element to which the iterator data object points.
- Incrementing (++iter or iter++) – Moves the iterator data object to the next element.
- Comparison (iter != iter_end) – Compares iterators to check for equality or non-equality.
- Arrow operator (iter->) – Accesses the member of the data object pointed to by the iterator. This makes it useful for containers such as maps or sets.
Using the iterator concept with container data types.
Here are some examples that demonstrate how to use iterators with different container data type methods in a program.
Iterator concept in C++ with vectors.
In the C++ programming language, vectors are a sequence-continuous data type container method. They support random-access iterator methods, which means C++ users can use them to quickly traverse data elements in any order.
Example of iterator usage with the vector container data type.
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> vctr = {9, 23, 47, 59, 87, 98};
// Here we declare an iterator method for the vector data type element
vector<int>::iterator it = vctr.begin();
// Here we access vector data type elements using the iterators method
cout << “Here we use the iterator method to traverse vector data elements – “;
while (it != vctr.end()) {
cout << *it << ” “;
// Here we use the iterator concept
++it;
// Here it increments the iterator value with the ++ increment operator
}
cout << endl;
return 0;
}
Explanation of iterator usage with the vector container data type.
- In this vector iterator example, vctr.begin() returns an iterator value pointing to the first element of the vector.
- vctr.end() returns an iterator value pointing to the last element of the vector.
- *It dereferences the iterator to access the vector data type container value.
- Increment operator ++ It increments the iterator to move to the next vector element.
List data type Iterator concept in C++.
In the C++ programming language, the list container data type is similar to a doubly linked list data type and supports bidirectional iterator methods. This allows C++ users to view and operate on the list container data type in both forward and backward directions.
Example of using a container iterator with a list.
#include <iostream>
#include <list>
using namespace std;
int main() {
list<int> lst = {712, 489, 987, 569, 789, 898};
// Here we declare an iterator for the list element
list<int>::iterator it = lst.begin();
// Here we traversing the list in the forward direction
cout << “Iterator method to traverse a list element in the forward direction – “;
while (it != lst.end()) {
cout << *it << ” “;
++it;
}
cout << endl;
// here it traversing the list in backwards direction
cout << “Iterator method to traverse list element in backward direction – “;
list<int>::reverse_iterator rit = lst.rbegin(); // here it reverse iterator method
while (rit != lst.rend()) {
cout << *rit << ” “;
++rit;
}
cout << endl;
return 0;
}
Explanation of using a container iterator with a list.
- In the list iterator example, lst.begin() provides a forward iterator pointing to the first list element.
- lst.end() provides the iterator value after the last element in the list.
- lst.rbegin() provides a reverse iterator pointing to the last list element, allowing backward traversal.
- In the list iterator, ++it and ++rit increment the iterator to move forward or backward through the sequence.
Set iterator concept in C++.
In C++ programming, a set is a group data type container, an ordered collection of unique data elements. It supports the bidirectional iterator method in C++, which allows traversal of a set data type container element in sorted order, from the start to the last element in the set data type container.
Example of using the iterator method with sets.
#include <iostream>
#include <set>
using namespace std;
int main() {
set<int> sat = {99, 66, 55, 77, 88, 99, 100};
// Here we declare an iterator for the set element container
set<int>::iterator it = sat.begin();
// Here it traversing the set element
cout << “Iterator method to traverse list element set is – “;
while (it != sat.end()) {
cout << *it << ” “; // Here it dereferencing the iterator method
++it; // Here it incrementing the iterator method
}
cout << endl;
return 0;
}
Explanation of using the iterator method with sets.
- In this example, sat.begin() provides an iterator value pointing to the first (smallest) element of the set.
- sat.end() provides an iterator value after the last element of the set data.
- Due to the nature of the set data type, the set data elements are automatically sorted in an increasing order sequence.
Map iterator concept in C++.
In C++ programming, a map is a container data type element that stores data and information in a key-value pair-ordered sequence. The elements of the map data type are automatically sorted and arranged in sequential order based on their keys. The map container data type supports bidirectional iterator methods.
Example of using an iterator with map container data.
#include <iostream>
#include <map>
using namespace std;
int main() {
map<string, int> mep = {{“c++”, 999}, {“Java”, 1099}, {“Matlab”, 1299}, {“Python”, 1199}};
// here we are declaring an iterator for the map container data type
map<string, int>::iterator it = mep.begin();
// here it traversing the map container data type
cout << “Iterator method to traverse map stored element – “;
while (it != mep.end()) {
cout << it->first << ” –> ” << it->second << “, “;
++it;
}
cout << endl;
return 0;
}
Explanation of using an iterator with map container data.
- Here, in the map container data type example, it->first accesses the key from the map data element, and it->second accesses the value in the map data container key-value pair.
- Map data type iterators automatically traverse the map elements in sequence in sorted order based on the key.
Iterator method vectors, lists, sets, maps detail Summary.
| Iterator data Type | Detail Description | Operations Supported type |
| Input Iterator | It read container data elements in one direction | Use as ++, * dereferencing container operator |
| Output Iterator | It used to write container elements in one direction | It used as ++, * dereferencing container operator |
| Forward Iterator | It used to read and write container data elements in one direction | It used as ++, *, != comparison operator |
| Bidirectional Iterator | Here it read/write container data elements in both directions | It used as ++, –, *, != operator |
| Random Access Iterator | Here it supports all operations (like array access) | It used as ++, –, *, !=, [], +, – operator in container |

