3

私の要件には、次のようなマップが必要です。

pair<string key1, string key2> _keypair;
map<_keypair, shared_ptr<classX>>

私のニーズは次のとおりです。

  1. key1 と key2 のペアは一意である必要があります。

  2. key1 と Key2 のペアを使用してアクセスできるはずです。

  3. 入れる。

  4. 複合キーを使用して削除する

私は出くわしましboost::multi_indexたが、それについてははっきりしていません。誰かが私の状況の例を教えてもらえますか?

4

1 に答える 1

0

このようなものです(私はこのコードをコンパイルしませんでした):

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);
于 2013-08-21T15:37:30.043 に答える