【发布时间】:2016-02-07 12:31:55
【问题描述】:
我是 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
【问题讨论】:
-
你需要用类型实例化
std::unordered_map:unordered_map<std::string,row> country;
标签: c++ struct hashtable unordered-map