STL vector push_backpack

B

Brian C

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

Larry Smith

Brian said:
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);
 
C

Clark S. Cox III

Brian said:
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.
 
B

Brian C

Larry said:
// 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);
Makes sense, can't believe I forgot that, thanks.
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top