加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 从Boost多索引迭代器获取数字索引

发布时间:2020-12-16 03:43:56 所属栏目:百科 来源:网络整理
导读:我存储了一堆以下内容 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_uniquetagby_db_i
我存储了一堆以下内容
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>,std::string,&Article::title> >
    >
> ArticleSet;

现在我有两个迭代器,一个来自索引< by_title>和索引< by_id>中的一个.将这些转换为索引到容器的随机访问部分的最简单方法是什么,而不向结构文章添加数据成员?

解决方法

每个索引都支持使用 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;
  }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读