从Boost多索引迭代器获取数字索引 [英] Get numeric index from Boost multi-index iterator

查看:564
本文介绍了从Boost多索引迭代器获取数字索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我存储了以下一组

  struct文章{
std :: string title;
unsigned db_id; // MediaWiki数据库转储中的id字段
};在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& >
>
>文章集;



现在我有两个迭代器,一个来自 index< by_title> 和一个来自 index< by_id> 。在 struct Article


中添加数据成员时,将这些索引转换为容器的随机访问部分的最简单方法是什么? div class =h2_lin>解决方案

每个索引都支持使用iterator_to 。如果你已经在一个索引中有一个迭代器到目标值,你可以使用它来转换为另一个索引中的迭代器。

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

对于转换到索引,你可以按照 random_access_index.hpp

 迭代器擦除(迭代器第一,迭代器最后)
{
BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR (第一);
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;
}


I'm storing a bunch of the following

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

in a Boost.MultiIndex container, defined as

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;

Now I've got two iterators, one from index<by_title> and one from index<by_id>. What is the easiest way to transform these to indexes into the random access part of the container, without adding a data member to struct Article?

解决方案

Every index supports generation of an iterator by value using iterator_to. If you already have an iterator to the target value in one index, you could use this to convert to an iterator in another index.

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

For conversion to index you can likely follow the model in 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;
  }

这篇关于从Boost多索引迭代器获取数字索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