C++ – Compare two consecutive elements in std::list

c++compareiteratorlist

I'd like to compare two consecutive elements in a std::list while iterating through the list. What is the proper way to access element i+1 while my iterator is at element i?
Thanks
Cobe

Best Solution

STL provide the adjacent_find() algorithm that can be used to find two consecutive equal elements. There is also a version with a custom predicate.

These are the prototypes:

template <class ForwardIterator>
   ForwardIterator adjacent_find ( ForwardIterator first, ForwardIterator last );

template <class ForwardIterator, class BinaryPredicate>
   ForwardIterator adjacent_find ( ForwardIterator first, ForwardIterator last,
                                   BinaryPredicate pred );