参考:What data structure should I use to model a database/table?
如何表示table类型的数据结构:
表头是age和name,对应一个record
┃ |10 |....┃
┠────┼────┼────┨
┃John│data│....┃
┃....│....│....┃
┃....│....│....┃
比较简单的方式是使用std::map转化这个table,按照行列值生成map的key:
uint16_t key GenKey(int age, std::string name) {
return (age << 8) + (uint8_t)std::hash<std::string>()(name);
}
std::map<key, data> records= {
{ GenKey(10, "John"), data}, // 实际的写法为 { (10 << 8) + (uint8_t)std::hash<std::string>()(John), data},
...
};
record GetRecord(uint16_t key) {
record empty;
if( records.find(key) != records.end()) {
return records[key];
} else {
printf("Record not found");
return empty;
}
}