C++之std::queue::emplace

std::queue::emplace 是 C++ STL 中 std::queue 容器的成员函数,它用于在队列的末尾就地构造一个新元素。这个函数类似于 std::queue::push,但是 emplace 允许你通过传递参数来构造元素,而不需要显式地创建一个元素对象。

理解 std::queue::emplace

在理解 std::queue::emplace 之前,需要先了解几个重要的概念:

  1. Queue(队列):是一种先进先出(FIFO)的数据结构,允许在队尾添加元素,在队头移除元素。

  2. emplace 操作:是 C++11 引入的特性,允许在容器中就地构造元素,而不需要显式地创建一个对象。

使用 std::queue::emplace

std::queue::emplace 的签名通常如下:

复制代码

cpp

复制代码
template <typename... Args>
void emplace(Args&&... args);

它接受可变数量的参数 args,这些参数会被传递给元素类型的构造函数。具体地说,当你调用 emplace 时,参数会被传递给底层元素类型的构造函数,从而在队列的末尾构造一个新的元素。

示例

下面是一个简单的示例,展示了如何使用 std::queue::emplace:

复制代码

cpp

复制代码
#include <iostream>
#include <queue>

struct MyStruct {
    int x;
    std::string str;

    MyStruct(int a, std::string s) : x(a), str(std::move(s)) {
        std::cout << "Constructor called with values: " << x << ", " << str << std::endl;
    }
};

int main() {
    std::queue<MyStruct> myQueue;

    // 使用 emplace 添加元素
    myQueue.emplace(10, "Hello");

    // 使用 emplace 添加另一个元素
    myQueue.emplace(20, "World");

    // 遍历队列中的元素(这里只是简单输出,实际应用可以根据需求处理)
    while (!myQueue.empty()) {
        auto& front = myQueue.front();
        std::cout << "Element: " << front.x << ", " << front.str << std::endl;
        myQueue.pop();
    }

    return 0;
}

在这个示例中:

  • MyStruct 是一个简单的结构体,它有一个带参数的构造函数。
  • std::queue<MyStruct> 是一个存储 MyStruct 对象的队列。
  • myQueue.emplace(10, "Hello"); 和 myQueue.emplace(20, "World"); 分别在队列中构造了两个 MyStruct 对象。
  • 最后,通过遍历队列并输出每个元素的值来展示结果。

与 push 的区别

  • std::queue::push 是传统的添加元素方式,需要传递一个已经创建好的对象。
  • std::queue::emplace 则是直接在队列中构造元素,可以避免额外的复制或移动操作,特别是对于非默认构造的对象类型来说更为高效。

总结来说,std::queue::emplace 提供了一种方便和高效的方式来在队列中添加元素,并且避免了不必要的对象复制或移动操作。

那么myQueue.emplace(10, "Hello"); 相当于myQueue.push(MyStruct(10, "Hello"); 吗?

从最终效果来看,myQueue.emplace(10, "Hello"); 和 myQueue.push(MyStruct(10, "Hello")); 在结果上是等效的:两种方式都会在队列末尾添加一个 MyStruct 对象。然而,两者之间有一些重要的差异,特别是在性能和对象构造方面:

  1. 对象构造:

    • emplace 直接在容器内部构造对象,通过传递给 emplace 的参数来调用元素类型的构造函数。
    • push 则需要先在外部创建一个对象,然后将这个对象复制或移动到容器中。
  2. 性能:

    • emplace 可以避免不必要的临时对象创建和复制操作,因此通常比 push 更高效,特别是在元素类型具有复杂构造函数或禁止复制的情况下。
    • push 可能涉及对象的复制或移动,具体取决于元素类型的构造函数和赋值运算符。

让我们通过实际代码来对比一下:

复制代码

cpp

复制代码
#include <iostream>
#include <queue>
#include <string>

struct MyStruct {
    int x;
    std::string str;

    MyStruct(int a, std::string s) : x(a), str(std::move(s)) {
        std::cout << "Constructor called with values: " << x << ", " << str << std::endl;
    }

    // 复制构造函数
    MyStruct(const MyStruct& other) : x(other.x), str(other.str) {
        std::cout << "Copy constructor called\n";
    }

    // 移动构造函数
    MyStruct(MyStruct&& other) noexcept : x(other.x), str(std::move(other.str)) {
        std::cout << "Move constructor called\n";
    }
};

int main() {
    std::queue<MyStruct> myQueue;

    // 使用 emplace 添加元素
    std::cout << "Using emplace:\n";
    myQueue.emplace(10, "Hello");

    // 使用 push 添加元素
    std::cout << "\nUsing push:\n";
    myQueue.push(MyStruct(20, "World"));

    return 0;
}

输出可能类似于:

复制代码
复制代码
Using emplace:
Constructor called with values: 10, Hello

Using push:
Constructor called with values: 20, World
Move constructor called

在使用 emplace 时,构造函数只被调用一次,因为对象是直接在队列中构造的。而在使用 push 时,构造函数和移动构造函数都被调用,这表明对象在创建后被移动到了队列中。

结论

  • emplace 在很多情况下比 push 更高效,因为它可以避免额外的对象复制或移动操作。
  • 如果你希望直接在容器中构造对象并减少开销,应该优先选择 emplace。
相关推荐
破茧蝴蝶2 小时前
Python分布式爬虫完整教程
开发语言·python
狗凯之家源码网2 小时前
全新 PHP 轻量客服聊天室源码深度评测
开发语言·php·聊天室系统
远离UE43 小时前
UE5 Substrate Thin Film 源码拆析
开发语言·javascript·ue5
keyipatience3 小时前
JavaScript学习(1)书写位置,变量、数据类型、运算符
开发语言·前端·javascript·css·学习
一拳一个呆瓜3 小时前
【STL】文件系统导航
c++·stl
All for pursuit.3 小时前
【动态规划-21】312.戳气球
数据结构·c++·算法·leetcode·动态规划
栈帧之外4 小时前
【C++】STL之set
开发语言·c++
catchadmin4 小时前
免费可商用 PHP 管理后台 CatchAdmin V5.4.1 发布,新增 MCP 支持
开发语言·php
zhanghaha13145 小时前
C++构造函数的一些其他应用(逐词逐句保姆讲解)
c++
jimy16 小时前
c++的 Type Traits (二、更详细版本)
开发语言·c++