从 Boost 多索引迭代器获取数字索引
我将以下一堆内容存储
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;
现在我有两个迭代器,一个来自 index
,一个来自 index
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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每个索引都支持使用 iterator_to。如果一个索引中已有指向目标值的迭代器,则可以使用它转换为另一索引中的迭代器。
要转换为索引,您可以遵循 random_access_index.hpp 中的模型:
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.
For conversion to index you can likely follow the model in
random_access_index.hpp
:iterator_to
< /a> 是 Boost 中相对较新的函数(从 1.35 开始就有)。与默认索引一起使用时,它添加了一些语法糖。对于旧版本的 Boost,函数project
是唯一的选择。您可以使用项目
如下:
iterator_to
is a relatively new function in Boost (it's there since 1.35). It adds a little of the syntax sugar when using with default index. For older versions of Boost the functionproject
is the only choise. You can useproject
as follows: