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

4

60

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?

Attributive answered 13/4, 2012 at 8:0 Comment(6)
When you do it++, it translates as it = it + 1, meaning technically the iterator object could just as well be a new object everytime. Not sure what you're asking.Selfstyled
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. But they are wrappers around this "copy and increment" operationNaumachia
Thanks @jalf. Your Answer is the only full answer.Attributive
@elusive I have putted "it + 1" and "it - 1" to the quotes because they are not valid operations of the bidirectional iterator. Do you care if I add them back?Attributive
@MihranHovsepyan: you can put the quotes back if you like. I don't think they're necessary though (because it's already clear that this is what you want to do, but it can't be done). And thanks, I'll post it as an answer then :)Naumachia
@MihranHovsepyan: I do not think the quotes are necessary. The problem is that they may be interpreted as strings, which is clearly not what you intended. Thats why I removed them. ;)Buonomo
N
24

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".

Naumachia answered 13/4, 2012 at 8:37 Comment(0)
M
61

Yes, since C++11 there are the two methods you are looking for called std::prev and std::next. You can find them in the iterator library.

Example from cppreference.com

#include <iostream>
#include <iterator>
#include <list>
 
int main() 
{
    std::list<int> v{ 3, 1, 4 };
 
    auto it = v.begin();
 
    auto nx = std::next(it, 2);
 
    std::cout << *it << ' ' << *nx << '\n';
}

Output:

3 4
Muntjac answered 13/4, 2012 at 8:4 Comment(2)
And of course, if you don't have C++11, they're very easy to implement. (I think most of us had them in our toolkits well before they were even proposed for C++11.)Escalera
If you don't have C++11, just use std::advance() instead.Shem
N
24

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".

Naumachia answered 13/4, 2012 at 8:37 Comment(0)
D
7

A simple precanned solution are prior and next from Boost.utility. They take advantage of operator-- and operator++ but don't require you to create a temporary.

Diplopia answered 13/4, 2012 at 8:3 Comment(1)
Of course it should be mentionned that 1. They create the temporary on their own and 2. You'd better be aware of boundary conditions.Romito
I
0

if you realize your own list, you can write your own internal class Iterator, where you can easy realize next() and prev() methods throw the reloading of operator--() and operator++()

Intermarriage answered 17/7, 2024 at 8:50 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.