c++ - 如何在没有 lambda 表达式的情况下将函数传递给 boost::geometry::index::satisfies()
问题描述
所以我使用 Boost 的 R-tree 来优化空间搜索。查询的参数是搜索中心和半径,所以我将使用我自己的函数来查看 rtree 中给定点与给定搜索中心之间的距离是否小于给定距离。这是 lambda 表达式的完美之处,因为我看到的 satisfies() 函数的大多数示例都使用 lambda 表达式。不幸的是,我们仍在使用 C++03,所以 lambda 是不可能的。
我能够传递我自己的函数,但我不知道如何传递任何参数,只是它正在迭代的 R-tree 中的值。我最终对静态成员变量使用了一种奇怪的解决方法,这感觉像是一种非常错误的方法。
bool inRange(Location const& loc)
{
return boost::geometry::distance(loc, RTree::searchCenter) < RTree::searchRadius;
}
vector<Location> RTree::query(Location const& loc, double const& sr)
{
searchCenter = loc; // static member variable
searchRadius = sr; // static member variable
vector<Location> ret;
rtree.query(boost::geometry::index::satisfies(inRange), back_inserter(ret));
return ret;
}
这对我来说看起来和感觉都令人难以置信。我怎样才能让它看起来像这样?
bool inRange(Location const& loc1, Location const& loc2, double const& sr)
{
return boost::geometry::distance(loc1, loc2) < sr;
}
vector<Location> RTree::query(Location const& loc, double const& sr)
{
vector<Location> ret;
rtree.query(boost::geometry::index::satisfies(inRange(loc,sr)), back_inserter(ret));
return ret;
}
解决方案
对于任何感兴趣的人,这是我的解决方案
bool RTree::inRange(Location const& loc1, Location const& loc2, double const& sr)
{
return boost::geometry::distance(loc1, loc2) < sr;
}
vector<Location> RTree::query(Location const& loc, double const& sr)
{
vector<Location> ret;
rtree.query(boost::geometry::index::satisfies(boost::bind(&RTree::inRange, this, _1, loc, sr), back_inserter(ret));
return ret;
}
推荐阅读
- html - 如何使用 CSS flexbox 创建此布局?
- javascript - 开玩笑:尝试导入本机模块时出错;无法用 Mock 预防
- sas - SAS:如何重复计算和更改值?
- javascript - 在函数内部使用“this”从视图模型列表中获取视图模型属性
- c - 寻找元素平均值的问题
- c# - WebApi iActionFilter 实现。如何读取自定义控制器属性?
- reactjs - REACT.js 中的多个独特的下拉安全问题
- c - C 释放堆栈上的内存
- c# - C# FolderBrowserDialog - 如何包含共享文件夹
- ruby-on-rails-3 - 设计 gem - 密码中不允许有特殊字符