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

标签 c++ search boost multi-index

如何根据先前搜索的结果限制 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 高度的搜索? (然后找到结果中具有最高值的对象?)

最佳答案

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

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

关于c++ - 使用 Boost Multi-Index 搜索多个索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1818821/

相关文章:

C++顺序搜索找不到最后一个元素

javascript - 在对象的子数组中搜索 - JavaScript

c++ - 在唯一键上 boost unordered_multimap 循环

c++ - 在C++中连接两个字符

c++ - 如何在解决方案中构建多个项目之前执行流程

search - 查找大文本中重复次数最多的短语

c++ - Boost multi_index_container,通过标签获取索引导致编译错误

c++ - std::unordered_map<boost::any, boost::any> 抛出烦人的编译错误

c++ - 如何制作可滚动但不显示滚动条的单行 systabcontrol32

c++ - for循环中的条件不执行