STL vector push_backpack

Discussion in 'C++' started by Brian C, Nov 16, 2006.

  1. Brian C

    Brian C Guest

    Hello all,
    I was playing around (don't know why, perhaps bored at work) with the
    vector class. This was on an AIX machine using IBM's xlC compiler.
    Anyway, I wrote a simple class, and inserted it a couple of times into
    a vector. What I found odd was that when I inserted the 2nd item, I saw
    that it called my copy constructor for the 1st item and the 2nd item.
    When I inserted the 3rd object, it called the copy constructor for the
    1st,2nd,3rd item, etc.
    I got home and rewrote it quick and tested it under VC++, and it does
    the same thing. Below is the code I ran on the VC++ compiler. I
    understand the copy of the object I'm push_back()'ing, but why each item
    already in the vector?

    P.S. the code may not be pretty, just wrote it up quick for the test.

    #include <iostream>
    #include <vector>
    #include <string>

    using namespace std;

    class DemoClass
    {
    public:
    DemoClass() { cout << "default ctor" << endl; }
    DemoClass(string Name) { Data=Name; cout << "ctor " << Data << endl; }
    virtual ~DemoClass() { cout << "dtor " << Data << endl; }

    DemoClass(const DemoClass &dc) { Data=dc.Data; cout << "copy ctor " <<
    Data << endl; }
    DemoClass &operator=(const DemoClass &dc) { Data=dc.Data; return(*this); }
    private:
    string Data;
    };

    int main(void)
    {
    vector<DemoClass> Vector;

    DemoClass dc1("Hi");
    DemoClass dc2("there");
    DemoClass dc3("friend.");

    cout << "Pushing 1..." << endl;
    Vector.push_back(dc1);
    cout << "Pushing 2..." << endl;
    Vector.push_back(dc2);
    cout << "Pushing 3..." << endl;
    Vector.push_back(dc3);

    cout << "Done" << endl;
    }
    Brian C, Nov 16, 2006
    #1
    1. Advertising

  2. Brian C

    Larry Smith Guest

    Brian C wrote:
    > Hello all,
    > I was playing around (don't know why, perhaps bored at work) with
    > the vector class. This was on an AIX machine using IBM's xlC compiler.
    > Anyway, I wrote a simple class, and inserted it a couple of times
    > into a vector. What I found odd was that when I inserted the 2nd item, I
    > saw that it called my copy constructor for the 1st item and the 2nd
    > item. When I inserted the 3rd object, it called the copy constructor for
    > the 1st,2nd,3rd item, etc.
    > I got home and rewrote it quick and tested it under VC++, and it
    > does the same thing. Below is the code I ran on the VC++ compiler. I
    > understand the copy of the object I'm push_back()'ing, but why each item
    > already in the vector?
    >
    > P.S. the code may not be pretty, just wrote it up quick for the test.
    >
    > #include <iostream>
    > #include <vector>
    > #include <string>
    >
    > using namespace std;
    >
    > class DemoClass
    > {
    > public:
    > DemoClass() { cout << "default ctor" << endl; }
    > DemoClass(string Name) { Data=Name; cout << "ctor " << Data << endl; }
    > virtual ~DemoClass() { cout << "dtor " << Data << endl; }
    >
    > DemoClass(const DemoClass &dc) { Data=dc.Data; cout << "copy ctor "
    > << Data << endl; }
    > DemoClass &operator=(const DemoClass &dc) { Data=dc.Data;
    > return(*this); }
    > private:
    > string Data;
    > };
    >
    > int main(void)
    > {
    > vector<DemoClass> Vector;
    >


    // reserve memory in advance for 3 DemoClass objects.
    // otherwise, as we add to the vector it will have to
    // reallocate space for the larger array, copy all
    // existing entries to the new array, then delete them
    // from the old array
    Vector.reserve(3);


    > DemoClass dc1("Hi");
    > DemoClass dc2("there");
    > DemoClass dc3("friend.");
    >
    > cout << "Pushing 1..." << endl;
    > Vector.push_back(dc1);
    > cout << "Pushing 2..." << endl;
    > Vector.push_back(dc2);
    > cout << "Pushing 3..." << endl;
    > Vector.push_back(dc3);
    >
    > cout << "Done" << endl;
    > }
    Larry Smith, Nov 16, 2006
    #2
    1. Advertising

  3. Brian C wrote:
    > Hello all,
    > I was playing around (don't know why, perhaps bored at work) with
    > the vector class. This was on an AIX machine using IBM's xlC compiler.
    > Anyway, I wrote a simple class, and inserted it a couple of times
    > into a vector. What I found odd was that when I inserted the 2nd item, I
    > saw that it called my copy constructor for the 1st item and the 2nd
    > item. When I inserted the 3rd object, it called the copy constructor for
    > the 1st,2nd,3rd item, etc.
    > I got home and rewrote it quick and tested it under VC++, and it
    > does the same thing. Below is the code I ran on the VC++ compiler. I
    > understand the copy of the object I'm push_back()'ing, but why each item
    > already in the vector?


    Because they have to be copied when the vector is resized.


    --
    Clark S. Cox III
    Clark S. Cox III, Nov 16, 2006
    #3
  4. Brian C

    Brian C Guest

    Clark S. Cox III wrote:
    > Brian C wrote:
    >> Hello all,
    >> I was playing around (don't know why, perhaps bored at work) with
    >> the vector class. This was on an AIX machine using IBM's xlC compiler.
    >> Anyway, I wrote a simple class, and inserted it a couple of times
    >> into a vector. What I found odd was that when I inserted the 2nd item, I
    >> saw that it called my copy constructor for the 1st item and the 2nd
    >> item. When I inserted the 3rd object, it called the copy constructor for
    >> the 1st,2nd,3rd item, etc.
    >> I got home and rewrote it quick and tested it under VC++, and it
    >> does the same thing. Below is the code I ran on the VC++ compiler. I
    >> understand the copy of the object I'm push_back()'ing, but why each item
    >> already in the vector?

    >
    > Because they have to be copied when the vector is resized.
    >
    >

    Makes sense, can't believe I forgot that, thanks.
    Brian C, Nov 16, 2006
    #4
  5. Brian C

    Brian C Guest

    Larry Smith wrote:
    > Brian C wrote:
    >> Hello all,
    >> I was playing around (don't know why, perhaps bored at work) with
    >> the vector class. This was on an AIX machine using IBM's xlC compiler.
    >> Anyway, I wrote a simple class, and inserted it a couple of times
    >> into a vector. What I found odd was that when I inserted the 2nd item, I
    >> saw that it called my copy constructor for the 1st item and the 2nd
    >> item. When I inserted the 3rd object, it called the copy constructor for
    >> the 1st,2nd,3rd item, etc.
    >> I got home and rewrote it quick and tested it under VC++, and it
    >> does the same thing. Below is the code I ran on the VC++ compiler. I
    >> understand the copy of the object I'm push_back()'ing, but why each item
    >> already in the vector?
    >>
    >> P.S. the code may not be pretty, just wrote it up quick for the test.
    >>
    >> #include <iostream>
    >> #include <vector>
    >> #include <string>
    >>
    >> using namespace std;
    >>
    >> class DemoClass
    >> {
    >> public:
    >> DemoClass() { cout << "default ctor" << endl; }
    >> DemoClass(string Name) { Data=Name; cout << "ctor " << Data << endl; }
    >> virtual ~DemoClass() { cout << "dtor " << Data << endl; }
    >>
    >> DemoClass(const DemoClass &dc) { Data=dc.Data; cout << "copy ctor "
    >> << Data << endl; }
    >> DemoClass &operator=(const DemoClass &dc) { Data=dc.Data;
    >> return(*this); }
    >> private:
    >> string Data;
    >> };
    >>
    >> int main(void)
    >> {
    >> vector<DemoClass> Vector;
    >>

    >
    > // reserve memory in advance for 3 DemoClass objects.
    > // otherwise, as we add to the vector it will have to
    > // reallocate space for the larger array, copy all
    > // existing entries to the new array, then delete them
    > // from the old array
    > Vector.reserve(3);
    >
    >
    >> DemoClass dc1("Hi");
    >> DemoClass dc2("there");
    >> DemoClass dc3("friend.");
    >>
    >> cout << "Pushing 1..." << endl;
    >> Vector.push_back(dc1);
    >> cout << "Pushing 2..." << endl;
    >> Vector.push_back(dc2);
    >> cout << "Pushing 3..." << endl;
    >> Vector.push_back(dc3);
    >>
    >> cout << "Done" << endl;
    >> }

    Makes sense, can't believe I forgot that, thanks.
    Brian C, Nov 16, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dennis
    Replies:
    1
    Views:
    2,581
    Dennis
    Jun 7, 2004
  2. CD
    Replies:
    2
    Views:
    806
    Victor Bazarov
    Oct 5, 2004
  3. Replies:
    2
    Views:
    415
  4. Replies:
    8
    Views:
    1,915
    Csaba
    Feb 18, 2006
  5. Luca Risolia

    STL map to STL vector

    Luca Risolia, Jan 13, 2014, in forum: C++
    Replies:
    32
    Views:
    366
    Seungbeom Kim
    Jan 18, 2014
Loading...

Share This Page