C++ Iterators are used to point at the memory addresses of STL containers. They are primarily used in the sequence of numbers, characters etc.used in C++ STL. It is like a pointer that points to an element of a container class (e.g., vector, list, map, etc.).
- Operator * :
- Returns the element of the current position.
- Operator ++ and — :
- It allows the iterator to step forward to the next element by ‘++,’ also it allows the iterator to step backward to the previous element by ‘–’
- Operator == and !=:
- Check whether two iterators represent the same position or not.
- Operator =: Assigns an iterator (the position of the element to which it refers.
#Advantage and Disadvantage of iterator
Advantage
- The STL provides iterators as a convenient abstraction for accessing many different types of containers.
- Iterators for template classes that are generated inside a class scope with the syntax.
Example: class_name::iterator
Disadvantage
- Iterators do not provide the bounds checking; it is possible to overstep the bounds of the container, resulting in the segmentation faults.
- Different containers support different iterators, so it is not always possible to change an underlying container type without making the changes to your code.
- Iterators can be invalidated if an underlying container (the container being iterated over) is changed significantly.
#c++ #c++ iterators #c++ stl