c++ - unordered_map.emplace 给出编译器时间错误?

标签 c++ c++11 dictionary containers

我将此容器作为类中的成员:

std::unordered_map<std::string, Fruit> m_fruits;

我想在同一个类中添加一个新元素,我尝试了两种方法,两种方法都应该基于示例工作。 (在 emplace 的页面上)但是我在某个地方犯了一个错误。 (fruitName 是一个 const std::string& )

m_fruits.emplace(fruitName, Fruit());

Error C2660 'std::pair::pair': function does not take 2 arguments

m_fruits.emplace(std::make_pair(fruitName, Fruit()));

Error C2440 '': cannot convert from 'initializer list' to '_Mypair'

水果类:

class Fruit {

public:
    Fruit(); 
    Fruit(const Fruit& fruit) = delete;
    Fruit operator=(const Fruit& fruit) = delete;
    virtual ~Fruit();
};

更新:

我发现我不应该删除 Fruit 的默认复制构造函数。

但是我不明白。 emplace 不是用于将对象构造到容器中,而不是在容器外部创建对象然后将其复制到容器中吗?

Inserts a new element into the container constructed in-place with the given args if there is no element with the key in the container.

请有人解释一下为什么我需要一个复制构造函数才能使用此方法。 谢谢!

最佳答案

这就是 std::pair 代码的作用

_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
    pair(const _T1& __x, const _T2& __y)
         : first(__x), second(__y) {}

根据上面的代码,参数是从复制或移动构造函数构造的。因此您需要其中之一。

Fruit 类没有定义复制构造函数或移动构造函数。在这里,m_fruits.emplace(fruitName, Fruit())编译器生成临时 Fruit 对象,该对象必须在映射内复制构造或移动构造。由于 Fruit 类的复制构造函数被删除并且没有移动构造函数,因此会出现编译器错误。 有两种方法可以消除此错误

1)引入移动构造函数

Fruit(Fruit && other) {
}

2) 或者不要删除复制构造函数,而是定义复制构造函数

水果(常量水果和其他){ }

这是工作片段

#include <iostream>
#include <unordered_map>

class Fruit {
public:
    Fruit() {}
    Fruit(const Fruit& fruit) {
    }
    Fruit operator=(const Fruit& fruit) = delete;
    ~Fruit() {}
};

int main() {
    std::unordered_map<std::string, Fruit> m_fruits;

    m_fruits.emplace("apple", Fruit());
    m_fruits.emplace(std::make_pair("orange", Fruit()));
    for (const auto & e: m_fruits) {
       std::cout << "key=" << e.first << std::endl;
    }
}

关于c++ - unordered_map.emplace 给出编译器时间错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41317880/

相关文章:

c++ - 在静态函数中初始化静态变量导致未解决

c++ - 抽象接口(interface)的单个​​派生是否会内联它的虚函数?

c++ - 将 STL 容器与 boost 范围适配器一起使用时出现 value_type 错误

c++ - Boost元函数类高阶函数

c++ - 是否可以创建模板别名?

c++ - operator =重载,终止符递归调用

std::search 的 C++ 二元谓词实现要求

algorithm - 以两个值作为键的哈希表

java - 如何通过集合改进 Map 值的排序

dictionary - 如何在 Go 中获取变量的内存大小?