C++ 应用中的 map

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)
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值