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.).

  1. Operator * :
  2. Returns the element of the current position.
  3. Operator ++ and — :
  4. 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 ‘–’
  5. Operator == and !=:
  6. Check whether two iterators represent the same position or not.
  7. Operator =: Assigns an iterator (the position of the element to which it refers.

#Advantage and Disadvantage of iterator

Advantage

  1. The STL provides iterators as a convenient abstraction for accessing many different types of containers.
  2. Iterators for  template classes that are generated inside a class scope with the syntax.

Example: class_name::iterator

Disadvantage

  1. Iterators do not provide the bounds checking; it is possible to overstep the bounds of the container, resulting in the segmentation faults.
  2. Different containers support different iterators, so it is not always possible to change an underlying container type without making the changes to your code.
  3. Iterators can be invalidated if an underlying container (the container being iterated over) is changed significantly.

#c++ #c++ iterators #c++ stl

C++ Iterators Example | Iterators in C++
1.50 GEEK