Hello everyone I am reading Bjarne Stroustrups new book and i am having a hard time figuring out what this code does.The problem I have with it is 'while(n--)'
, the n++ I get but this is suppose to be an example of a forward iterator so how is he decrementing the index.Thanks
1 2 3 4 5 6 7 8 9 10 11 12 13 14
On the other hand, a forward iterator must get to the nth
element by moving one step at a time (e.g.,
following links on a list):
template<typename Iter>
void advance_helper(Iter p, int n, forward_iterator_tag)
{
if (0<n)
while (nāā) ++p;
elseif (n<0)
while (n++) āāp;
}
This is the helper function for std::advance() which increments given iterator it by n elements.
With this proviso: If n is negative, the iterator is decremented. In this case, the iterator must meet the requirements of BidirectionalIterator, otherwise the behaviour is undefined. http://en.cppreference.com/w/cpp/iterator/advance
In other words, if n is positive, any InputIterator would do,
but if n is negative, the iterator must be a BidirectionalIterator.