473,322 Members | 1,307 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,322 software developers and data experts.

question on vector<T>

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
11 1377
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
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
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
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

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
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
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

"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
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

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

Similar topics

0
by: Marc Schellens | last post by:
my dinkumware docu says, vector<...>::rbegin() returns an iterator which points just BEYOND the end of the controlled sequence. Is that true? so I cannot say: for( riter i=v.rbegin(); i !=...
2
by: john smith | last post by:
Hi, when there is a vector<> of pointers to some objects, does calling resize cause vector to call delete on each object, or is there a memory leak problem? for example: struct base {//some...
10
by: Stefan Höhne | last post by:
Hi, as I recon, std::vector::clear()'s semantics changed from MS VC++ 6.0 to MS' DOT.NET - compiler. In the 6.0 version the capacity() of the vector did not change with the call to...
6
by: Joe | last post by:
I have a: vector<string> which contains a few dozen elements. I want to find the index of the element containing a certain string. for example: vector<string> strings;...
4
by: Anu | last post by:
Hi, We have a class that has its own overloaded operator new and whose prototype seems to correspond to the standard placement new :- class AppClass { public: operator new (size_t size,...
5
by: Numeromancer | last post by:
From the C++-FAQ Lite: http://www.parashift.com/c++-faq-lite/containers.html#faq-34.3 ---------------------------- 34.3] Is the storage for a std::vector<Tguaranteed to be contiguous? Yes. ...
18
by: subramanian100in | last post by:
Consider a class that has vector< pair<int, string>* c; as member data object. I need to use operator>to store values into this container object and operator<< to print the contents of the...
8
by: jacek.dziedzic | last post by:
Hi! I need to be able to track memory usage in a medium-sized application I'm developing. The only significant (memory-wise) non- local objects are of two types -- std::vector<and of a custom...
1
by: iammilind | last post by:
In one of my code, I was using vector<> for certain class. In one of my struct, I have 'const' member data. However, vector<>::clear() throws compile error with that:...
4
by: iammilind | last post by:
I am not able to understand why the 2nd ~Test() is called with different 'this' but the same string value ?? Also why does it give error, if I want to declare Test::Str as a const string member ?? ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.