map是c++的一个标准容器,提供了很好的一对一的关系。对于迭代器来说,可以修改实值,而不能修改key。
std:map <int, string> personnel;
参数的第一个为 key 的类型,第二个为 value 的类型。
enum class ac_class : int
{
none = 1,
ic,
pm
}
using us_ac = std::function<int(ac_class, const Value &)>; /* 接口 */
class bd_class
{
public:
map<ac_class, us_ac> ac_list; /* 接口列表 */
int deca(const char *data, int len);
}
int bd_class::trh(ac_class type, us_ac &&xif)
{
ac_list[type] = xif;
// ac_list.insert(pair<ac_class, us_ac>(type, xif));
return 0;
}
int bd_class::deca(const char *data, int len)
{
Value root;
ac_class type = (ac_class)root["cmd"].asInt();
auto it = ac_list.find(type); // find 返回一个迭代器指向键值为key的元素
if (ac_list.cend() == it) // cend 用于测试迭代器是否超过了其范围的末尾
return 0;
return it->second(type, root); // second 为(key)键值对应的数值(value)
}