排序算法 C++

use*_*778 3 c++ sorting

#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; });
}
Run Code Online (Sandbox Code Playgroud)

排序算法完美运行,直到存在具有相同等级值的对象。

PS我知道使用排序和反向的其他方法。

我正在使用 Visual Studio 2019

错误:调试断言失败!表达式:无效的比较器

joh*_*ohn 6

排序比较器必须false为相等的项目返回,您的将返回true。

试试这个。

sort(arr, arr + 5, [](Test& a, Test& b) { return b < a; });
Run Code Online (Sandbox Code Playgroud)