c++如何对结构数组进行排序

标签 c++ arrays sorting struct

我有一个结构数组,如下所示。

struct valueCache
{
    bool valid;
    int startPc;
    int index;
    f32bit input[12];
    f32bit result[12];
};

static const int vcSize = 32;
static valueCache * vc = new valueCache[vcSize];

我想按索引降序对这个数组进行排序。所以我尝试了这个:

我的类.cpp

bool myClass::sortByIndex(const valueCache &lhs, const valueCache &rhs) { return lhs.index > rhs.index; }

bool myClass::sortArray(
{    
    std::sort(vc, vc + vcSize, sortByIndex);
}

我的类.h

class myClass
{
public:
    bool sortByIndex(const valueCache &lhs, const valueCache &rhs);
}

我得到了这个错误

error: argument of type 
‘bool (gpu3d::myClass::)(const gpu3d::valueCache&, const gpu3d::valueCache&)’ does not match 
‘bool (gpu3d::myClass::*)(const gpu3d::valueCache&, const gpu3d::valueCache&)’

有什么办法可以解决这个问题吗?

最佳答案

声明sortByIndex static,因为你不能将非静态成员函数传递给std::sort

关于c++如何对结构数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23363588/

相关文章:

c++ - 为什么 clang 会混淆 Boost 和 STL?

c++ - __vfptr 表在未删除的对象中损坏

c++ - C++ 中作为类成员的静态映射

java - 如何对对象的 ArrayList 进行排序?

C++ 字符串 (int) + 字符串 (int)

arrays - 将Struct数组元素作为参数传递给Go语言中的函数

c - 3x3 数组 = 10 个数字

c++ - 是否可以同时控制 [] operator 和 operator=?

python - 排序 XML 文件

c - 排序**矩阵结构