gpt4 book ai didi

c++ - 如何在类中使用 "void sort(_RAIter, _RAIter, _Compare)"头文件的 "algorithm"

转载 作者:行者123 更新时间:2023-11-30 01:15:58 26 4
gpt4 key购买 nike

我正在编写代码,但无法在类中使用 sort() 方法。

class MyClass {
struct MyStruct {
...
} MyStructArr[10];

void fun() {
... // setting values of MyStructArr
sort(MyStructArr, MyStructArr + 10, cmp); // !ERROR!
}

int cmp(struct MyStruct a, struct MyStruct b) {
...
}
};

但是相同的代码在没有类的情况下也能工作

struct MyStruct {
...
} MyStructArr[10];

int main() {
... // setting values of MyStructArr
sort(MyStructArr, MyStructArr + 10, cmp);
return 0;
}

int cmp(struct MyStruct a, struct MyStruct b) {
...
}

我无法理解为什么带有比较功能的 sort(...) 在内部不起作用类。
这是错误:

SuffixArray.cpp: In member function 'void SuffixArray_Entry::suffixArray()':
SuffixArray.cpp:31:43: error: no matching function for call to 'sort(SuffixArray_Entry::SuffixArray_entry [65536], SuffixArray_Entry::SuffixArray_entry*, <unresolved overloaded function type>)'
sort(L, L + N, SuffixArray_cmp);
^
SuffixArray.cpp:31:43: note: candidates are:
In file included from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\algorithm:62:0,
from SuffixArray.cpp:3:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_algo.h:5461:5: note: template<class _RAIter> void std::sort(_RAIter, _RAIter)
sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_algo.h:5461:5: note: template argument deduction/substitution failed:
SuffixArray.cpp:31:43: note: candidate expects 2 arguments, 3 provided
sort(L, L + N, SuffixArray_cmp);
^
In file included from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\algorithm:62:0,
from SuffixArray.cpp:3:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_algo.h:5497:5: note: void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = SuffixArray_Entry::SuffixArray_entry*; _Compare = int (SuffixArray_Entry::*)(SuffixArray_Entry::SuffixArray_entry, SuffixArray_Entry::SuffixArray_entry)]
sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_algo.h:5497:5: note: no known conversion for argument 3 from '<unresolved overloaded function type>' to 'int (SuffixArray_Entry::*)(SuffixArray_Entry::SuffixArray_entry, SuffixArray_Entry::SuffixArray_entry)'
make.exe[2]: *** [build/Debug/MinGW-Windows/SuffixArray.o] Error 1
make.exe[2]: Leaving directory `/c/Users/Afzalex/Documents/NetBeansProjects/TestingStore/CPPStoreRoom'
make.exe[1]: *** [.build-conf] Error 2
make.exe[1]: Leaving directory `/c/Users/Afzalex/Documents/NetBeansProjects/TestingStore/CPPStoreRoom'
make.exe": *** [.build-impl] Error 2

这是我原始程序的错误堆栈跟踪。我将 Netbeans 与 MinGW 结合使用

最佳答案

声明成员函数cmp作为静态成员函数。考虑到如果函数的参数是引用会好得多。

另一种方法是在类内部定义一个功能对象。例如

class MyClass {
struct MyStruct {
...
} MyStructArr[10];

struct cmp
{
bool operator ()( const struct MyStruct &a, const struct MyStruct &b ) const
{
//....
}
};

void fun() {
... // setting values of MyStructArr
sort(MyStructArr, MyStructArr + 10, cmp());
}

...

你也可以定义operator <用于结构 MyStruct .在这种情况下,您可以调用 std::sort没有谓词。

关于c++ - 如何在类中使用 "void sort(_RAIter, _RAIter, _Compare)"头文件的 "algorithm",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27573052/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com