6

私は次の束を保存しています

struct Article {
    std::string title;
    unsigned db_id;     // id field in MediaWiki database dump
};

次のように定義された Boost.MultiIndex コンテナ内

typedef boost::multi_index_container<
    Article,
    indexed_by<
        random_access<>,
        hashed_unique<tag<by_db_id>,
                      member<Article, unsigned, &Article::db_id> >,
        hashed_unique<tag<by_title>,
                      member<Article, std::string, &Article::title> >
    >
> ArticleSet;

これで、 からのイテレータと からのイテレータの 2 つができましindex<by_title>index<by_id>。にデータ メンバーを追加せずに、これらをコンテナーのランダム アクセス部分にインデックスに変換する最も簡単な方法は何struct Articleですか?

4

2 に答える 2

6

すべてのインデックスは、iterator_toを使用した値によるイテレータの生成をサポートしています。あるインデックスのターゲット値へのイテレータがすでにある場合は、これを使用して別のインデックスのイテレータに変換できます。

iterator       iterator_to(const value_type& x);
const_iterator iterator_to(const value_type& x)const;

インデックスへの変換については、次のモデルに従うことができますrandom_access_index.hpp

  iterator erase(iterator first,iterator last)
  {
    BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(first);
    BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(last);
    BOOST_MULTI_INDEX_CHECK_IS_OWNER(first,*this);
    BOOST_MULTI_INDEX_CHECK_IS_OWNER(last,*this);
    BOOST_MULTI_INDEX_CHECK_VALID_RANGE(first,last);
    BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
    difference_type n=last-first;
    relocate(end(),first,last);
    while(n--)pop_back();
    return last;
  }
于 2010-11-18T18:03:06.050 に答える
6

iterator_toBoostの比較的新しい機能です(1.35以降にあります)。デフォルトのインデックスで使用する場合、構文シュガーが少し追加されます。Boost の古いバージョンでは、関数projectが唯一の選択肢です。project次のように使用できます。

ArticleSet x;
// consider we've found something using `by_db_id` index
ArticleSet::index_const_iterator<by_db_id>::type it = 
  x.get<by_db_id>().find( SOME_ID );

// convert to default index ( `random_access<>` )
ArticleSet::const_iterator it1 = x.project<0>( it );
// iterator_to looks like:
ArticleSet::const_iterator it11 = x.iterator_to( *it );

// convert to index tagged with `by_title` tag
ArticleSet::index_const_iterator<by_title>::type it2 = x.project<by_title>( it );
// iterator_to doen't look better in this case:
ArticleSet::index_const_iterator<by_title>::type it2 = x.get<by_title>().iterator_to( *it );

// etc.
于 2010-11-18T18:43:11.867 に答える