M
Martin Magnusson
Hi
I'm traversing a std::list backwards with a reverse_iterator, and need
to check for the case when the iterator points to the first element of
the list.
My code now looks something like the code posted below. This usually
works a few times, but then gives me a segmentation fault, apparently at
the line "if (pos == --(the_list.rend()))". Shouldn't that always be
valid, as long as there is always at least one element in the list at
the beginning of the loop? Is there a better way to check if the
iterator points to the first list element?
list<MyClass*>::reverse_iterator i;
for (pos = the_list.rbgin(); pos != the_list.rend(); /**/ )
{
if (pos == --(the_list.rend())) /* Is this not always valid? */
{
// Do something with *pos
the_list.clear();
pos = the_list.rend();
}
else
{
// Do something else with *pos
++pos;
the_list.erase( pos.base(), the_list.end() );
pos = the_list.rbegin();
}
}
I'm traversing a std::list backwards with a reverse_iterator, and need
to check for the case when the iterator points to the first element of
the list.
My code now looks something like the code posted below. This usually
works a few times, but then gives me a segmentation fault, apparently at
the line "if (pos == --(the_list.rend()))". Shouldn't that always be
valid, as long as there is always at least one element in the list at
the beginning of the loop? Is there a better way to check if the
iterator points to the first list element?
list<MyClass*>::reverse_iterator i;
for (pos = the_list.rbgin(); pos != the_list.rend(); /**/ )
{
if (pos == --(the_list.rend())) /* Is this not always valid? */
{
// Do something with *pos
the_list.clear();
pos = the_list.rend();
}
else
{
// Do something else with *pos
++pos;
the_list.erase( pos.base(), the_list.end() );
pos = the_list.rbegin();
}
}