是什么解释了这个片段中的许多动作和破坏

时间:2018-06-14 02:18:36

标签: c++ performance push-back

为了理解五的规则,我提出了这个:

#include <iostream>
#include <vector>

class A
{
public:
A(int y)
{
    std::cout << "constructed\n";
    x = new int[y];
}
~A()
{
    std::cout << "destructed\n";
    delete[] x;
}
A(const A &other)
{
    std::cout << "copied\n";
    if (other.x != nullptr)
        x = new int[sizeof(other.x) / sizeof(other.x[0])];
}
A(A && other) :
    x(other.x)
{
    std::cout << "moved\n";
    other.x = nullptr;
}
A& operator=(A other)
{
    std::cout << "assignments\n";
    std::swap(x, other.x);
    return *this;
}
int *x;
};

class B
{
private:
std::vector<A> a;
public:
B()
{
    for (int i = 0; i < 5; i++)
        a.emplace_back(A(1));
    std::cout << "----------------------\n";
    for (int i = 0; i < 5; i++)
        a.push_back({ 2 });
}
};

int main()
{
B *b = new B();
std::cin.get();
}

相反,如果我每次使用emplace_back和push_back 1次,我得到这个输出,无论我调用每个方法的顺序如何:

constructed
moved
destructed
----------------------
constructed
moved
destructed
moved
destructed

我接受了。但是,如果我使用上面写的代码,我会得到一些棘手的模式:

constructed
moved
destructed
constructed
moved
destructed
moved
destructed
constructed
moved
moved
destructed
destructed
moved
destructed
constructed
moved
moved
moved
destructed
destructed
destructed
moved
destructed
constructed
moved
moved
moved
moved
destructed
destructed
destructed
destructed
moved
destructed
----------------------
constructed
moved
destructed
constructed
moved
moved
moved
moved
moved
moved
destructed
destructed
destructed
destructed
destructed
destructed
moved
destructed
constructed
moved
destructed
constructed
moved
destructed
constructed
moved
moved
moved
moved
moved
moved
moved
moved
moved
destructed
destructed
destructed
destructed
destructed
destructed
destructed
destructed
destructed
moved
destructed

解释了这么多&#34;移动&#34;和#34;破坏&#34;?另外,我们是否可以通过此输出声明emplace_back优于push_back?

1 个答案:

答案 0 :(得分:-1)

我认为此输出不是错误

如果std :: vector的大小已满且你试图推送,vector会分配更多内存并将原始内存块中的对象移动到新的内存块并破坏原始内存块。

所以 你可以使用std :: vector.reserve()

#include <iostream>
#include <vector>

class A
{
public:
A(int y)
{
    std::cout << "constructed\n";
    x = new int[y];
}
~A()
{
    std::cout << "destructed\n";
    delete[] x;
}
A(const A &other)
{
    std::cout << "copied\n";
    if (other.x != nullptr)
        x = new int[sizeof(other.x) / sizeof(other.x[0])];
}
A(A && other) :
    x(other.x)
{
    std::cout << "moved\n";
    other.x = nullptr;
}
A& operator=(A other)
{
    std::cout << "assignments\n";
    std::swap(x, other.x);
    return *this;
}
int *x;
};

class B
{
private:
std::vector<A> a;
public:
B()
{
    //reserve
    a.reserve(5);

    for (int i = 0; i < 5; i++)
        a.emplace_back(A(1));
    std::cout << "----------------------\n";
   // for (int i = 0; i < 5; i++)
   //    a.push_back({ 2 });
}
};

int main()
{
B *b = new B();
std::cin.get();
}

此输出将如下

constructed
moved
destructed
constructed
moved
destructed
constructed
moved
destructed
constructed
moved
destructed
constructed
moved
destructed