1

このコードは、ブースト多重指数「mru」の例から採用されています。

http://www.boost.org/doc/libs/1_46_1/libs/multi_index/example/serialization.cpp

boost :: unordered_mapと同様のことをしているコードがありますが、この例からmru機能を追加したいと思います。

このコードをできるだけboost::unordered_mapに近づけて機能させたいと思います。私にとって重要な機能は、unordered_mapの[]演算子です。

main()の最後の行は壊れており、コメントとして各行の上にあるのが私の質問です。

すべての回答コメントに事前に感謝します。

#include <algorithm>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <iostream>

using namespace boost::multi_index;

class my_struct {
public:
    my_struct(int in_a, std::string in_b) : a(in_a), b(in_b) {}

    int a;
    std::string b;

    bool operator==(const my_struct &rhs) const
    {
        return (a == rhs.a);
    }
    bool operator!=(const my_struct &rhs) const
    {
            return !(*this == rhs);
    }

    friend std::ostream& operator<<(std::ostream &out, const my_struct&ms);
};

std::ostream& operator<<(std::ostream &out, my_struct &ms)
{
    out << ms.a << " " << ms.b << std::endl;
    return out;
}

inline std::size_t
hash_value(const my_struct &val)
{
    return boost::hash_value(val.a);
}

// tags for multi_index
struct umap {};
template <typename Item>
class mru_list
{
  typedef multi_index_container<
    Item,
    indexed_by<
      sequenced<>,
      hashed_unique<boost::multi_index::tag<umap>, identity<Item> >
    >
  > item_list;

public:

  typedef Item                         item_type;
  typedef typename item_list::iterator iterator;

  mru_list(std::size_t max_num_items_):max_num_items(max_num_items_){}

  void insert(const item_type& item)
  {
    std::pair<iterator,bool> p=il.push_front(item);

    if(!p.second){                     /* duplicate item */
      il.relocate(il.begin(),p.first); /* put in front */
    }
    else if(il.size()>max_num_items){  /* keep the length <= max_num_items */
      il.pop_back();
    }
  }

  iterator begin(){return il.begin();}
  iterator end(){return il.end();}

//private:
  item_list   il;
  std::size_t max_num_items;
};

int main()
{
    mru_list<my_struct> mru(10);
    my_struct one(1, "One");

    mru.insert(one);
    mru.insert(my_struct(2, "Two"));
    mru.insert(my_struct(3, "Three"));
    mru.insert(one);

    std::cout<<"most recently entered terms:"<<std::endl;
    for (mru_list<my_struct>::iterator itr = mru.begin(); itr != mru.end(); ++itr) {
        std::cout << itr->a << std::endl;
    }

    // what is my return type?
    mru.il.get<umap>();

    // Why doesn't this work?
    mru_list<my_struct>::iterator itr = mru.il.get<umap>().find(one);

    // Why doesn't this have a [] operator like boost:unordered_map
    mru.il.get<umap>()[1] = "foobar";

    return 0;
}
4

1 に答える 1

2

//リターンタイプは何ですか?

mru.il.get<umap>();

その戻りタイプは、umapインデックスのタイプです。これは次のとおりです。

  typedef typename boost::multi_index::index<
        item_list
      , umap
      >::type hashed_index_t;

  mru_list<my_struct>::hashed_index_t& hashed_index = mru.il.get<umap>();

C ++ 11では、次のようにすると簡単になりautoます。

  auto& hashed_index = mru.il.get<umap>();

//なぜこれが機能しないのですか?

mru_list<my_struct>::iterator itr = mru.il.get<umap>().find(one);

find()(2番目の)インデックスのイテレータを返し、umap上記のステートメントはそれを最初のインデックスのイテレータに割り当てています。同じマルチインデックスコンテナのあるインデックスイテレータタイプから別のイテレータタイプに変換するための射影操作があります。例:

mru_list<my_struct>::iterator itr = project<0>(mru.il, hashed_index.find(one));

//これにboost:unordered_mapのような[]演算子がないのはなぜですか

理由は言えませんが、そうではありません

于 2012-05-30T08:35:43.297 に答える