如何解释有9次破坏? [英] How to explain that there are 9 times of destruction?

查看:69
本文介绍了如何解释有9次破坏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解 push_back() emplace_back()有什么区别,尤其是当元素是所以我写如下代码:

I want to learn what differences are between push_back() and emplace_back(), especially when the elements are class type.So I write code like following:

int cnt = 0;
class foo{
public:
    //constructor
    foo() : ucnt(++cnt) { cout << ucnt << "\tdefault" << endl; }
    foo(const foo&) : ucnt(++cnt) { cout << ucnt << "\tcopy" << endl; }
    ~foo() { cout << ucnt << "\tdestroyed" << endl; }
    int ucnt;
};

int main()
{
    vector<foo> vf = {foo()};
    cout << vf.size() << " : " << vf[0].ucnt << endl;
    vf.push_back(foo());
    cout << vf.size() << " : " << vf[0].ucnt << " " << vf[1].ucnt << endl;
    vf.emplace_back(foo());
    cout << vf.size() << " : " << vf[0].ucnt << " " << vf[1].ucnt << " " << vf[2].ucnt << endl;
    return 0;
}

其结果是:

1       default
2       copy
1       destroyed
1 : 2
3       default
4       copy
5       copy
2       destroyed
3       destroyed
2 : 5 4
6       default
7       copy
8       copy
9       copy
5       destroyed
4       destroyed
6       destroyed
3 : 8 9 7
8       destroyed
9       destroyed
7       destroyed

似乎 vf 中的所有元素都已复制,然后在执行<$ c时销毁了$ c> push_back()和 emplace_back()。为什么?

It seems like all the elements in vf are copyed and then destroyed while executing push_back() and emplace_back().Why?

推荐答案

emplace_back



emplace_back的优点是它将其参数直接传递给要放置的类的构造函数,并构造新的

emplace_back

The advantage of emplace_back is that it passes its arguments directly to the constructor of the to-be-emplaced class and constructs the new object in place instead of copy constructing it.

例如

elections.emplace_back("Nelson Mandela", "South Africa", 1994);

与push_back相反,在这里传递一个临时对象并复制构造新对象。
例如。

As opposed to push_back, where you pass a temporary object and copy construct the new object. e.g.

elections.push_back(President("Franklin Delano Roosevelt", "the USA", 1936));

所以在您的情况下应该是

so in your case it should be

vf.emplace_back();

因为您没有要传递给就位构造函数的ctor参数。与

since you have no ctor arguments to be passed to the in place constructor. Compared to

vf.push_back(foo());



构造函数余额



许多意外副本

Constructor balance

The many unexpected copies and deletes stem from resizing the vector.

1个临时创建并销毁构造函数
= 1x默认值,1x复制,1x被破坏✔️

1 temporary created, and destroyed in constructor = 1x default, 1x copy, 1x destroyed ✔️

在push_back
中创建和销毁1个临时对象将1个现有对象复制到调整大小的向量中,因此,副本创建和销毁
= 1x默认值,2x复制,2x销毁✔️

1 temporary created and destroyed in push_back 1 existing object copied to the resized vector, therefor copy-created and destroyed = 1x default, 2x copy, 2x destroyed ✔️

在emplace_back
中创建并销毁的1个临时对象,将2个现有对象复制到调整大小的向量中,从而进行复制创建并销毁
= 1x默认值,3x副本,3x销毁✔️

1 temporary created and destroyed in emplace_back 2 existing objects copied to the resized vector, therefor copy-created and destroyed = 1x default, 3x copy, 3x destroyed ✔️

等等

以下代码来自 Artemy Vysotsky ,请参见此答案下的注释,并确切地说明了它是如何完成的。权利。尤其要注意使用 .reserve(3)来避免重新分配狂欢。

The following code is from Artemy Vysotsky, see comment under this answer and shows exactly to the point how it is done right. Especially note the use of .reserve(3) to avoid reallocation orgies.

#include <vector>
#include <iostream>

using std::cout;

int cnt = 0;
class foo{
public:
    //constructor
    foo() : ucnt(++cnt) { cout << ucnt << "\tdefault\n" ; }
    foo(const foo&) : ucnt(++cnt) { cout << ucnt << "\tcopy\n" ; }
    foo(foo&&) noexcept : ucnt(++cnt) { cout << ucnt << "\tmove\n" ; }
    ~foo() { cout << ucnt << "\tdestroyed\n" ; }
    int ucnt;
};

int main()
{
    std::vector<foo> vf = {foo()};
    cout << vf.size() << " 1: " << vf[0].ucnt << '\n';
    vf.reserve(3);
    cout << vf.size() << " 2: " << vf[0].ucnt << '\n';
    vf.push_back(foo());
    cout << vf.size() << " 3: " << vf[0].ucnt << " " << vf[1].ucnt << '\n';
    vf.emplace_back();
    cout << vf.size() << " 4: " << vf[0].ucnt << " " << vf[1].ucnt << " " << vf[2].ucnt << '\n';
    return 0;
}
/***************
 Output
 $ ./test
 1       default
 2       copy
 1       destroyed
 1 1: 2
 3       move
 2       destroyed
 1 2: 3
 4       default
 5       move
 4       destroyed
 2 3: 3 5
 6       default
 3 4: 3 5 6
 3       destroyed
 5       destroyed
 6       destroyed
 *****************/

这篇关于如何解释有9次破坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