本文介绍了C ++:堆栈的push()与emplace()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图了解使用 push() emplace()的区别std :: stack

Trying to understand the difference between using push() or emplace() for std::stack.

我在想,如果我创建 std :: stack< int> ,那么我会使用 push(),因为整数是原始类型, emplace()不需要构造。

I was thinking that if I create a std::stack<int>, then I'd use push() because integer is a primitive type and there is nothing for emplace() to construct.

但是,如果我正在创建 std :: stack< string> ,那么我会选择 emplace()是因为 std :: string 是一个对象。

However, if I was creating std::stack<string> then I'd choose emplace() because std::string is an object.

这是正确用法吗?

推荐答案

全面了解什么emplace_back确实,必须首先了解可变参数模板和右值引用。

To fully understand what emplace_back does, one must first understand variadic templates and rvalue references.

这是现代C ++中相当高级且深刻的概念。在地图上,它会被标记为有龙。

This is a fairly advanced, and deep concept in modern C++. On a map, it would be labeled "there be dragons".

您说您是C ++的新手,并试图学习这些东西。这可能不是您要找的答案,但是您现在应该跳过此详细信息,等到您将脑袋围绕可变参数模板和右值引用后再回来。

You say that you're new to C++ and trying to learn this stuff. This may not be the answer you might be looking for, but you should skip this detail for now, and come back later after you've wrapped your brain around variadic templates and rvalue references. Then it should all make sense.

但是,如果您坚持:对于包含简单,基本类型(如整数)的容器,几乎没有任何区别。区别在于容器的类型是一些大型的,复杂的类,带有复杂的构造函数和/或复制构造函数。

But if you insist: for a container containing simple, elementary types like integers, there's little, if any difference. The difference comes when the container's type is some large, sophisticated class, with a complicated constructor, and/or copy-constructor.

push或emplace的最终结果是完全一样100%一样容器将附加另一个元素。区别在于该元素来自何处:

The end result of either push or emplace is exactly, 100%, the same. The container gets another element appended to it. The difference is where the element comes from:

1)push接受一个现有元素,并将其副本附加到容器中。简单,直接。 push始终只使用一个参数,即要复制到容器中的元素。

1) push takes an existing element, and appends a copy of it to the container. Simple, straightforward. push always takes exactly one argument, the element to copy to the container.

2)emplace在容器中创建了该类的另一个实例,该实例已附加到容器中。放置的参数将作为参数转发给容器的类的构造函数。如果类具有默认构造函数,则Emplace可以具有一个参数,多个参数或完全没有参数。

2) emplace creates another instance of the class in the container, that's already appended to the container. The arguments to emplace are forwarded as arguments to the container's class's constructor. Emplace can have either one argument, more than one argument, or no argument at all, if the class has a default constructor.

请注意,当类的构造函数采用一个参数时并且未将其标记为 explicit ,可能会滥用push并将其传递给构造函数参数,而不是该类的现有实例。但是,让我们假装该选项不存在,它通常会导致糟糕的代码性能,尤其是对于非平凡的类。

Note that when the class's constructor takes one argument and it is not marked as explicit, it's possible to abuse push and pass it a constructor argument, instead of an existing instance of the class. But let's pretend that this option does not exist, it often leads to horrible code performance, especially with non-trivial classes.

所以:如果您想添加一个该容器的类的现有实例,请使用push。如果要从头开始创建该类的新实例,请使用emplace。

So: if you want to add a copy of an existing instance of the class to the container, use push. If you want to create a new instance of the class, from scratch, use emplace.

这篇关于C ++:堆栈的push()与emplace()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 04:48