c++ - 按顺序查找boost::multi_index,按顺序获取下一个元素

标签 c++ boost boost-multi-index

我想按顺序搜索 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));

关于c++ - 按顺序查找boost::multi_index,按顺序获取下一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33857751/

相关文章:

c++ - 标准库中 boost::make_transform_iterator 的等价物是什么?

C++:使用Boost MultiIndex确定指针类型的类

c++ - std::unordered_map - 如何随时获取 "track"最大/最小键

c++ - 在C中的一堆数组中找到最大值

c++ - 互斥作为类成员

c++ - 特殊值不能用作 unordered_map 中的键

c++ - 如何在 multi_index_container 中添加指定的有序唯一索引

c++ - 无法在另一个内部创建 Qt 对话框

c++ - 使用 Win32 API 获取 Windows 屏幕保护程序超时

java - Pydev 中的自动补全 - Eclipse for wxpython