Iterators - paulahemsi/ft_containers GitHub Wiki

Iterators_traits

std::iterator_traits is the type trait class that provides uniform interface to the properties of LegacyIterator types. This makes it possible to implement algorithms only in terms of iterators.

The template can be specialized for user-defined iterators so that the information about the iterator can be retrieved even if the type does not provide the usual typedefs.

the point is to make do_something work with pointers as well as class based iterators

stackOverflow usefull thread

Random-access_and_Bidirectional_iterators

Random-access iterators are iterators that can be used to access elements at an arbitrary offset position relative to the element they point to, offering the same functionality as pointers. Random-access iterators are the most complete iterators in terms of functionality. All pointer types are also valid random-access iterators.

Bidirectional iterators are iterators that can be used to access the sequence of elements in a range in both directions (towards the end and towards the beginning). They are similar to forward iterators, except that they can move in the backward direction also, unlike the forward iterators, which can move only in the forward direction.

It is to be noted that containers like vector, deque support random-access iterators. This means that if we declare normal iterators for them, and then those will be random-access iterators, just like in case of list, map, multimap, set and multiset they are bidirectional iterators.

Feature random access iterator bidirectional iterator
can be used in multi-pass algorithms, i.e., algorithm which involves processing the container several times in various passes. :heavy_check_mark: :heavy_check_mark:
can be compared for equality with another iterator. Since, iterators point to some location, so the two iterators will be equal only when they point to the same position, otherwise not. :heavy_check_mark: :heavy_check_mark:
can be dereferenced both as a rvalue as well as a lvalue. :heavy_check_mark: :heavy_check_mark:
can be incremented and decremented, so that it refers to the next/previous element in sequence, using operator ++() or --() :heavy_check_mark: :heavy_check_mark:
support all relational operators (== >= <= !=) :heavy_check_mark: :x: (only ==)
can be used with arithmetic operators like +, – and so on. This means that Random-access iterators can move in both the direction, and that too randomly :heavy_check_mark: :x:
support offset dereference operator ([ ]), which is used for random-access :heavy_check_mark: :x:
The value pointed to by these iterators can be exchanged or swapped :heavy_check_mark: :heavy_check_mark:

Reverse_iterators

std::reverse_iterator is an iterator adaptor that reverses the direction of a given iterator.

In other words, when provided with (at least) a bidirectional iterator, std::reverse_iterator produces a new iterator that moves from the end to the beginning of the sequence defined by the underlying bidirectional iterator.

For a reverse iterator r constructed from an iterator i, the relationship &*r == &*(i-1) is always true (as long as r is dereferenceable); thus a reverse iterator constructed from a one-past-the-end iterator dereferences to the last element in a sequence.

image image