Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How does the constructor of vector work?

Reply
Thread Tools

How does the constructor of vector work?

 
 
hellwolf
Guest
Posts: n/a
 
      07-26-2004
Hi,everyone.Because of my English level,I will try to use code to explain
where I confused.

//list of code:
#include <iostream>
#include <algorithm>
#include <vector>

class A{
static int si;
int i;
public:
A():i(si++){}
void show()const{std::cout << i << std::endl;}
};
int A::si(0);

class funA{
public:
void operator ()(A& a){a.show();}
};

int main(){
std::vector<A> veca(10);
std::for_each(veca.begin(),veca.end(),funA());
//system("pause");
}


result:
0
0
0
0
0
0
0
0
0
0

So ,isn't the vector call the constructor function for each member??


 
Reply With Quote
 
 
 
 
Rob Williscroft
Guest
Posts: n/a
 
      07-26-2004
hellwolf wrote in news:ce38ed$1r2n$(E-Mail Removed)99.com in comp.lang.c++:

> So ,isn't the vector call the constructor function for each member??
>


No it default constructs 1 object and then copy's that object for
each member.

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      07-26-2004

"hellwolf" <(E-Mail Removed)> wrote in message
news:ce38ed$1r2n$(E-Mail Removed)99.com...
> Hi,everyone.Because of my English level,I will try to use code to explain
> where I confused.
>
> //list of code:
> #include <iostream>
> #include <algorithm>
> #include <vector>
>
> class A{
> static int si;
> int i;
> public:
> A():i(si++){}


Add this copy constructor

A(const A&):i(si++){}

> void show()const{std::cout << i << std::endl;}
> };
> int A::si(0);
>
> class funA{
> public:
> void operator ()(A& a){a.show();}
> };
>
> int main(){
> std::vector<A> veca(10);
> std::for_each(veca.begin(),veca.end(),funA());
> //system("pause");
> }
>
>
> result:
> 0
> 0
> 0
> 0
> 0
> 0
> 0
> 0
> 0
> 0
>
> So ,isn't the vector call the constructor function for each member??
>


Yes but it calls the copy constructor.

std::vector<A> veca(10);

is the same as

std::vector<A> veca(10, A());

The second parameter A() is copied into each of the vector elements.

john


 
Reply With Quote
 
hellwolf
Guest
Posts: n/a
 
      07-27-2004

"John Harrison" <(E-Mail Removed)> дϢ
news:(E-Mail Removed)...
>
> "hellwolf" <(E-Mail Removed)> wrote in message
> news:ce38ed$1r2n$(E-Mail Removed)99.com...
> > Hi,everyone.Because of my English level,I will try to use code to

explain
> > where I confused.
> >
> > //list of code:
> > #include <iostream>
> > #include <algorithm>
> > #include <vector>
> >
> > class A{
> > static int si;
> > int i;
> > public:
> > A():i(si++){}

>
> Add this copy constructor
>
> A(const A&):i(si++){}
>
> > void show()const{std::cout << i << std::endl;}
> > };
> > int A::si(0);
> >
> > class funA{
> > public:
> > void operator ()(A& a){a.show();}
> > };
> >
> > int main(){
> > std::vector<A> veca(10);
> > std::for_each(veca.begin(),veca.end(),funA());
> > //system("pause");
> > }
> >
> >
> > result:
> > 0
> > 0
> > 0
> > 0
> > 0
> > 0
> > 0
> > 0
> > 0
> > 0
> >
> > So ,isn't the vector call the constructor function for each member??
> >

>
> Yes but it calls the copy constructor.
>
> std::vector<A> veca(10);
>
> is the same as
>
> std::vector<A> veca(10, A());
>
> The second parameter A() is copied into each of the vector elements.
>
> john
>
>

Thank you all , but how can I call the constructor for each member(a for
loop?)


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      07-27-2004
On Tue, 27 Jul 2004 08:19:34 +0800, hellwolf <(E-Mail Removed)>
wrote:

>
> "John Harrison" <(E-Mail Removed)> дÈëÏûÏ¢
> news:(E-Mail Removed)...
>>
>> "hellwolf" <(E-Mail Removed)> wrote in message
>> news:ce38ed$1r2n$(E-Mail Removed)99.com...
>> > Hi,everyone.Because of my English level,I will try to use code to

> explain
>> > where I confused.
>> >
>> > //list of code:
>> > #include <iostream>
>> > #include <algorithm>
>> > #include <vector>
>> >
>> > class A{
>> > static int si;
>> > int i;
>> > public:
>> > A():i(si++){}

>>
>> Add this copy constructor
>>
>> A(const A&):i(si++){}
>>
>> > void show()const{std::cout << i << std::endl;}
>> > };
>> > int A::si(0);
>> >
>> > class funA{
>> > public:
>> > void operator ()(A& a){a.show();}
>> > };
>> >
>> > int main(){
>> > std::vector<A> veca(10);
>> > std::for_each(veca.begin(),veca.end(),funA());
>> > //system("pause");
>> > }
>> >
>> >
>> > result:
>> > 0
>> > 0
>> > 0
>> > 0
>> > 0
>> > 0
>> > 0
>> > 0
>> > 0
>> > 0
>> >
>> > So ,isn't the vector call the constructor function for each member??
>> >

