Inserting an empty element in a list (STL)

  • Thread starter Massimiliano Alberti
  • Start date
M

Massimiliano Alberti

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;
}
 
W

WW

Massimiliano said:
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.
 
M

Massimiliano Alberti

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]
:)
 
W

WW

Massimiliano said:
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] ;-)
 

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,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top