简体   繁体   English

获取boost :: multi_index容器的元素的等级

[英]get the rank of an element of a boost::multi_index container

The code below shows a multi_index container which is indexed by sequence and order. 下面的代码显示了一个multi_index容器,它按顺序和顺序索引。

In my use case elements will be mainly searched by index, and if existing, the next element (by order) is obtained. 在我的用例中,元素将主要通过索引进行搜索,如果存在,则获取下一个元素(按顺序)。

My question is, how to get the rank (by order) of obtained next element? 我的问题是,如何获得下一个元素的等级(按顺序)?

#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/ranked_index.hpp>
#include <boost/multi_index/identity.hpp>

using namespace boost::multi_index;

typedef multi_index_container <
    int
  , indexed_by<
        sequenced<>
      , ordered_non_unique<identity<int>>
      , ranked_non_unique<identity<int>>
    >
> Ints;

int main() {
    Ints ints;

    auto & sequence=ints.get<0>();
    auto & order=ints.get<1>();

    sequence.push_back(2);
    sequence.push_back(-1);
    sequence.push_back(5);
    sequence.push_back(6);

    auto it = order.find(2);
    if (it!=order.end()) {
        std::cout
            << "next to "
            << *it
            << " by sequence is "
            << *(++(ints.project<0>(it)))
            << std::endl
        ;
        std::cout
            << "next to "
            << *it
            << " by order is "
            << *(++(ints.project<1>(it))) //++it is good too
            << std::endl
        ;
        std::cout
            << "rank of next by sequence is "
            // << ??? ints.rank<???>(???)
            << std::endl
        ;
        std::cout
            << "rank of next by order is "
            // << ??? ints.rank<???>(???)
            << std::endl
        ;
    }
}

@sehe's answer is perfectly valid but runs in linear time. @ sehe的答案完全有效,但在线性时间内运行。 If you want better performance, consider defining your index #0 as random_access and #1 as ranked_non_unique (index #2 is redundant): 如果想要更好的性能,可以考虑将索引#0定义为random_access ,将#1定义为ranked_non_unique (索引#2是冗余的):

typedef multi_index_container <
    int
  , indexed_by<
        random_access<>
      , ranked_non_unique<identity<int>>
    >
> Ints;

so that you can write: 这样你就可以写:

std::cout
    << "rank of next by sequence is "
    << ints.project<0>(it)-sequence.begin()+1 // O(1)
    << std::endl
;
std::cout
    << "rank of next by order is "
    << order.rank(it)+1 // O(log n)
    << std::endl
;

Assuming you want some kind of "index into" or "offset from begin" in the sequenced index: 假设您想要在序列索引中使用某种“索引到”或“从开头偏移”:

if (it!=order.end()) {
    auto rank_of = [&](auto it) {
        return std::distance(sequence.begin(), ints.project<0>(it));
    };

    auto seq_next = std::next(seq_it);
    auto ord_next = std::next(it);

    if (seq_next!=sequence.end())
    {
        std::cout << "next to                     " << *it << " by sequence is " << *seq_next << std::endl;
        std::cout << "rank of next by sequence is " << rank_of(seq_next) << std::endl;
    }

    if (ord_next!=order.end())
    {
        std::cout << "next to                     " << *it << " by order is " << *ord_next << std::endl ;
        std::cout << "rank of next by order is    " << rank_of(ord_next) << std::endl;
    }
}

Without polymorphic lambdas you should write it out 没有多态lambda你应该写出来

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM