473,387 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

std::list.insert usage

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<intmylist;
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
Sl********@bls.gov
http://members.cox.net/slatteryt
Jan 10 '07 #1
4 6508

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<intmylist;
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
Sl********@bls.gov
http://members.cox.net/slatteryt
end() returns iterator which points behind the last element. Use back()
- it returns iterator pointing to the last element.

Jan 10 '07 #2
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().

Jan 10 '07 #3
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.
Jan 10 '07 #4

"Gianni Mariani" <gi*******@mariani.wswrote in message
news:45***********************@per-qv1-newsreader-01.iinet.net.au...
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
Jan 11 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Dave | last post by:
Hello all, After perusing the Standard, I believe it is true to say that once you insert an element into a std::list<>, its location in memory never changes. This makes a std::list<> ideal for...
5
by: Eric Lilja | last post by:
Hello, consider this complete program (sorry, it's not minimal but I hope it's readable at least): #include <algorithm> #include <iostream> #include <vector> class Row { public:
4
by: MikeB | last post by:
Hi, I've observed some odd behaviour in the std::list supplied with our development system. In the accompanying code, the list has at most one string on it, yet the program eventually runs out...
44
by: Josh Mcfarlane | last post by:
Just out of curiosity: When would using std::list be more efficient / effective than using other containers such as vector, deque, etc? As far as I'm aware, list doesn't appear to be...
5
by: gerg | last post by:
I'm having to deal with some legacy code which I think may be causeing some problems. Can STL pro's please add comments about the correctness of the following code: class A { public: /// ......
7
by: SpreadTooThin | last post by:
I want to replace an object in a list with a new object.... std::list<myObj>::iterator it; for (it=mylist.begin(); it != mylist.end(); ++it) { if (it->compair(myval) == 0) { *it = newval;...
1
by: Hochyy | last post by:
Hello, I'm having problems writing a code that will insert and deleteMember. I understand that there are methods written that can be simply called. Can anyone help? Thanks!
3
by: Mike Copeland | last post by:
How do I work with a std::list that might have multiple objects having the same "key", but which have other data that is different? Here's code that compiles, but doesn't do quite what I expect:...
17
by: Isliguezze | last post by:
Does anybody know how to make a wrapper for that iterator? Here's my wrapper class for std::list: template <class Tclass List { private: std::list<T*lst; public: List() { lst = new...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.