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

question on vector<T>

P: n/a
Consider the following program:

#include <iostream>
#include <vector>

using namespace std;

class Test
{
public:
Test();
friend ostream &operator <<(ostream &os, const Test &t);
private:
static int counter;
int value;
};

int Test::counter = 0;

Test::Test()
{
value = ++counter;
cout << "from default ctor " << value << endl;
}

ostream &operator <<(ostream &os, const Test &t)
{
os << t.value;
return os;
}

int main()
{
vector<Testtvec(10);

for (vector<Test>::iterator iter = tvec.begin();
iter != tvec.end();
++iter)
cout << *iter << endl;

return 0;
}

When I compile and run this program under VC++ 2005 Express Edition,
it prints the following

from default ctor 1
1
1
1
1
1
1
1
1
1
1

The default ctor is called only once.I thought default ctor should be
called 10 times because the statement contains tvec(10) as in
vector<Testtvec(10);
Where am I going wrong.

Also every Test object in tvec has the same value 1. Why is it so ?

Kindly explain.

Thanks
V.Subramanian

Jun 19 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a
su**************@yahoo.com, India wrote:
>
The default ctor is called only once.I thought default ctor should be
called 10 times because the statement contains tvec(10) as in
vector<Testtvec(10);
Where am I going wrong.

Also every Test object in tvec has the same value 1. Why is it so ?
It's because the default constructor is only called once. You also need
to instrument the copy constructor.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Jun 19 '07 #2

P: n/a
On Jun 19, 4:16 am, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
Consider the following program:

#include <iostream>
#include <vector>

using namespace std;

class Test
{
public:
Test();
friend ostream &operator <<(ostream &os, const Test &t);
private:
static int counter;
int value;

};

int Test::counter = 0;

Test::Test()
{
value = ++counter;
cout << "from default ctor " << value << endl;

}

ostream &operator <<(ostream &os, const Test &t)
{
os << t.value;
return os;

}

int main()
{
vector<Testtvec(10);

for (vector<Test>::iterator iter = tvec.begin();
iter != tvec.end();
++iter)
cout << *iter << endl;

return 0;

}

When I compile and run this program under VC++ 2005 Express Edition,
it prints the following

from default ctor 1
1
1
1
1
1
1
1
1
1
1

The default ctor is called only once.I thought default ctor should be
called 10 times because the statement contains tvec(10) as in
vector<Testtvec(10);
Where am I going wrong.

Also every Test object in tvec has the same value 1. Why is it so ?

Kindly explain.

Thanks
V.Subramanian
Try this

Test::Test()
{
value = counter;
cout << "from default ctor " << value << endl;

}

Test::Test(const Test& t)
{
value = ++counter;
cout << "from default copy ctor " << value << endl;

}

Now when you run it, it would output the followng
from default ctor 0
from default copy ctor 1
from default copy ctor 2
from default copy ctor 3
from default copy ctor 4
from default copy ctor 5
from default copy ctor 6
from default copy ctor 7
from default copy ctor 8
from default copy ctor 9
from default copy ctor 10
1
2
3
4
5
6
7
8
9
10

HTH

Jun 19 '07 #3

P: n/a
On Jun 19, 8:45 pm, Pete Becker <p...@versatilecoding.comwrote:
subramanian10...@yahoo.com, India wrote:
The default ctor is called only once.I thought default ctor should be
called 10 times because the statement contains tvec(10) as in
vector<Testtvec(10);
Where am I going wrong.
Also every Test object in tvec has the same value 1. Why is it so ?

It's because the default constructor is only called once. You also need
to instrument the copy constructor.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Why the copy constructor being called only once?

Regards,
Sarath

Jun 20 '07 #4

P: n/a
On Jun 19, 6:06 pm, Naresh Rautela <nraut...@gmail.comwrote:
On Jun 19, 4:16 am, "subramanian10...@yahoo.com, India"

<subramanian10...@yahoo.comwrote:
Consider the following program:
#include <iostream>
#include <vector>
using namespace std;
class Test
{
public:
Test();
friend ostream &operator <<(ostream &os, const Test &t);
private:
static int counter;
int value;
};
int Test::counter = 0;
Test::Test()
{
value = ++counter;
cout << "from default ctor " << value << endl;
}
ostream &operator <<(ostream &os, const Test &t)
{
os << t.value;
return os;
}
int main()
{
vector<Testtvec(10);
for (vector<Test>::iterator iter = tvec.begin();
iter != tvec.end();
++iter)
cout << *iter << endl;
return 0;
}
When I compile and run this program under VC++ 2005 Express Edition,
it prints the following
from default ctor 1
1
1
1
1
1
1
1
1
1
1
The default ctor is called only once.I thought default ctor should be
called 10 times because the statement contains tvec(10) as in
vector<Testtvec(10);
Where am I going wrong.
Also every Test object in tvec has the same value 1. Why is it so ?
Kindly explain.
Thanks
V.Subramanian

Try this

Test::Test()
{
value = counter;
cout << "from default ctor " << value << endl;

}

Test::Test(const Test& t)
{
value = ++counter;
cout << "from default copy ctor " << value << endl;

}

Now when you run it, it would output the followng
from default ctor 0
from default copy ctor 1
from default copy ctor 2
from default copy ctor 3
from default copy ctor 4
from default copy ctor 5
from default copy ctor 6
from default copy ctor 7
from default copy ctor 8
from default copy ctor 9
from default copy ctor 10
1
2
3
4
5
6
7
8
9
10

