提高:: multi_index_container的:查找其他指标的迭代指数迭代器 [英] boost::multi_index_container: Find index iterator from other index iterator
问题描述
我有2索引的索引的multi_index_container的。我能找到其中一人的价值,但有可能找到其他相应索引的迭代器?
I have a multi_index_container indexed by 2 indexes. I'm able to find the value by one of them, but is it possible to find the iterator from the other corresponding index?
例如:
struct ById{};
struct ByName{};
typedef multi_index_container<
MyStruct,
indexed_by<
ordered_unique<tag<ById>, member< MyStruct, int, &MyStruct::id> >,
ordered_non_unique<tag<BySalary>, member< MyStruct, int, &MyStruct::salary> >
>
> MyStructsContainer;
typedef MyStructsContainer::index<ById>::type MyStructsContainerById;
typedef MyStructsContainer::index<BySalary>::type MyStructsContainerBySalary;
...
MyStructsContainerById& byId = myStructsContainer.get<ById>();
MyStructsContainerById::iterator itById = byId.find(3);
现在的问题是,有一个简单的方法来找到对应的:
The question is, is there an easy way to find the corresponding:
MyStructsContainerByName::iterator itBySalary
指向的值完全相同(* itById)?
which points to the exact same value ( *itById) ?
谢谢,
卡林
推荐答案
您正在寻找的boost :: multi_index ::项目
<一个href=\"http://www.boost.org/doc/libs/1_36_0/libs/multi_index/doc/reference/multi_index_container.html#projection\" rel=\"nofollow\">http://www.boost.org/doc/libs/1_36_0/libs/multi_index/doc/reference/multi_index_container.html#projection
给定一个的multi_index_container
与指数I1和I2,我们说比I1-迭代器 IT1
和i2-迭代器 IT2
是等价的,如果:
Given a
multi_index_container
with indices i1 and i2, we say than an i1-iteratorit1
and an i2-iteratorit2
are equivalent if:
-
IT1 == i1.end()
和IT2 == i2.end()
it1==i1.end()
ANDit2==i2.end()
或
-
IT1
和IT2
指向同一个元素。
it1
andit2
point to the same element.
在你的样品
auto& byId = myStructsContainer.get<ById>();
auto itById = byId.find(3);
您可以使用
MyStructsContainerByName::iterator itBySalary = project<1>(
myStructsContainer, itById);
或事实上:
auto itBySalary = project<BySalary>(myStructsContainer, itById);
使用效果完全一样。
这篇关于提高:: multi_index_container的:查找其他指标的迭代指数迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!