搜索boost::根据序列获取multi_index并按顺序获取下一个元素

时间:2022-08-23 22:39:54

I want to search a boost::multi_index container by sequence and obtain the next element by order.

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

The code below stores four floatswith different indexes (sequence and ordered).

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

The last if statement is the problem. I don't know how to edit to get the next element by order.

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

Here's some code:

这里有一些代码:

#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
    }
}

2 个解决方案

#1


3  

auto j=++(floats.project<1>(i));

#2


2  

have a look at the project<N> function for multi_index iterators. Documentation is here:

查看多_index迭代器的项目 函数。文档:

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

http://www.boost.org/doc/libs/1_59_0/libs/multi_index/doc/reference/multi_index_container.html投影

so you would write something like:

你可以这样写:

auto iorder = project<0>(i);

#1


3  

auto j=++(floats.project<1>(i));

#2


2  

have a look at the project<N> function for multi_index iterators. Documentation is here:

查看多_index迭代器的项目 函数。文档:

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

http://www.boost.org/doc/libs/1_59_0/libs/multi_index/doc/reference/multi_index_container.html投影

so you would write something like:

你可以这样写:

auto iorder = project<0>(i);