按顺序搜索boost::multi_index,按顺序获取下一个元素

searching boost::multi_index by sequence and obtaining the next element by order

本文关键字:顺序 元素 获取 下一个 顺序搜索 boost multi index      更新时间:2023-10-16

我想按顺序搜索boost::multi_index容器,并按顺序获得下一个元素。

下面的代码存储了四个具有不同索引(顺序和有序)的float。

最后一个if语句就是问题所在。我不知道如何编辑以按顺序获得下一个元素。

这里有一些代码:

#include <iostream>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/identity.hpp>
using namespace boost::multi_index;
typedef multi_index_container <
    float
  , indexed_by<
        sequenced<>
      , ordered_non_unique<identity<float>>
    >
> Floats;
int main() {
    Floats floats;
    auto & sequence=floats.get<0>();
    auto & order=floats.get<1>();
    order.insert(0.3);
    sequence.push_back(0.1);
    sequence.push_back(0.9);
    order.insert(0.6);
    // 0.1 0.3 0.6 0.9
    for (auto i=order.begin(),j=order.end(); i!=j; ++i) {
        std::cout << *i << std::endl;
    }
    // 0.3 0.1 0.9 0.6
    for (auto i=sequence.begin(),j=sequenceend(); i!=j; ++i) {
        std::cout << *i << std::endl;
    }
    auto i = order.find(0.3);
    if (i!=order.end()) {
        // get the next element by order, 0.6 in this case
    }
}
auto j=++(floats.project<1>(i));

了解用于multi_index迭代器的project<N>函数。文档在这里:

http://www.boost.org/doc/libs/1_59_0/libs/multi_index/doc/reference/multi_index_container.html#projection

所以你会写这样的东西:

auto iorder = project<0>(i);
相关文章: