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
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