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