Every Variable in c++ has some type like in int i , i has a type int .
Similarly we have iterators in STL which we declare say something like this
map<string,int>::iterator it .
What is the type of the it over here ? Is it pointer type or is it a pointer type as
we gerally deference iterators to fetch values associated or pointed by those itearors in case of vectors which store int or some other type.? Or the operator * is overloaded for iterators in STL ?
24.2.1/1 [iterator.requirements.general] sums it up nicely:
The phrase “generalization of pointers” means that pointers are iterators.
std::vector<T>::iteratoris allowed to be a typedefT *. However, most iterators achieve the interface by operator overloading. (Note that iterators don’t need to belong to containers, either.)Such language is very typical of the way the C++ standard is written. It describes how things behave, but avoids defining interfaces in terms of base classes. There are various kinds of iterators: input, output, forward, bidirectional, and random-access. Each has a different specification, and although random-access is a strict superset of the bidirectional interface, they are totally unrelated in the C++ type system.
An iterator can be any class with
++and*overloaded, and a valid specialization ofstd::iterator_traits. There is a base classstd::iteratorwhich works withstd::iterator_traitsto define the necessary interface. It is a good case study in C++ generic programming and traits classes.