我正在尝试实现一个 2D unordered_map ,如下所示:
std::unordered_map<std::string, std::unordered_map<std::string, double>>
首先,我通过执行以下操作实现了内部 unordered_graph:
std::unordered_map<std::string, std::unordered_map<std::string, double> *inner = new
std::unordered_map<std::string, std::unordered_map<std::string, double>>()
inner->insert(std::make_pair("X", 0));
然后,我尝试通过以下方式制作外部 unordered_map
std::unordered_map<std::string, std::unordered_map<std::string, double> *outer =
std::unordered_map<std::string, std::unordered_map<std::string, double>>()
outer->insert("X", inner);
但它给了我一个错误,说 no matching function for call to insert
最佳答案
您在此处使用的插入
错误:
outer->insert("X", inner);
它需要一个 value_type,即 std::pair。您传递的是两个参数而不是一个,因此您需要对这些参数执行 make_pair()
,另外您还需要传递一个值,因此 *inner
而不是 inner
这是一个指针。
一旦这一切都说完了,你可能会更好地使用不同的数据结构,因为哈希表的哈希表通常不是最有效的。
关于c++ - 创建 2d unordered_map C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25698409/