Checking if iterator points to a specific element

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Magnusson

    Checking if iterator points to a specific element

    Hi

    I'm traversing a std::list backwards with a reverse_iterato r, 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_iterat or 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 ();
    }
    }

  • Victor Bazarov

    #2
    Re: Checking if iterator points to a specific element

    "Martin Magnusson" <loveslave@frus tratedhousewive s.zzn.com> wrote...[color=blue]
    > I'm traversing a std::list backwards with a reverse_iterato r, 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?[/color]

    'rend' returns a temporary. -- expects a modifiable lvalue. Could that
    be the source of the problem?
    [color=blue]
    >Is there a better way to check if the
    > iterator points to the first list element?[/color]

    'front' returns a reference to the first element, or you could do

    list<MyClass*>: :reverse_iterat or one_before_rend = the_list.rend() ;
    --one_before_rend ;

    and compare with 'one_before_ren d' instead of that expression.
    [color=blue]
    >
    > list<MyClass*>: :reverse_iterat or 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 ();
    > }
    > }
    >[/color]


    Comment

    • Martin Magnusson

      #3
      Re: Checking if iterator points to a specific element

      Victor Bazarov wrote:[color=blue][color=green]
      >>Is there a better way to check if the
      >>iterator points to the first list element?[/color]
      >
      >
      > 'front' returns a reference to the first element, or you could do
      >
      > list<MyClass*>: :reverse_iterat or one_before_rend = the_list.rend() ;
      > --one_before_rend ;
      >
      > and compare with 'one_before_ren d' instead of that expression.[/color]

      Thanks! Comparing *pos with the_list.front( ) did the trick! Using the
      soltion with one_before_end still gave me sporadic segmentaition faults
      though.


      Comment

      Working...