默认哈希函数只适用于基本数据类型(包括 string 类型),而不适用于自定义的结构体或者类。
unordered_map详解
struct QStringHash {
size_t operator()(const QString& str) const {
return qHash(str);
}
};
int dataTypeSize(const QString& datatype) {
static const std::unordered_map<QString, int, QStringHash> typeSizes = {
{"int", sizeof(int)},
{"short", sizeof(short)},
{"float", sizeof(float)},
{"double", sizeof(double)},
{"byte", 1},
{"bit", 1},
};
auto it = typeSizes.find(datatype);
return (it != typeSizes.end()) ? it->second : 1;
}