排序算法c++

Sort algorithm c++

本文关键字:c++ 算法 排序      更新时间:2023-10-16
#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; });
}

排序算法可以完美地工作,直到有具有相同等级值的对象为止。

p.S我知道使用排序和反转的其他方法。

我正在使用visual studio 2019

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

对于相等的项,排序比较器必须返回false,而您的则返回true

试试这个。

sort(arr, arr + 5, [](Test& a, Test& b) { return b < a; });