473,756 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::list.inser t 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<intmy list;
std::list<int>: :iterator myiter;
mylist.push_bac k(1);
mylist.push_bac k(2);
mylist.push_bac k(3);
mylist.push_bac k(4);
mylist.push_bac k(5);
myiter = mylist.end();
mylist.push_bac k(6);
mylist.push_bac k(7);
mylist.push_bac k(8);
mylist.push_bac k(9);

mylist.insert(m yiter, 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 6549

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<intmy list;
std::list<int>: :iterator myiter;
mylist.push_bac k(1);
mylist.push_bac k(2);
mylist.push_bac k(3);
mylist.push_bac k(4);
mylist.push_bac k(5);
myiter = mylist.end();
mylist.push_bac k(6);
mylist.push_bac k(7);
mylist.push_bac k(8);
mylist.push_bac k(9);

mylist.insert(m yiter, 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*******@mari ani.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(con t.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
5633
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 storing vertices of an arbitrary n-ary tree where a vertex contain pointers to its parent / children. These parent / child vertices need to stay put if we've got pointers to them somewhere! Am I correct in my assertion?
5
1786
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
2673
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 of heap. However, if instead of using a std::list I use a std::deque, the program doesn't run out of heap. Can anyone explain the behaviour of std::list or is it a bug ? TIA,
44
3872
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 specialized for anything. Thanks, Josh McFarlane
5
22835
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: /// ... assume constructors and such are defined char* sz; // pointer to memory buffer };
7
2160
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
1727
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
1636
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: (Please note that there's some specialized I/o code here, but the logic flow should be clear...) struct GenCheck // Gender Check data { char genCode; int useCount;
17
4189
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 std::list<T>(); } List(const List<T&rhs) { lst = new std::list<T>(*rhs.lst); } List(int n, const T& value) { lst = new std::list<T>(n, value); }
0
9455
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9838
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9708
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8709
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7242
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5140
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3354
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2665
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.