next function

My understanding for next(ptr,n) is it returns a pointer to the nth element after *ptr.

My question is: Can n be negative? what happened to e.g. next(ptr,-2)?

Thanks,
L


If I ask you half of a question, can you tell me the answer?

How are we supposed to know about the function and data structure you're talking about?
Assuming OP is referring to std::next():
N can be negative, as long as the iterator (the first argument) meets the requirements of BidirectionalIterator. Pointers satisfy this requirement.

Note: every BidirectionalIterator is a ForwardIterator; the iterator passed to std::next() is only required to be a ForwardIterator.

You should use std::prev() if you know N will be negative.

http://en.cppreference.com/w/cpp/iterator/next
Last edited on
Repeat of mbozzi's post; deleted
Last edited on
Yes, it is std::next().

Thanks.
Topic archived. No new replies allowed.