std::list.insert usage

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
 
O

Ondra Holub

Tim Slattery napsal:
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?

end() returns iterator which points behind the last element. Use back()
- it returns iterator pointing to the last element.
 
G

Gianni Mariani

Tim Slattery wrote:
....
"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?

Yes - use:
myiter = mylist.end();
-- myiter;

The "end" interator points to the mythical place one past the last
element of the list. So your push backs are always made between the
last element and end().
 
G

Gianni Mariani

Ondra Holub wrote:
....
end() returns iterator which points behind the last element. Use back()
- it returns iterator pointing to the last element.

back() returns a reference, not an iterator.
 
S

Sylvester Hesp

Gianni Mariani said:
Tim Slattery wrote:
...

Yes - use:
myiter = mylist.end();
-- myiter;

The "end" interator points to the mythical place one past the last element
of the list. So your push backs are always made between the last element
and end().

This makes sense as cont.push_back(element) is really defined as:
cont.insert(cont.end(), element)
and insert inserts elements right before the iterator. Therefore, end()
always remains at the end of the list :)

- Sylvester
 

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

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top