我是unordered_map的新手。我希望使用某些结构的特定字符串元素定义unordered_map哈希表键,该结构定义如下: - hashtable.cpp写在下面: -
#include <tr1/unordered_map>
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace std::tr1;
struct row{
string state;
int population;
};
struct total{
string key;
row value;
};
int main ()
{
total data;
unordered_map<data.key,data.value> country;
data.key="Australia";
data.value.state="Canberra";
data.value.population=12000;
return 0;
}
我收到了这样的错误: -
hashtable.cpp:17: error: ‘data’ cannot appear in a constant-expression
hashtable.cpp:17: error: ‘.’ cannot appear in a constant-expression
hashtable.cpp:17: error: ‘data’ cannot appear in a constant-expression
hashtable.cpp:17: error: ‘.’ cannot appear in a constant-expression
hashtable.cpp:17: error: template argument 1 is invalid
hashtable.cpp:17: error: template argument 2 is invalid
hashtable.cpp:17: error: template argument 3 is invalid
hashtable.cpp:17: error: template argument 4 is invalid
hashtable.cpp:17: error: template argument 5 is invalid
hashtable.cpp:17: error: invalid type in declaration before ‘;’ token
答案 0 :(得分:0)
你做错了。也许,正确的方法是
int main ()
{
total data{"Australia", {"Canbeera", 12000}};
unordered_map<std::string,row> country;
count.insert({ data.key, { data.value } });
return 0;
}
选中此page以获取参考
答案 1 :(得分:0)
unordered_map<data.key,data.value> country;
你想要的是c ++ 11及以上的内容
unordered_map<decltype(data.key),decltype(data.value)> country;
因为你无论如何都使用了tr1扩展,我建议你为你的编译启用至少C ++ 11语法,并从std::
获取曾经的tr1内容。