>>
>> Yes but it calls the copy constructor.
>>
>> std::vector<A> veca(10);
>>
>> is the same as
>>
>> std::vector<A> veca(10, A());
>>
>> The second parameter A() is copied into each of the vector elements.
>>
>> john
>>
>>

> Thank you all , but how can I call the constructor for each member(a for
> loop?)
>


Well you are calling a constructor, it's just not the constructor you
thought it would be.

You could use a for loop

std::vector<A> veca;
for (int i = 0; i < 10; ++i)
veca.push_back(A());

but that looks exactly the same as your old code.

What do you think is wrong with this?

std::vector<A> veca(10);

If you say what you think is wrong, we might be able to help you better.

john
 
Reply With Quote
 
Sandeep
Guest
Posts: n/a
 
      07-27-2004
Rob Williscroft <(E-Mail Removed)> wrote in message news:<Xns9532A9C8DC3B8ukcoREMOVEfreenetrtw@130.133 .1.4>...
> hellwolf wrote in news:ce38ed$1r2n$(E-Mail Removed)99.com in comp.lang.c++:
>
> > So ,isn't the vector call the constructor function for each member??
> >

>
> No it default constructs 1 object and then copy's that object for
> each member.
>
> HTH.
>
> Rob.


I was trying that, but got more puzzled.
this code:
______________________________
#include <iostream>
#include <algorithm>
#include <vector>

class A{
static int si;
int i;
public:
A():i(si++){}
A(const A& obj):i(si++){}
void show()const{std::cout << i << std::endl;}
};
int A::si(0);

class funA{
public:
void operator ()(A& a){a.show();}
};

int main(){
std::vector<A> veca;
for(int i = 0;i < 10;++i){
veca.push_back(A());
}
std::for_each(veca.begin(),veca.end(),funA());
return 0;
}
___________________________________
gives results
24
25
26
27
28
29
30
31
32
34

But if I remove the copy constructor, I get
0
1
2
3
4
5
6
7
8
9

Please mention why this is happening

Regards
Sandeep
 
Reply With Quote
 
Jeff Flinn
Guest
Posts: n/a
 
      07-27-2004

"Sandeep" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> Rob Williscroft <(E-Mail Removed)> wrote in message

news:<Xns9532A9C8DC3B8ukcoREMOVEfreenetrtw@130.133 .1.4>...
>
> I was trying that, but got more puzzled.
> this code:
> ______________________________
> #include <iostream>
> #include <algorithm>
> #include <vector>
>
> class A{
> static int si;
> int i;
> public:
> A():i(si++){}
> A(const A& obj):i(si++){}
> void show()const{std::cout << i << std::endl;}
> };
> int A::si(0);
>
> class funA{
> public:
> void operator ()(A& a){a.show();}
> };
>
> int main(){
> std::vector<A> veca;
> for(int i = 0;i < 10;++i){
> veca.push_back(A());
> }


You are default-constructing 10 locally scoped(temporary) A's. push_back
copy constructs each of these ( another 10 ), and the temporaries go out of
scope and are destructed. push_back reallocates space and again copy
constructs an additional 5 A's when there isn't enough space to hold the
additional item. The reallocation strategy may vary with implementations.

> std::for_each(veca.begin(),veca.end(),funA());
> return 0;
> }
> ___________________________________
> gives results
> 24
> 25
> 26
> 27
> 28
> 29
> 30
> 31
> 32
> 34
>
> But if I remove the copy constructor, I get
> 0
> 1
> 2
> 3
> 4
> 5
> 6
> 7
> 8
> 9


Jeff F


 
Reply With Quote
 
Rob Williscroft
Guest
Posts: n/a
 
      07-27-2004
Sandeep wrote in news:(E-Mail Removed) om in
comp.lang.c++:

> Please mention why this is happening


You're not measuring construction and copy-construction
seperatly, when the vector resizes all its elements
are copy-constructed in the new location.

#include <iostream>
#include <algorithm>
#include <vector>

struct A
{
static int si, ci;
int i, j;
A() : i(si++), j(0) {}
A( A const & obj ) : i(obj.i), j(ci++) {}
};

int A::si = 0, A::ci = 0;

struct funA
{
void operator ()(A const & a)
{
std::cout << a.i << " copy " << a.j << '\n';
}
};

int main()
{
std::vector<A> veca;

//veca.reserve( 10 );

for(int i = 0;i < 10;++i)
{
veca.push_back(A());
}

std::for_each( veca.begin(), veca.end(), funA() );
}

I Get:

0 copy 15
1 copy 16
2 copy 17
3 copy 18
4 copy 19
5 copy 20
6 copy 21
7 copy 22
8 copy 23
9 copy 24

But if I uncomment the line:

//veca.reserve( 10 )

in main I get:

0 copy 0
1 copy 1
2 copy 2
3 copy 3
4 copy 4
5 copy 5
6 copy 6
7 copy 7
8 copy 8
9 copy 9


Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
Initializing vector<vector<int> > and other vector questions... pmatos C++ 6 04-26-2007 05:39 PM
Initializing a vector of a vector in a constructor fatgirl.brown@gmail.com C++ 4 11-12-2006 09:22 AM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM



Advertisments