c++ - boost::multi_index_container:从任意索引取equal_range,只执行一次循环

标签 c++ boost boost-multi-index

我有一个 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 循环。

最佳答案

与其尝试使用范围进行类型删除,不如将循环逻辑放入 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;
}

关于c++ - boost::multi_index_container:从任意索引取equal_range,只执行一次循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25133468/

相关文章:

c++ - 当我使用remove()时权限被拒绝

c++ - 计算字母时出现奇怪的编译器错误

c++ - 使用带有 MFC CString 的 boost 字符串算法来检查字符串的结尾

c++ - 使用具有不同编译器版本的 boost 库

c++ - 进程崩溃时如何释放boost::interprocess::named_mutex

c++ - 使用内部迭代器从 boost 多索引中删除项目时的一致性

c++ - 重载赋值运算符以返回 std::vector

c++ - shellsort 算法不起作用

c++ - 基于 boost::asio 的慢速 http 客户端 - (分块传输)

c++ - boost.multiindex 和值的地址作为键