Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Checking if iterator points to a specific element

Reply
Thread Tools

Checking if iterator points to a specific element

 
 
Martin Magnusson
Guest
Posts: n/a
 
      10-11-2003
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();
}
}

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-11-2003
"Martin Magnusson" <(E-Mail Removed)> 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();
> }
> }
>



 
Reply With Quote
 
 
 
 
Martin Magnusson
Guest
Posts: n/a
 
      10-12-2003
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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
XML parsing problem finding a specific element in a specific place mazdotnet ASP .Net 2 10-02-2009 10:07 AM
Writing a macro for checking close enough floating points. pereges C Programming 3 07-23-2008 03:37 PM
Writing a macro for checking close enough floating points. pereges C Programming 8 07-22-2008 01:32 PM
how to Update/insert an xml element's text----> (<element>text</element>) HANM XML 2 01-29-2008 03:31 PM
Canon A80l, some specific points Anthony Buckland Digital Photography 6 12-20-2003 01:29 AM



Advertisments