gpt4 book ai didi

c++ - Boost Multi-Index 自定义复合键比较器

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:58:29 24 4
gpt4 key购买 nike

我正在寻找为带有复合键的 boost ordered_non_unique 索引编写自定义比较器。我不确定该怎么做。 Boost 有一个 composite_key_comparer,但这对我不起作用,因为键成员的比较器之一取决于前一个成员。这是一个简化的示例,但我希望当 second_ 为“A”时,索引按 third_ 降序排序,首先为 third_ 保留 0 值,然后在所有其他情况下使用 std::less 。希望这是有道理的。我想打印下面的代码:

3,BLAH,A,0
5,BLAH,A,11
2,BLAH,A,10
4,BLAH,A,9
1,BLAH,A,8

代码将代替这里有什么???。感谢您的帮助。

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

最佳答案

我认为你碰壁了。

您可能想引用这里:Ordered Indices

与 STL 一样,您实际上必须自己提供比较标准,因此您可以根据自己的需要对其进行定制。

正如我链接的页面上所解释的(在“比较谓词”部分):

The last part of the specification of an ordered index is the associated comparison predicate, which must order the keys in a less-than fashion.

因此,您的工作有两个方面:

  1. 您需要定义一个适用于您的类型的比较谓词
  2. 您需要向 Boost.MultiIndex 表明您希望使用此谓词进行键的实际比较

这是一个示例,但我不确定我是否完全理解您的要求,因此您可能需要检查它是否确实按照您的意愿排序。

struct WidgetComparer
{
bool operator()(const Widget& lhs, const Widget& rhs) const
{
if (lhs._second == 'A' && rhs._second == 'A')
{
return lhs._third == 0 || rhs._third < lhs._third;
}
else
{
return lhs._third < rhs._third;
}
} // operator()
};

然后,您只需完成索引即可。因此,将“其他键”替换为 identity < Widget >,将“WHAT GOES HERE”替换为 WidgetComparer

给你!

重要的一点是你不应该关注容器的“关键”部分。键本身什么都不是,它是进行实际排序的一对(键,比较谓词)。重点是文档中的键以 boost 代码重用(特别是受益于已经实现的比较谓词,如 std::less)。

作为替代方案,您可能已经决定为您的 Widget 类编写一个“operator<”代码或专门化 std::less 算法。如果您打算多次使用这种排序方式,您应该更喜欢这种解决方案。但是,如果您的容器是唯一会使用它的容器,那么自定义谓词会更好。

希望对您有所帮助。

关于c++ - Boost Multi-Index 自定义复合键比较器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1324800/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com