473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

STL vector push_backpack

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
4 1488
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: {AGUT2}=IWIK= | last post by:
Hello all, It's my fisrt post here and I am feeling a little stupid here, so go easy.. :) (Oh, and I've spent _hours_ searching...) I am desperately trying to read in an ASCII...
9
by: luigi | last post by:
Hi, I am trying to speed up the perfomance of stl vector by allocating/deallocating blocks of memory manually. one version of the code crashes when I try to free the memory. The other version...
7
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using...
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.