使用 Boost Multi-Index 搜索多个索引

发布于 2024-08-13 03:28:34 字数 1448 浏览 4 评论 0原文

如何根据先前搜索的结果限制 boost::multi_index 中的搜索? 举个例子:假设我有一个具有如下内部值的矩形类:

    class MyRect
    {
    public:
        int    width;  
        int    height; 

        double value;
    }

并且我需要此类对象的数据结构来回答诸如“给定一个 input_rectangle - 哪个对象 MyRect 包含在该矩形中并且具有最高值?”

我可以使用这样的“multi_index”:

    struct given_value{};
    struct given_width{};
    struct given_height{};

    typedef multi_index_container<MyRect,
        indexed_by<
            ordered_non_unique< tag<given_value>, 
                member<MyRect, double, &MyRect::value>,
            ordered_non_unique< tag<given_width>, 
                member<MyRect, int, &MyRect::width>,
            ordered_non_unique< tag<given_height>, 
                member<MyRect, int, &MyRect::height>, >
        >
    > MyDataStructure;

    typedef MyDataStructure::index<given_width>::type MyDataStructureGivenWidth;
    typedef MyDataStructureGivenWidth::iterator WidthIterator;

如果我的 input_rectangle 有宽度 input_width 我可以使用这样的东西:

WidthIterator start_iter = data_object.get<given_width>().begin();
WidthIterator end_iter   = data_object.get<given_width>().upper_bound(input_width);

但是我如何限制对 coresp 高度的搜索两个给定的迭代器? (然后找到该结果中具有最高值的对象?)

how do I limit the search in a boost::multi_index by the result of a previous search?
As an example: suppose I have a rectangle class with an internal value like this:

    class MyRect
    {
    public:
        int    width;  
        int    height; 

        double value;
    }

and I need a data structure of such object to answer queries like "given an input_rectangle - which object MyRect is contained in that rectangle and has the highest value?"

I could use a 'multi_index' like this:

    struct given_value{};
    struct given_width{};
    struct given_height{};

    typedef multi_index_container<MyRect,
        indexed_by<
            ordered_non_unique< tag<given_value>, 
                member<MyRect, double, &MyRect::value>,
            ordered_non_unique< tag<given_width>, 
                member<MyRect, int, &MyRect::width>,
            ordered_non_unique< tag<given_height>, 
                member<MyRect, int, &MyRect::height>, >
        >
    > MyDataStructure;

    typedef MyDataStructure::index<given_width>::type MyDataStructureGivenWidth;
    typedef MyDataStructureGivenWidth::iterator WidthIterator;

If my input_rectangle has width input_width I could use something like this:

WidthIterator start_iter = data_object.get<given_width>().begin();
WidthIterator end_iter   = data_object.get<given_width>().upper_bound(input_width);

But how do I limit the search for the coresp height by the two given iterators?
(And after that find the object with the highest value in that result?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帝王念 2024-08-20 03:28:34

我认为您无法进行就地限制。
将匹配宽度查询的结果迭代器存储在另一个容器中,并使用该容器通过remove_if 查找匹配高度。然后使用 max_element 找到最大的。

如果将元素存储为指针,则可以使用相同的 MIC 来存储结果。

I don't think you can do an inplace limitation.
Store the resulting iterators of the matching widths query in another container and use that container to find the matching heights with remove_if. Then use max_element to find the largest.

If you store the elements as pointers, you could use the same MIC to store the results.

我不在是我 2024-08-20 03:28:34

如果我正确理解你的问题,可能会有一个更简单的解决方案。只需将您的 MyRect 放入按值排序的 STL-Set 中(需要定义比较运算符或自定义比较函数)。您可以创建一个自定义谓词并使用它来检查给定的 MyRect 是否在特定范围内。然后使用 STL 算法 find_if,并将自定义谓词交给它。如果您确保它以降序遍历序列(例如通过使用reverse_iterator),它应该返回您正在寻找的MyRect。

希望这是可以理解的并且适用于您的问题。

If I understand your problem correctly, there may be a simpler solution. Just put your MyRects into an STL-Set ordered by value (need to define comparison operator or custom comparison function). The you can create a custom predicate that and use that checks if a given MyRect is within a certain range. Then you use the STL-Algorithm find_if, and hand it the custom predicate. If you make sure that it traverses the sequence in decreasing order (e.g. by using reverse_iterator), it should return the MyRect you are looking for.

Hope that is understandable and applies to your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文