How does the constructor of vector work?

Discussion in 'C++' started by hellwolf, Jul 26, 2004.

  1. hellwolf

    hellwolf Guest

    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??
    hellwolf, Jul 26, 2004
    #1
    1. Advertising

  2. hellwolf wrote in news:ce38ed$1r2n$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/
    Rob Williscroft, Jul 26, 2004
    #2
    1. Advertising

  3. "hellwolf" <> wrote in message
    news:ce38ed$1r2n$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
    John Harrison, Jul 26, 2004
    #3
  4. hellwolf

    hellwolf Guest

    "John Harrison" <> дÈëÏûÏ¢
    news:...
    >
    > "hellwolf" <> wrote in message
    > news:ce38ed$1r2n$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?)
    hellwolf, Jul 27, 2004
    #4
  5. On Tue, 27 Jul 2004 08:19:34 +0800, hellwolf <>
    wrote:

    >
    > "John Harrison" <> ôÈëÃûâ
    > news:...
    >>
    >> "hellwolf" <> wrote in message
    >> news:ce38ed$1r2n$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
    John Harrison, Jul 27, 2004
    #5
  6. hellwolf

    Sandeep Guest

    Rob Williscroft <> wrote in message news:<Xns9532A9C8DC3B8ukcoREMOVEfreenetrtw@130.133.1.4>...
    > hellwolf wrote in news:ce38ed$1r2n$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
    Sandeep, Jul 27, 2004
    #6
  7. hellwolf

    Jeff Flinn Guest

    "Sandeep" <> wrote in message
    news:...
    > Rob Williscroft <> 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
    Jeff Flinn, Jul 27, 2004
    #7
  8. Sandeep wrote in news: 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/
    Rob Williscroft, Jul 27, 2004
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page