我是unordered_map.I的新手,我想用某些结构的特定字符串元素来定义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发布于 2015-11-06 11:22:55
你做错了。也许,正确的方法是
int main ()
{
total data{"Australia", {"Canbeera", 12000}};
unordered_map<std::string,row> country;
count.insert({ data.key, { data.value } });
return 0;
}检查此页面以供参考
发布于 2015-11-06 11:38:05
unordered_map<data.key,data.value> country;
你想要的显然是来自c++11和上面的东西
unordered_map<decltype(data.key),decltype(data.value)> country;因为您无论如何都使用tr1扩展,所以我建议您至少为编译启用C++11语法,并从std::获取曾经是tr1的内容。
https://stackoverflow.com/questions/33565472
复制相似问题