首页 > 解决方案 > 如何在没有 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;
}

标签: c++boostlambdaparameter-passingc++03

解决方案


对于任何感兴趣的人,这是我的解决方案

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;
}

推荐阅读