std :: vector构造函数不为每个元素调用对象构造函数吗?

时间:2023-01-18 16:58:11

My code resembles something along these lines.

我的代码类似于这些内容。

class A
{
  public:
    A(int i) { printf("hello %d\n", i); }
    ~A() { printf("Goodbye\n"); }
}


std::vector(10, A(10));

I notice that hello prints out once. It seems to imply that the vector only allocates space for the element but doesn't construct it. How do I make it construct 10 A objects?

我注意到你好打印一次。这似乎意味着向量只为元素分配空间,但不构造它。如何构建10个A对象?

4 个解决方案

#1


12  

The object is constructed only once, as you pass it to std::vector. Then this object is copied 10 times. You have to do a printf in the copy constructor to see it.

当你将它传递给std :: vector时,该对象只构造一次。然后将此对象复制10次。您必须在复制构造函数中执行printf才能看到它。

#2


6  

You forgot the copy constructor:

你忘记了复制构造函数:

#include <iostream>
#include <vector>
using namespace std;

class A
{
        int i;
public:
        A(int d) : i(d) { cout << "create i=" << i << endl; }
        ~A() { cout << "destroy" << endl; }
        A(const A& d) : i(d.i) { cout << "copy i=" << d.i << endl; }
};

int main()
{
        vector<A> d(10,A(10));
}

Output:

create i=10
copy i=10
copy i=10
copy i=10
copy i=10
copy i=10
copy i=10
copy i=10
copy i=10
copy i=10
copy i=10
destroy
destroy
destroy
destroy
destroy
destroy
destroy
destroy
destroy
destroy
destroy

#3


3  

A(10) constructs a temporary object. Only once. The 10 elements of your vector are constructed via the copy-constructor. So if you define a copy-constructor to print B you'll get 10 B's.

A(10)构造临时对象。只有一次。向量的10个元素是通过复制构造函数构造的。因此,如果您定义一个复制构造函数来打印B,那么您将获得10个B。

#4


1  

Define copy constructor and you will be fine:

定义复制构造函数,你会没事的:

#include <cstdio>
#include <vector>


class A
{
    public:
        A(int i) { printf("hello %d\n", i); }
        ~A() { printf("Goodbye\n"); }
        A(const A&)
        {
            printf("copy constructing\n");
        }
};


int main()
{
    std::vector< A > a( 10, A(5) );
}

#1


12  

The object is constructed only once, as you pass it to std::vector. Then this object is copied 10 times. You have to do a printf in the copy constructor to see it.

当你将它传递给std :: vector时,该对象只构造一次。然后将此对象复制10次。您必须在复制构造函数中执行printf才能看到它。

#2


6  

You forgot the copy constructor:

你忘记了复制构造函数:

#include <iostream>
#include <vector>
using namespace std;

class A
{
        int i;
public:
        A(int d) : i(d) { cout << "create i=" << i << endl; }
        ~A() { cout << "destroy" << endl; }
        A(const A& d) : i(d.i) { cout << "copy i=" << d.i << endl; }
};

int main()
{
        vector<A> d(10,A(10));
}

Output:

create i=10
copy i=10
copy i=10
copy i=10
copy i=10
copy i=10
copy i=10
copy i=10
copy i=10
copy i=10
copy i=10
destroy
destroy
destroy
destroy
destroy
destroy
destroy
destroy
destroy
destroy
destroy

#3


3  

A(10) constructs a temporary object. Only once. The 10 elements of your vector are constructed via the copy-constructor. So if you define a copy-constructor to print B you'll get 10 B's.

A(10)构造临时对象。只有一次。向量的10个元素是通过复制构造函数构造的。因此,如果您定义一个复制构造函数来打印B,那么您将获得10个B。

#4


1  

Define copy constructor and you will be fine:

定义复制构造函数,你会没事的:

#include <cstdio>
#include <vector>


class A
{
    public:
        A(int i) { printf("hello %d\n", i); }
        ~A() { printf("Goodbye\n"); }
        A(const A&)
        {
            printf("copy constructing\n");
        }
};


int main()
{
    std::vector< A > a( 10, A(5) );
}