C++ 如何使用该类中的成员函数调用stl::nth_元素?
我想将函数C++ 如何使用该类中的成员函数调用stl::nth_元素?,c++,stl,member-function-pointers,nth-element,C++,Stl,Member Function Pointers,Nth Element,我想将函数nth\u element与我自己的排序函数(应该可以访问对象的数据)一起在一个类中使用。目前,我正在做以下工作: class Foo { public: glm::vec3 *points; int nmbPoints; bool idxPointCompareX(int a, int b); void bar(); } bool Foo::idxPointCompareX(int a, int b) {return points[a].x < p
nth\u element
与我自己的排序函数(应该可以访问对象的数据)一起在一个类中使用。目前,我正在做以下工作:
class Foo
{
public:
glm::vec3 *points;
int nmbPoints;
bool idxPointCompareX(int a, int b);
void bar();
}
bool Foo::idxPointCompareX(int a, int b)
{return points[a].x < points[b].x;)
void Foo::bar()
{
stl::vector<int> idxPointList;
for(int i = 0; i < nmbPoints; i++) idxPointList.push_back(i);
stl::nth_element(idxPointList.first(),idxPointList.first()+nmbPoints/2,idxPointList.end(), idxPointCompareX);
}
class-Foo
{
公众:
glm::vec3*点;
int NMB点数;
bool-idxPointCompareX(inta,intb);
空心钢筋();
}
bool Foo::idxPointCompareX(inta,intb)
{返回点[a].x<点[b].x;)
void Foo::bar()
{
向量idxPointList;
对于(int i=0;i
当然,这不起作用,我得到了一个错误:“必须调用对非静态成员函数的引用”。之后,我看了一下,还有其他一些问题。我理解为什么这不起作用,但我不确定如何解决这个问题
有人能帮助我并告诉我如何解决这个问题吗?
idxPointCompareX
是一个成员函数,也就是说,如果没有对Foo
对象的引用,就不能调用它。但从它的定义来看,它似乎不需要是成员,因为它完全是根据参数定义的
您可以将其设置为
静态函数(即“类函数”)或者是一个自由函数,然后将其传递给std::nth_元素
idxPointCompareX
是一个成员函数,也就是说,如果没有对Foo
对象的引用,就不能调用它。不过,看看它的定义,它似乎不需要是一个成员,因为它纯粹是根据参数定义的
您可以将其设置为静态
函数(即“类函数”)或自由函数,然后将其传递给std::nth_元素
,以获取成员函数的地址。您需要使用正确的语法,即&Foo::idxPointCompareX
而不仅仅是idxPointCompareX
但是您还需要一个Foo
对象来调用函数,因此您需要将一个对象绑定到它。您可能想在this
上调用它,以便使用std::bind
:
using namespace std::placeholders;
stl::nth_element(begin, begin+n, end,
std::bind(&Foo::idxPointCompareX, this, _1, _2));
或者更简单,使用lambda函数:
stl::nth_element(begin, begin+n, end,
[this](int a, int b) { return idxPointCompareX(a, b);}
);
这将创建一个lambda函数,用于捕获This
,并将其参数传递给捕获的This
指针上的idxPointCompareX函数。要获取成员函数的地址,需要使用正确的语法,即&Foo::idxPointCompareX
而不仅仅是idxPointCompareX
但是您还需要一个Foo
对象来调用函数,因此您需要将一个对象绑定到它。您可能想在this
上调用它,以便使用std::bind
:
using namespace std::placeholders;
stl::nth_element(begin, begin+n, end,
std::bind(&Foo::idxPointCompareX, this, _1, _2));
或者更简单,使用lambda函数:
stl::nth_element(begin, begin+n, end,
[this](int a, int b) { return idxPointCompareX(a, b);}
);
这将创建一个lambda函数,该函数捕获This
,并将其参数传递给捕获的This
指针上的idxPointCompareX函数。在创建对象方法之前,不能调用该对象方法,因此这里有一些选项:
使方法保持静态
将构造函数保留为空,并将所有内容移动到调用比较部分的init
方法中
使用lambda
示例:
静态方法:
static bool idxPointCompareX(glm::vec3 a, glm::vec3 b)
{return a.x < b.x;)
static bool idxPointCompareX(glm::vec3 a,glm::vec3 b)
{返回a.x
初始方法:
Foo::bar()
{
stl::vector<int> idxPointList;
for (int i = 0; i < nmbPoints; i++)
idxPointList.push_back(i);
}
Foo::init()
{
stl::nth_element(idxPointList.first(),
idxPointList.first()+nmbPoints/2,idxPointList.end(),
idxPointCompareX);
}
Foo::bar()
{
向量idxPointList;
对于(int i=0;i
拉姆达:
Foo::bar()
{
stl::vector<int> idxPointList;
for (int i = 0; i < nmbPoints; i++)
idxPointList.push_back(i);
stl::nth_element(idxPointList.first(),
idxPointList.first()+nmbPoints/2,idxPointList.end(),
[](int a, int b){return points[a].x < points[b].x;));
}
Foo::bar()
{
向量idxPointList;
对于(int i=0;i
我自己也会选择lambda版本。在对象方法创建之前,您不能调用它,因此您在这里有一些选项:
使方法保持静态
将构造函数保留为空,并将所有内容移动到调用比较部分的init
方法中
使用lambda
示例:
静态方法:
static bool idxPointCompareX(glm::vec3 a, glm::vec3 b)
{return a.x < b.x;)
static bool idxPointCompareX(glm::vec3 a,glm::vec3 b)
{返回a.x
初始方法:
Foo::bar()
{
stl::vector<int> idxPointList;
for (int i = 0; i < nmbPoints; i++)
idxPointList.push_back(i);
}
Foo::init()
{
stl::nth_element(idxPointList.first(),
idxPointList.first()+nmbPoints/2,idxPointList.end(),
idxPointCompareX);
}
Foo::bar()
{
向量idxPointList;
对于(int i=0;i
拉姆达:
Foo::bar()
{
stl::vector<int> idxPointList;
for (int i = 0; i < nmbPoints; i++)
idxPointList.push_back(i);
stl::nth_element(idxPointList.first(),
idxPointList.first()+nmbPoints/2,idxPointList.end(),
[](int a, int b){return points[a].x < points[b].x;));
}
Foo::bar()
{
向量idxPointList;
对于(int i=0;i
我自己会选择lambda版本。很抱歉我在问题中犯了错误。我会在发布此问题后直接编辑它。很抱歉我在问题中犯了错误。我会在发布此问题后直接编辑它。正如我在他的回答中已经提到的,我在编写问题时犯了错误。idxPointCompareX使用索引获取访问“点”的点数据。此处缺少第n个元素。stl::n_元素(begin,begin+smth,end,[this](inta,intb){返回idxPointCompareX(a,b);}
正如我在他的回答中提到的,我在写问题时犯了一个错误。idxPointCompareX使用索引来访问“points”的点数据。这里缺少第n个元素。stl::n_元素(begin,begin+