469,621 Members | 1,678 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,621 developers. It's quick & easy.

Inserting an empty element in a list (STL)

How can I insert an empty element in a list? The insert method has as
a parameter the source object to insert... So you have to use it in
this way:
(where MyObject is an object with only a member, x (an integer))
Normally a copy constructor is longer to execute than a simple
constructor, and this example code uses the constructor once (for the
obj object) and the copy constructor five times (to add the elements
in the list)

list <MyObject>::iterator L_Iter;
list<MyObject> L;
MyObject obj;

for (int i = 1 ; i < 6 ; i++ )
{
L_Iter = L.insert(L.end(), obj);
L_Iter->x = i;
}
Jul 19 '05 #1
3 2058
WW
Massimiliano Alberti wrote:
How can I insert an empty element in a list? The insert method has as
a parameter the source object to insert... So you have to use it in
this way:
(where MyObject is an object with only a member, x (an integer))
Normally a copy constructor is longer to execute than a simple
constructor, and this example code uses the constructor once (for the
obj object) and the copy constructor five times (to add the elements
in the list)

list <MyObject>::iterator L_Iter;
list<MyObject> L;
MyObject obj;

for (int i = 1 ; i < 6 ; i++ )
{
L_Iter = L.insert(L.end(), obj);
L_Iter->x = i;
}


This is how STL works. Annoying, but this is the way. Some of us were
porposing to look at possible templated solution (so it would try to use
your int to construct the "object inside") but the Goths (Gurus Of The Holy
Standard) said that its stupid, it is only our problem noone else has it
etc. At that time I was receiving 5 mails per day saying: I have that
problem too...

Thanx God in your case it does not matter. If obj has an inlinable, simple
constructor it does not matter if you pass in an int or an obj. What it
will end up being is copying the int.

--
WW aka Attila
Jul 19 '05 #2
> Thanx God in your case it does not matter. If obj has an inlinable, simple
constructor it does not matter if you pass in an int or an obj. What it
will end up being is copying the int.


I thought it was quite obvious that it was only a sample... and then
you can't know what are the other members of that class... You know
only what I've initialized... Perhaps there is a char buffer[10000]
:-)
Jul 19 '05 #3
WW
Massimiliano Alberti wrote:
Thanx God in your case it does not matter. If obj has an inlinable,
simple constructor it does not matter if you pass in an int or an
obj. What it will end up being is copying the int.


I thought it was quite obvious that it was only a sample... and then
you can't know what are the other members of that class... You know
only what I've initialized... Perhaps there is a char buffer[10000]
:-)


I know it is not true. You have a long double unused[10000] ;-)

--
WW aka Attila
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by bill | last post: by
25 posts views Thread by Matthias | last post: by
10 posts views Thread by Ryan Graham | last post: by
6 posts views Thread by Allerdyce.John | last post: by
59 posts views Thread by Steve R. Hastings | last post: by
6 posts views Thread by Jakob Bieling | last post: by
2 posts views Thread by barcaroller | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.