473,385 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Checking if iterator points to a specific element

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();
}
}

Jul 19 '05 #1
2 2156
"Martin Magnusson" <lo*******@frustratedhousewives.zzn.com> 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();
}
}

Jul 19 '05 #2
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.
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Michael Klatt | last post by:
I am trying to write an iterator for a std::set that allows the iterator target to be modified. Here is some relvant code: template <class Set> // Set is an instance of std::set<> class...
4
by: steflhermitte | last post by:
Dear cpp-ians, I am working with a structure struct segment { .... vector <meta_segment>::iterator it_Z; .... };
18
by: silversurfer | last post by:
Ok, this should be fairly easy for most of you (at least I hope so), but not for me: Let us say we have got the following elements: std::vector<Entry> models; //Entry is a struct...
4
by: arnuld | last post by:
i wrote a programme to create a vector of 5 elements (0 to 4), here is the code & output: #include <iostream> #include <vector> int main() { std::vector<intivec; // dynamically create a...
15
by: Boltar | last post by:
Hi I'm going through an STL list container using a reverse iterator but it seems the erase() method only accepts ordinary iterators. Is there a similar method that will accept reverse iterators...
0
by: toton | last post by:
Hi, I have a complex design of some dynamic situation. First I am posting the design, then a few questions regarding the problem I am facing. Hope some of the experts will answer the questions....
3
by: JurgenvonOerthel | last post by:
I want to replace one element in a list<stringby a list<stringand I need to obtain an iterator to the first element in the inserted list. In code: void...
4
by: pandit | last post by:
Hello i dont understand how to Deal with iterator. its little bit confusing ? how they work??
5
by: subramanian100in | last post by:
Whenever we specify an iterator range to a container operation or an std::algorithm, the first iterator should always be a valid iterator ie it should point to some element in the respective...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.