Inserting an empty element in a list (STL)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Massimiliano Alberti

    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>::ite rator 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;
    }
  • WW

    #2
    Re: Inserting an empty element in a list (STL)

    Massimiliano Alberti wrote:[color=blue]
    > 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>::ite rator 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;
    > }[/color]

    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


    Comment

    • Massimiliano Alberti

      #3
      Re: Inserting an empty element in a list (STL)

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

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

      Comment

      • WW

        #4
        Re: Inserting an empty element in a list (STL)

        Massimiliano Alberti wrote:[color=blue][color=green]
        >> 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.[/color]
        >
        > 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]
        > :-)[/color]

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

        --
        WW aka Attila


        Comment

        Working...