Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
<queue>'s emplace 和 push有什么区别?
<queue>
这是关于std::queue::emplace和std::queue::push 的解释。
两种方法都在其当前最后一个元素之后添加元素,返回None。
None
push() 将已构造对象的副本作为参数添加到队列中,它采用队列元素类型的对象。
emplace() 在队列末尾就地构造一个新对象。它将队列的元素类型构造函数采用的参数作为参数。
如果您的使用模式是创建一个新对象并将其添加到容器中,您可以使用 emplace() 来缩短几个步骤(创建一个临时对象并复制它)。