T
Tim Slattery
It would be convenient for my app to store the stuff I'm generating in
a std::list. I'd like to remember the location of a particular place
in the list - sort of like sticking my finger into it - and insert an
entry in that place some time later. There's an "insert" member
function, but it takes an iterator to designate the place to insert.
And I can't figure out how to get an iterator that points to the right
place. For instance:
std::list<int> mylist;
std::list<int>::iterator myiter;
mylist.push_back(1);
mylist.push_back(2);
mylist.push_back(3);
mylist.push_back(4);
mylist.push_back(5);
myiter = mylist.end();
mylist.push_back(6);
mylist.push_back(7);
mylist.push_back(8);
mylist.push_back(9);
mylist.insert(myiter, 100);
for (myiter = mylist.begin(); myiter != mylist.end(); myiter++)
{
std::cout << *myiter << std::endl;
}
The first invocation of "mylist.end" seems to me like it should return
an iterator pointing to element 6. But the result from this code is:
1
2
3
4
5
6
7
8
9
100
"myiter" continued pointing past the end of the list even after more
elements were added! So, is there a way to remember a location so that
I can insert something there later?
--
Tim Slattery
(e-mail address removed)
http://members.cox.net/slatteryt
a std::list. I'd like to remember the location of a particular place
in the list - sort of like sticking my finger into it - and insert an
entry in that place some time later. There's an "insert" member
function, but it takes an iterator to designate the place to insert.
And I can't figure out how to get an iterator that points to the right
place. For instance:
std::list<int> mylist;
std::list<int>::iterator myiter;
mylist.push_back(1);
mylist.push_back(2);
mylist.push_back(3);
mylist.push_back(4);
mylist.push_back(5);
myiter = mylist.end();
mylist.push_back(6);
mylist.push_back(7);
mylist.push_back(8);
mylist.push_back(9);
mylist.insert(myiter, 100);
for (myiter = mylist.begin(); myiter != mylist.end(); myiter++)
{
std::cout << *myiter << std::endl;
}
The first invocation of "mylist.end" seems to me like it should return
an iterator pointing to element 6. But the result from this code is:
1
2
3
4
5
6
7
8
9
100
"myiter" continued pointing past the end of the list even after more
elements were added! So, is there a way to remember a location so that
I can insert something there later?
--
Tim Slattery
(e-mail address removed)
http://members.cox.net/slatteryt