我似乎找不到一种有效的方法来迭代boost R-tree ( boost::geometry::index::rtree
)。到目前为止,我想出的唯一方法是使用非常大的边界框执行查询,以便在向量中返回所有元素的副本,但这显然既不节省空间也不节省时间。理想情况下,我只想使用 STL 样式的迭代器以通常的方式迭代树,但这似乎不可能?
问问题
2266 次
2 回答
9
自 1.59.0 起
begin()
和end()
成员函数在 中定义bgi::rtree
,返回一个const_iterator
. 因此,可以在没有下面描述的技术的情况下迭代所有元素。在 C++11 中:
for(auto const& v: rtree)
/* do something with v */
1.59.0 之前
正如其他人所说,要遍历存储在 rtree 中的所有元素,您可以使用查询迭代器。但是,不需要执行实际的空间查询(通过边界等)。你可以传递一个虚拟的 UnaryPredicate 总是true
用bgi::satisfies()
. 在 C++11 中:
std::for_each(rtree.qbegin(bgi::satisfies([](Value const&){ return true; })),
rtree.qend(),
[](Value const& v){
/* do something with v */
});
非迭代查询也可用于此目的,但它需要一个特殊的输出迭代器,例如boost::function_output_iterator
在Boost.Iterator库中实现(参见http://www.boost.org/doc/libs/1_57_0/libs/iterator/文档/function_output_iterator.html)。在 C++11 中:
rtree.query(bgi::satisfies([](Value const&){ return true; }),
boost::make_function_output_iterator([](Value const& v){
/* do something with v */
}));
旁注:
- 上面的代码需要文档中提到的Boost.Geometry库头文件
namespace bgi = boost::geometry::index
Value
是存储在bgi::rtree
boost::function_output_iterator
需要#include <boost/function_output_iterator.hpp>
- 在 C++14 中可以使用通用 lambda,那么上面的代码将与值类型无关
于 2015-01-06T13:08:57.130 回答