Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > std::list.insert usage

Reply
Thread Tools

std::list.insert usage

 
 
Tim Slattery
Guest
Posts: n/a
 
      01-10-2007
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(;
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
http://members.cox.net/slatteryt
 
Reply With Quote
 
 
 
 
Ondra Holub
Guest
Posts: n/a
 
      01-10-2007

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(;
> 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 Removed)
> http://members.cox.net/slatteryt


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

 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      01-10-2007
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().

 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      01-10-2007
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.
 
Reply With Quote
 
Sylvester Hesp
Guest
Posts: n/a
 
      01-11-2007

"Gianni Mariani" <(E-Mail Removed)> wrote in message
news:45a55351$0$22065$(E-Mail Removed)...
> 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().
>


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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the difference between Memory Usage and Heap Usage in my JVMMetrics ? Krist Java 8 02-10-2010 12:44 AM
retrieving CPU Usage and Memory Usage information in JAVA hvt Java 0 03-13-2007 01:09 PM
retrieving CPU Usage and Memory Usage information in JAVA hvt Java 0 03-13-2007 01:07 PM
Webchecker Usage - a problem with local usage Colin J. Williams Python 1 02-26-2004 12:28 AM
Need help on memory usage VS PF usage metfan Java 2 10-21-2003 01:58 PM



Advertisments