我正在尋找一個帶有組合鍵的增強ordered_non_unique
索引的自定義比較器。我不確定如何做到這一點。 Boost有composite_key_comparer
,但這對我不起作用,因爲密鑰成員之一的比較器取決於以前的成員。這是一個簡化的例子,但我希望third_
上的索引按third_
降序排列,當second_
爲'A'時,首先保留third_
的0值,並在所有其他情況下使用std :: less。希望這是有道理的。我想下面的代碼打印出:增強多索引自定義複合鍵比較器
3,BLAH,A,0
5,BLAH,A,11
2,BLAH,A,10
4,BLAH,A,9
1,BLAH,A,8
的代碼會去的地方善有善報HERE ???。謝謝你的幫助。
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/key_extractors.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/composite_key.hpp>
#include <iostream>
namespace bmi = boost::multi_index;
namespace bt = boost::tuples;
struct Widget
{
Widget (const std::string& id, const std::string& f, char s, unsigned int t)
: id_(id)
, first_(f)
, second_(s)
, third_(t)
{ }
~Widget() { }
std::string id_;
std::string first_;
char second_;
unsigned int third_;
};
std::ostream& operator<< (std::ostream& os, const Widget& w)
{
os << w.id_ << "," << w.first_ << "," << w.second_ << "," << w.third_;
return os;
}
struct id_index { };
struct other_index { };
typedef bmi::composite_key<
Widget*,
bmi::member<Widget, std::string, &Widget::first_>,
bmi::member<Widget, char, &Widget::second_>,
bmi::member<Widget, unsigned int, &Widget::third_>
> other_key;
typedef bmi::multi_index_container<
Widget*,
bmi::indexed_by<
bmi::ordered_unique<
bmi::tag<id_index>,
bmi::member<Widget, std::string, &Widget::id_>
>,
bmi::ordered_non_unique<
bmi::tag<other_index>,
other_key,
***************WHAT GOES HERE???***************
>
>
> widget_set;
typedef widget_set::index<other_index>::type widgets_by_other;
typedef widgets_by_other::iterator other_index_itr;
int main()
{
widget_set widgets;
widgets_by_other& wbo_index = widgets.get<other_index>();
Widget* w;
w = new Widget("1", "BLAH", 'A', 8);
widgets.insert(w);
w = new Widget("2", "BLAH", 'A', 10);
widgets.insert(w);
w = new Widget("3", "BLAH", 'A', 0);
widgets.insert(w);
w = new Widget("4", "BLAH", 'A', 9);
widgets.insert(w);
w = new Widget("5", "BLAH", 'A', 11);
widgets.insert(w);
std::pair<other_index_itr,other_index_itr> range =
wbo_index.equal_range(boost::make_tuple("BLAH", 'A'));
while (range.first != range.second)
{
std::cout << *(*range.first) << std::endl;
++range.first;
}
return 0;
}