By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,599 Members | 1,267 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,599 IT Pros & Developers. It's quick & easy.

STL vector push_backpack

P: n/a
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;
}
Nov 16 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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;
}
Nov 16 '06 #2

P: n/a
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
cl*******@gmail.com
Nov 16 '06 #3

P: n/a
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.
Nov 16 '06 #4

P: n/a
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.
Nov 16 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.