3

我有一个multi_index_container看起来基本上是这样的:

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;

我实例化这样一个容器并像这样使用它的索引:

MyContainer c;

MyContainer::index<Tag1>::type& index1 = c.get<Tag1>;
MyContainer::index<Tag2>::type& index2 = c.get<Tag2>;

现在,在运行时,我想对equal_range两个索引之一进行操作。实际使用哪个索引,取决于当前配置。我想要完成的是这样的:

// 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在我的代码中为可能使用的每个索引重复循环。

4

1 回答 1

2

不要尝试使用范围进行类型擦除,而是将循环逻辑放入 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;
}
于 2014-08-05T14:10:31.277 回答