C++ 如何使用boost::multi_索引组合键

C++ 如何使用boost::multi_索引组合键,c++,boost,C++,Boost,我的需求需要一张这样的地图: pair<string key1, string key2> _keypair; map<_keypair, shared_ptr<classX>> pair\u密钥对; 地图 我的需要是: 键1和键2对必须是唯一的 我应该能够使用key1和Key2对进行访问 插入 使用复合键删除 我遇到了boost::multi_index,但我对此不太清楚。有人能给我举个例子说明我的情况吗?smth(我没有编译这段代码): struct

我的需求需要一张这样的地图:

pair<string key1, string key2> _keypair;
map<_keypair, shared_ptr<classX>>
pair\u密钥对;
地图
我的需要是:

  • 键1和键2对必须是唯一的

  • 我应该能够使用key1和Key2对进行访问

  • 插入

  • 使用复合键删除

  • 我遇到了
    boost::multi_index
    ,但我对此不太清楚。有人能给我举个例子说明我的情况吗?

    smth(我没有编译这段代码):

    struct value\u类型
    {
    std::字符串键1;
    std::字符串键2;
    std::共享ptr ptr;
    };
    //索引标记(否则在get()对应索引时必须使用number)
    结构综合指数;
    结构键1_索引;
    结构键2_索引;
    使用名称空间boost::multi_索引;
    typedef多索引容器<
    值类型
    ,由<
    有序_唯一<
    标签
    ,复合_键<
    值类型
    ,委员
    ,委员
    >
    >
    ,有序\u非唯一<
    标签
    ,委员
    >
    >
    ,有序\u非唯一<
    标签
    ,委员
    >
    >
    >指数型;
    a型指数;
    a、 插入({“key-1”、“key-2”,std::shared_ptr(新的some_类(…)});
    //按键1_索引(或键2_索引)查找
    自动范围=a.get().相等范围(“键-1”);
    //按综合指数查找
    autoit=a.get().find(std::make_tuple(“key-1”、“key-2”);
    //通过任何迭代器类型擦除
    a、 抹去(它);
    
    谢谢zaufi,它工作得很好,只是我不得不使用boost::make_tuple而不是std::make_tuple。但是我在删除它时遇到了一个问题,显示错误“没有用户定义的转换运算符可以执行此转换,或者该运算符不能被称为”有什么想法吗?
    struct value_type
    {
        std::string key1_;
        std::string key2_;
        std::shared_ptr<some_class> ptr_;
    };
    
    // index tags (otherwise you have to use number when get<N>() corresponding index)
    struct composite_index;
    struct key1_index;
    struct key2_index;
    
    using namespace boost::multi_index;
    typedef multi_index_container<
        value_type
      , indexed_by<
            ordered_unique<
                tag<composite_index>
              , composite_key<
                    value_type
                  , member<value_type, std::string, &value_type::key1_>
                  , member<value_type, std::string, &value_type::key2_>
                  >
              >
          , ordered_non_unique<
                tag<key1_index>
              , member<value_type, std::string, &value_type::key1_>
              >
          >
          , ordered_non_unique<
                tag<key2_index>
              , member<value_type, std::string, &value_type::key2_>
              >
          >
      > index_type;
    
    index_type a;
    a.insert({"key-1", "key-2", std::shared_ptr<some_class>(new some_class(...)});
    
    // find by key1_index (or key2_index)
    auto range = a.get<key1_index>().equal_range("key-1");
    
    // find by composite index
    auto it = a.get<composite_index>().find(std::make_tuple("key-1", "key-2"));
    
    // erase by any iterator type
    a.erase(it);