C++ 多索引容器:从任意索引获取相等的范围,只实现一次循环
我有一个C++ 多索引容器:从任意索引获取相等的范围,只实现一次循环,c++,boost,boost-multi-index,C++,Boost,Boost Multi Index,我有一个multi\u index\u容器,基本上看起来像这样: struct MyStruct { int a, b, c; }; struct Tag1; struct Tag2; typedef multi_index_container< MyStruct, indexed_by< hashed_non_unique< tag<Tag1>, composite_key< MyStruct,
multi\u index\u容器
,基本上看起来像这样:
struct MyStruct {
int a, b, c;
};
struct Tag1;
struct Tag2;
typedef multi_index_container<
MyStruct,
indexed_by<
hashed_non_unique<
tag<Tag1>,
composite_key<
MyStruct,
member<MyStruct, int, &MyStruct::a>,
member<MyStruct, int, &MyStruct::b>
>
>,
hashed_non_unique<
tag<Tag2>,
composite_key<
MyStruct,
member<MyStruct, int, &MyStruct::a>,
member<MyStruct, int, &MyStruct::b>,
member<MyStruct, int, &MyStruct::c>
>
>
>
> MyContainer;
// Search in container
SomeType range;
if (useIndex1)
range = index1.equal_range(...);
else
range = index2.equal_range(...);
// Loop through range
for (auto i = range.first; i != range.second; ++i)
...
我不知道怎么做。事实证明,
index1.equal_range
的返回类型是一对迭代器,与index2.equal_range
返回的迭代器不同。两者是否有共同的基类型?看看我的例子,SomeType
应该是什么样子?我不想在我的代码中为可能使用的每个索引重复for
循环。与其尝试使用范围进行类型擦除,不如将循环逻辑放入lambda中,并使用std::for_each:
#include <boost\multi_index_container.hpp>
#include <boost\multi_index\composite_key.hpp>
#include <boost\multi_index\member.hpp>
#include <boost\multi_index\hashed_index.hpp>
using namespace boost::multi_index;
struct MyStruct {
int a, b, c;
};
struct Tag1;
struct Tag2;
typedef multi_index_container
<
MyStruct
, indexed_by
<
hashed_non_unique
<
tag<Tag1>
, composite_key
<
MyStruct
, member<MyStruct, int, &MyStruct::a>
, member<MyStruct, int, &MyStruct::b>
>
>
, hashed_non_unique
<
tag<Tag2>
, composite_key
<
MyStruct
, member<MyStruct, int, &MyStruct::a>
, member<MyStruct, int, &MyStruct::b>
, member<MyStruct, int, &MyStruct::c>
>
>
>
> MyContainer;
int main()
{
MyContainer c;
MyContainer::index<Tag1>::type& index1 = c.get<Tag1>();
MyContainer::index<Tag2>::type& index2 = c.get<Tag2>();
//! Add some values
for (int i = 0; i < 10; ++i)
{
MyStruct s = { i, i * 2, i * 3 };
c.insert(s);
}
auto loop = [](const MyStruct& s){ std::cout << "{ " << s.a << ", " << s.b << ", " << s.c << " }" << std::endl; };
// Search in container
bool useIndex1 = true;
if (useIndex1)
{
auto range = std::make_pair(index1.begin(), index1.end());
std::for_each(range.first, range.second, loop);
}
else
{
auto range = std::make_pair(index1.begin(), index1.end());
std::for_each(range.first, range.second, loop);
}
// Loop through range
//for (auto i = range.first; i != range.second; ++i)
return 0;
}
#包括
#包括
#包括
#包括
使用名称空间boost::multi_索引;
结构MyStruct{
INTA、b、c;
};
结构Tag1;
结构标记2;
typedef多索引容器
<
我的结构
,由
<
散列非唯一
<
标签
,复合_键
<
我的结构
,委员
,委员
>
>
,散列非唯一
<
标签
,复合_键
<
我的结构
,委员
,委员
,委员
>
>
>
>霉素;
int main()
{
霉素c;
MyContainer::index::type&index1=c.get();
MyContainer::index::type&index2=c.get();
//!添加一些值
对于(int i=0;i<10;++i)
{
MyStruct s={i,i*2,i*3};
c、 插入(s);
}
自动循环=[](const MyStruct&s){std::cout谢谢,这确实有帮助!这会导致我遇到下一个问题,我可能不得不从循环中中断,因此for_each
并不完全是我需要的,但这是另一个故事……如果需要中断for_each循环,请尝试使用std::find_,如果条件为真的话(在lambda中返回true)否则返回false以继续循环。感谢您的添加,johnco3,find_if不在我的活动词汇表中,但我能够自己完成模板化循环lambda。
#include <boost\multi_index_container.hpp>
#include <boost\multi_index\composite_key.hpp>
#include <boost\multi_index\member.hpp>
#include <boost\multi_index\hashed_index.hpp>
using namespace boost::multi_index;
struct MyStruct {
int a, b, c;
};
struct Tag1;
struct Tag2;
typedef multi_index_container
<
MyStruct
, indexed_by
<
hashed_non_unique
<
tag<Tag1>
, composite_key
<
MyStruct
, member<MyStruct, int, &MyStruct::a>
, member<MyStruct, int, &MyStruct::b>
>
>
, hashed_non_unique
<
tag<Tag2>
, composite_key
<
MyStruct
, member<MyStruct, int, &MyStruct::a>
, member<MyStruct, int, &MyStruct::b>
, member<MyStruct, int, &MyStruct::c>
>
>
>
> MyContainer;
int main()
{
MyContainer c;
MyContainer::index<Tag1>::type& index1 = c.get<Tag1>();
MyContainer::index<Tag2>::type& index2 = c.get<Tag2>();
//! Add some values
for (int i = 0; i < 10; ++i)
{
MyStruct s = { i, i * 2, i * 3 };
c.insert(s);
}
auto loop = [](const MyStruct& s){ std::cout << "{ " << s.a << ", " << s.b << ", " << s.c << " }" << std::endl; };
// Search in container
bool useIndex1 = true;
if (useIndex1)
{
auto range = std::make_pair(index1.begin(), index1.end());
std::for_each(range.first, range.second, loop);
}
else
{
auto range = std::make_pair(index1.begin(), index1.end());
std::for_each(range.first, range.second, loop);
}
// Loop through range
//for (auto i = range.first; i != range.second; ++i)
return 0;
}