list::iterator behavior (after insert/delete) specification

J

Jason S

where is the behavior of list::iterator specified? I need to keep track
of ranges of data, all I can figure out so far is that iterators are
guaranteed to be valid unless you remove the elements they point to.

Example -- if L is a list and Li = L.end(), and you call
L.push_back(something), how do you know whether Li will still point to
the list's end, or whether it will point to the something?

I tried this program in Microsoft VC++ 6.0 and it looks like if Li is
assigned to L.end(), it will always point to L.end() in the face of
insertions. This happens to be the behavior I'm looking for, in one
case, but how do I know this is portable to other implementations of
list<> ?

#include <stdio.h>
#include <list>

typedef std::list<int> L_t;

void print(L_t::iterator start, L_t::iterator stop, char *tag)
{
printf("%s", tag);
while (start != stop)
{
printf(" %d", *start);
++start;
}
printf("\n");
}

int main(int argc, char* argv[])
{
L_t the_list;

L_t::iterator a, b, c, d;

the_list.push_back(1);
the_list.push_back(2);
the_list.push_back(3);
a = the_list.begin();
b = the_list.end();
c = b; c--;
print(a,b,"a to b:");
print(a,c,"a to c:");
the_list.push_back(4);
print(a,b,"a to b:");
print(a,c,"a to c:");

return 0;
}

==== output when I run it ====
a to b: 1 2 3
a to c: 1 2
a to b: 1 2 3 4
a to c: 1 2
 
V

Victor Bazarov

Jason said:
where is the behavior of list::iterator specified?

In the language Standard document.
I need to keep
track of ranges of data, all I can figure out so far is that
iterators are guaranteed to be valid unless you remove the elements
they point to.

Pretty much. What else do you need to know? The list iterators are
bidirectional, so you should also look at the traits of bidirectional
iterators (and the requirements for them).
Example -- if L is a list and Li = L.end(), and you call
L.push_back(something), how do you know whether Li will still point to
the list's end, or whether it will point to the something?

None of the valid list iterators change when you insert, right? So,
if you obtained the end (and held onto it), it won't change.
I tried this program in Microsoft VC++ 6.0 and it looks like if Li is
assigned to L.end(), it will always point to L.end() in the face of
insertions. This happens to be the behavior I'm looking for, in one
case, but how do I know this is portable to other implementations of
list<> ?

It is, AFAICT by reading the Standard.

V
 
J

Jason S

Victor said:
In the language Standard document.

thanks. we have an old copy of the standard (1998?) at my company so I
guess we'll have to get an update.

sigh. what about the average person, why do they have to spend $300 to
get the standard to find out how the STL is supposed to be have, & why
doesn't this fact seem to be mentioned in any compiler/STL documents?
 
V

Victor Bazarov

Jason said:
thanks. we have an old copy of the standard (1998?) at my company so I
guess we'll have to get an update.

You probably should, it's not that expensive, but in 2003 nothing (IIRC)
changed in the way iterators or list are defined.
sigh. what about the average person, why do they have to spend $300 to
get the standard to find out how the STL is supposed to be have, & why
doesn't this fact seem to be mentioned in any compiler/STL documents?

I'm an average person and I spend about $18 once in five years to get
an electronic version of the Standard. You could also get (and I
strongly recommend it) draft versions from the Standard Committee page
(predictable) and updated books on the language and the library (not
very predicatble).

V
 
B

Bo Persson

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top