How get next (previous) element in std::list without incrementing (decrementing) iterator?

c++, iterator, list

Solution

Copying and incrementing/decrementing the copy is the only way it can be done.

You can write wrapper functions to hide it (and as mentioned in answers, C++11 has std::prev/std::next which do just that (and Boost defines similar functions). But they are wrappers around this "copy and increment" operation, so you don't have to worry that you're doing it "wrong".

Problem

Say I have an `std::list<int> lst` and some `std::list<int>::iterator it` for iterating through the list. And depended to value of the `it` I want to use `it + 1` or `it - 1` in my code. Is there some good way to do that like `next()`, `prev()` (I couldn't find such things in stl documentation)? Or should I copy the `it` each time and increment(decrement) the copy?

Original source