How does the constructor of vector work?

H

hellwolf

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

Rob Williscroft

hellwolf wrote in 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.
 
J

John Harrison

hellwolf said:
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
 
H

hellwolf

John Harrison said:
Add this copy constructor

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


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?)
 
J

John Harrison

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
 
S

Sandeep

Rob Williscroft said:
hellwolf wrote in in comp.lang.c++:


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
 
J

Jeff Flinn

Sandeep said:
Rob Williscroft <[email protected]> wrote in message
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
 
R

Rob Williscroft

Sandeep wrote in 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top