#include <iostream>
#include <array>
#include <algorithm>
using namespace std;
class Test
{
private:
int value;
public:
Test()
{
}
Test(int _value)
{
value = _value;
}
bool operator<(Test&);
};
bool Test::operator<(Test& rValue) {
return this->value < rValue.value;
}
int main()
{
Test* arr = new Test[950];
arr[0] = Test(5);
arr[1] = Test(10);
arr[2] = Test(7);
arr[3] = Test(3);
arr[4] = Test(10);
sort(arr, arr + 5, [](Test& a, Test& b) { return a < b ? false : true; });
}
直到出现相同等级值的对象,排序算法才能完美运行。
另外,我知道使用排序和反转的其他方法。
我正在使用 Visual Studio 2019。
错误: Debug Assertion Failed! Expression: invalid comparator
Test
对象将具有未初始化的value
字段。 - cigienoperator<
取反来进行排序,因为!(a < b)
等同于a >= b
。 - Slava