Boost.MultiIndex 空间操作

发布于 2024-12-03 01:13:59 字数 574 浏览 5 评论 0原文

我需要一个应用程序的空间地图。我找到了 Boost.MultiIndex。
我遵循了它的教程并了解了如何创建类型:

typedef boost::multi_index_container<MapNode,
    indexed_by<
        ordered_non_unique<member<MapNode, int, &MapNode::X>>,
        ordered_non_unique<member<MapNode, int, &MapNode::Y>>
    >
> Map_T;

以及如何插入它:

Map.insert(Node);

如何根据其 xy 坐标检索值?我如何检查那里是否有值?

I needed a spatial map for an application. I found Boost.MultiIndex.
I followed its tutorial and understood how to create a type:

typedef boost::multi_index_container<MapNode,
    indexed_by<
        ordered_non_unique<member<MapNode, int, &MapNode::X>>,
        ordered_non_unique<member<MapNode, int, &MapNode::Y>>
    >
> Map_T;

And how to insert to it:

Map.insert(Node);

How do I retrieve a value based on its x and y coordinates? and how do I check if there is a value there?

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

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

发布评论

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

评论(1

你的他你的她 2024-12-10 01:13:59

首先,你不需要 boost::multi_index 来解决这个问题。简单地重载运算符<对于您的类型 MapNode 并使用 std::set (如果可能出现具有相同坐标的多个节点,则分别使用 std::map )。 Therby,操作员<首先比较(例如)x 值。如果它们相等,则继续比较 y 坐标。

使用 boost::multi_index 只有一个原因:如果需要不同的访问方法。例如,如果您想要一个额外的“视图”,其中节点首先按 y 排序,然后再按 x 排序。
但是,multi_index 的成员方法(如上面的代码所示)并不是一个好主意。使用identity两次,但提供两个不同的比较函数。详细信息可以在 boost 文档中找到。

最后,所有这些方法可能不是最好的方法 - 取决于您的应用程序。例如,Berg 等人的《计算几何》一书中描述了专门的数据结构。等人。
不幸的是,我不知道这些算法的任何免费实现......

First of all, you do not need boost::multi_index to solve this problem. Simply overload operator< for your type MapNode and use std::set (resp. std::map if multiple nodes with identical coordinates can occur). Therby, operater< compares (e.g.) for x-values first. Iff they are equal, proceed comparing the y coordinate.

There is only one reason for using boost::multi_index: If different access methodes are required. For instance, if you want an additional "view" where the nodes are first sorted by y and than by x too.
However, the member approach of multi_index (as in your code above) is not a good idea. Use identity twice, but provide two different comparison functions. Details can be found in the boost docu.

Finally, all these approches may not be the best possible ones - depending on your application. Specialized data structures are e.g. described in the book "Computational geometry" by Berg et. al.
Unfortunaetlly, I do not know any free implementation of these algorithms...

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