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

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值