【问题标题】:How to find all matches boost::multi_index with a key?如何使用键查找所有匹配项 boost::multi_index?
【发布时间】:2016-05-06 15:44:37
【问题描述】:

我有一个 boost::multi_index 容器。谁能告诉我如何根据某个键检索一系列迭代器?经过数小时的搜索,我得到了 lower_bound 或 upper_bound 应该可以解决问题的想法,但我仍然没有得到示例。在以下示例中,我想获取价格在 22 到 24 之间的所有迭代器。非常感谢。

 struct order                                                      
 {                                                                 
     unsigned int    id;                                           
     unsigned int    quantity;                                     
     double          price;                                        

     order(unsigned int id_, unsigned int quantity_, double price_)
         :id(id_), quantity(quantity_), price(price_){}
}
typedef multi_index_container<                                    
  order,                                                          
  indexed_by<                                                     
    ordered_unique<                                               
      tag<id>,  BOOST_MULTI_INDEX_MEMBER(order, unsigned int, id),
      std::greater<unsigned int>>,                                
    ordered_non_unique<                                           
      tag<price>,BOOST_MULTI_INDEX_MEMBER(order ,double, price)>  
  >                                                               
> order_multi;                                                              
 int main()                                                            
 {                                                                     
     order_multi order_book;                                           

     order_book.insert(order(/*id=*/0, /*quantity=*/10, /*price=*/20));
     order_book.insert(order(/*id=*/1, /*quantity=*/11, /*price=*/21));
     order_book.insert(order(/*id=*/3, /*quantity=*/12, /*price=*/22));
     order_book.insert(order(/*id=*/2, /*quantity=*/1,  /*price=*/22));
    order_book.insert(order(/*id=*/4, /*quantity=*/1,  /*price=*/23));
    order_book.insert(order(/*id=*/5, /*quantity=*/1,  /*price=*/24));
    order_book.insert(order(/*id=*/6, /*quantity=*/1,  /*price=*/24));
    order_book.insert(order(/*id=*/7, /*quantity=*/1,  /*price=*/26));   

  }                                                                                                                                 

【问题讨论】:

    标签: c++ boost boost-multi-index


    【解决方案1】:

    你所追求的范围是[lower_bound(22),upper_bound(24)),即:

    auto first=order_book.get<price>().lower_bound(22);
    auto last=order_book.get<price>().upper_bound(24);
    for(;first!=last;++first)std::cout<<first->id<<" "; // prints 3 2 4 5 6
    

    如果您难以确定何时需要使用 lower_upper_bound,则有一个 range member function 可能更容易正确(并且速度略快):

    auto range=order_book.get<price>().range(
      [](double p){return p>=22;},  // "left" condition
      [](double p){return p<=24;}); // "right" condition
    for(;range.first!=range.second;++range.first)std::cout<<range.first->id<<" ";
    

    前提是您使用 C++11 并具有 lambda 函数。你也可以在没有原生 lambda 函数的 C++03 中使用range,但是你必须求助于Boost.Lambda 或者手动编写range 接受的函子,这两个选项可能都太麻烦了。

    【讨论】:

    • 酷。关于range() 成员函数的TIL。我怎么会错过这么久。顺便说一句,我会使用for (auto&amp; el : boost::make_iterator_range(order_book.get&lt;price&gt;().range(...))) {...}
    • 这个范围函数可以和复合键一起使用吗?有没有使用复合键的例子..??它可以用来查找具有空解集的范围吗??
    • 是的,可以。适合与复合键一起使用的下界(“左”条件)看起来像 [](const auto&amp; k){return k&gt;=std::tuple(...);}(并且对称地用于上界,即“右”条件)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-23
    • 2021-10-17
    相关资源
    最近更新 更多