HTH
>From this I understand that the default ctor is called once and then
the copy ctor is called 10 times. Kindly let me know where this detail
is mentioned - is it defined in some book or defined in the ISO C++98
standard ?

Thanks
V.Subramanian

Jun 20 '07 #5

P: n/a

Sarath <CS*****@gmail.comwrote in message...
On Jun 19, 8:45 pm, Pete Becker <p...@versatilecoding.comwrote:
The default ctor is called only once.I thought default ctor should be
called 10 times because the statement contains tvec(10) as in
vector<Testtvec(10);
Where am I going wrong.
Also every Test object in tvec has the same value 1. Why is it so ?
It's because the default constructor is only called once. You also need
to instrument the copy constructor.

Why the copy constructor being called only once?
It's much easier to tell what you are doing if you copy your code (only
what's needed) into the post, so we can see it too.

{
vector<Testtvec(2);
/* You should see something like
Test Ctor // 1
Test Copy Ctor // 2
Test Copy Ctor // 3
Test Dtor // 1
*/
} // vector destructs
/*
Test Dtor // 3
Test Dtor // 2 (or 2, 3 ?)
*/

--
Bob R
POVrookie
Jun 20 '07 #6

P: n/a
On Jun 20, 10:43 am, Sarath <CSar...@gmail.comwrote:
On Jun 19, 8:45 pm, Pete Becker <p...@versatilecoding.comwrote:
subramanian10...@yahoo.com, India wrote:
The default ctor is called only once.I thought default ctor should be
called 10 times because the statement contains tvec(10) as in
vector<Testtvec(10);
Where am I going wrong.
Also every Test object in tvec has the same value 1. Why is it so ?
It's because the default constructor is only called once. You also need
to instrument the copy constructor.
--
-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)

Why the copy constructor being called only once?

Regards,
Sarath
Sorry for the type in my question, for the elements, why the
constructor only called once?

Jun 20 '07 #7

P: n/a
From this I understand that the default ctor is called once and then

the copy ctor is called 10 times. Kindly let me know where this detail
is mentioned - is it defined in some book or defined in the ISO C++98
standard ?

Thanks
V.Subramanian- Hide quoted text -

- Show quoted text -
Hmm. Let me take a shot at explaining what happens behind the scenes.

vector<Tt(n) is actually a constructor of type vector(size_type n,
const T& value = T(), const Allocator& = Allocator())

-The allocator first allocates memory to store the n objects i.e. it
allocates sizeof(A) * n bytes and returns the starting address
-An object is created from the default constructor
-Then the constructor calls _Ufill to copy this object to the memory
location provided by allocator. The copy constructor is used during
this process.
-Once this is done the object created in step 2 is destroyed.

HTH

Jun 20 '07 #8

P: n/a

"Sarath" <CS*****@gmail.comwrote in message
news:11**********************@j4g2000prf.googlegro ups.com...

The default ctor is called only once.I thought default ctor should be
called 10 times because the statement contains tvec(10) as in
vector<Testtvec(10);
Sorry for the type in my question, for the elements, why the
constructor only called once?
Because the vector's default constructor's parameters are two

vector(int num, T t = T())

The quoted code is equivalent to:

vector<Testtvec(10, Test());

So the Test's default constructor is called in the constructor's call, then
the copy constructor is used internally in that constructor to copy the
second parameter to each element of the vector.

--
Marco
Jun 20 '07 #9

P: n/a
On Jun 20, 10:18 am, Naresh Rautela <nraut...@gmail.comwrote:
Hmm. Let me take a shot at explaining what happens behind the scenes.

vector<Tt(n) is actually a constructor of type vector(size_type n,
const T& value = T(), const Allocator& = Allocator())

-The allocator first allocates memory to store the n objects i.e. it
allocates sizeof(A) * n bytes and returns the starting address
-An object is created from the default constructor
-Then the constructor calls _Ufill to copy this object to the memory
location provided by allocator. The copy constructor is used during
this process.
-Once this is done the object created in step 2 is destroyed.

HTH

I understood it now. Thank you very much. Kindly let me know the
material(ie book) or internet site where I can find such details about
STL.

Jun 21 '07 #10

P: n/a

<su**************@yahoo.comwrote in message...
>
I understood it now. Thank you very much. Kindly let me know the
material(ie book) or internet site where I can find such details about
STL.
SGI has the (dated) docs to the STL (most of which is now in the 'standard
C++ library').

Dinkumware has more up-to-date references.
http://www.dinkumware.com/manuals/.

"Thinking in C++", vol 2 has some on the 'standard C++ library':
(some minor errors due to changes in the standard after it's (TiCppv2) final
release.)

Get "Thinking in C++", 2nd ed. Volume 1&2 by Bruce Eckel
(available for free here. You can buy it in hardcopy too.):
http://www.mindview.net/Books/TICPP/...ngInCPP2e.html

--
Bob R
POVrookie
Jun 21 '07 #11

P: n/a
BobR <re***********@worldnet.att.netwrote:
>
<su**************@yahoo.comwrote in message...
>>
I understood it now. Thank you very much. Kindly let me know the
material(ie book) or internet site where I can find such details about
STL.

SGI has the (dated) docs to the STL (most of which is now in the 'standard
C++ library').

Dinkumware has more up-to-date references.
http://www.dinkumware.com/manuals/.
Josuttis's book is also a highly regarded reference.
http://www.josuttis.com/libbook/

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jun 21 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.