Boost.MultiIndex 空间操作
我需要一个应用程序的空间地图。我找到了 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);
如何根据其 x
和 y
坐标检索值?我如何检查那里是否有值?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,你不需要 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...