Checking if iterator points to a specific element

Discussion in 'C++' started by Martin Magnusson, Oct 11, 2003.

  1. 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();
    }
    }
    Martin Magnusson, Oct 11, 2003
    #1
    1. Advertising

  2. "Martin Magnusson" <> wrote...
    > 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?


    'rend' returns a temporary. -- expects a modifiable lvalue. Could that
    be the source of the problem?

    >Is there a better way to check if the
    > iterator points to the first list element?


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

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

    and compare with 'one_before_rend' instead of that expression.

    >
    > 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();
    > }
    > }
    >
    Victor Bazarov, Oct 11, 2003
    #2
    1. Advertising

  3. Victor Bazarov wrote:
    >>Is there a better way to check if the
    >>iterator points to the first list element?

    >
    >
    > 'front' returns a reference to the first element, or you could do
    >
    > list<MyClass*>::reverse_iterator one_before_rend = the_list.rend();
    > --one_before_rend;
    >
    > and compare with 'one_before_rend' instead of that expression.


    Thanks! Comparing *pos with the_list.front() did the trick! Using the
    soltion with one_before_end still gave me sporadic segmentaition faults
    though.
    Martin Magnusson, Oct 12, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hendrik Maryns
    Replies:
    18
    Views:
    1,418
  2. HANM
    Replies:
    2
    Views:
    708
    Joseph Kesselman
    Jan 29, 2008
  3. pereges
    Replies:
    8
    Views:
    329
    Flash Gordon
    Jul 22, 2008
  4. pereges
    Replies:
    3
    Views:
    347
    ~Glynne
    Jul 23, 2008
  5. mazdotnet
    Replies:
    2
    Views:
    396
    Alexey Smirnov
    Oct 2, 2009
Loading...

Share This Page