如何按降序排序UDT向量?


#include <bits/stdc++.h>
using namespace std;
class Point
{
public:
int x;
int y;
Point(int x = 0, int y = 0)
{
this->x = x;
this->y = y;
}
bool operator>(const Point &p1)
{
return (x + y) > (p1.x + p1.y);
}
};
int main()
{
vector<Point> v = {{1, 2}, {3, 1}, {0, 1}};
sort(v.begin(), v.end(), greater<Point>());
for (auto i : v)
cout << i.x << " " << i.y << endl;
return 0;
}

我想对一个UDT向量按降序排序。所以我尝试重载操作符>就像课堂上写的那样。但它会给我误差。我该如何对UDT向量按降序排序呢?

错误:

In file included from /opt/compiler-explorer/gcc-trunk-20221128/include/c++/13.0.0/string:49,
from /opt/compiler-explorer/gcc-trunk-20221128/include/c++/13.0.0/bitset:52,
from /opt/compiler-explorer/gcc-trunk-20221128/include/c++/13.0.0/x86_64-linux-gnu/bits/stdc++.h:52,
from <source>:1:
/opt/compiler-explorer/gcc-trunk-20221128/include/c++/13.0.0/bits/stl_function.h: In instantiation of 'constexpr bool std::greater<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = Point]':
/opt/compiler-explorer/gcc-trunk-20221128/include/c++/13.0.0/bits/predefined_ops.h:158:30:   required from 'constexpr bool __gnu_cxx::__ops::_Iter_comp_iter<_Compare>::operator()(_Iterator1, _Iterator2) [with _Iterator1 = __gnu_cxx::__normal_iterator<Point*, std::vector<Point> >; _Iterator2 = __gnu_cxx::__normal_iterator<Point*, std::vector<Point> >; _Compare = std::greater<Point>]'
/opt/compiler-explorer/gcc-trunk-20221128/include/c++/13.0.0/bits/stl_algo.h:1819:14:   required from 'void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Point*, vector<Point> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<greater<Point> >]'
/opt/compiler-explorer/gcc-trunk-20221128/include/c++/13.0.0/bits/stl_algo.h:1859:25:   required from 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Point*, vector<Point> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<greater<Point> >]'
/opt/compiler-explorer/gcc-trunk-20221128/include/c++/13.0.0/bits/stl_algo.h:1950:31:   required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Point*, vector<Point> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<greater<Point> >]'
/opt/compiler-explorer/gcc-trunk-20221128/include/c++/13.0.0/bits/stl_algo.h:4893:18:   required from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<Point*, vector<Point> >; _Compare = greater<Point>]'
<source>:27:9:   required from here
/opt/compiler-explorer/gcc-trunk-20221128/include/c++/13.0.0/bits/stl_function.h:398:20: error: no match for 'operator>' (operand types are 'const Point' and 'const Point')
398 |       { return __x > __y; }
|                ~~~~^~~~~
<source>:17:10: note: candidate: 'bool Point::operator>(const Point&)' (near match)
17 |     bool operator>(const Point &p1)
|          ^~~~~~~~
<source>:17:10: note:   passing 'const Point*' as 'this' argument discards qualifiers

将单词const添加到您的operator>方法中,使其签名变为

bool operator>(const Point &p1) const

不是

bool operator>(const Point &p1)

const添加到该方法中意味着您通知编译器该方法不会修改该方法关联的对象。这是必需的,因为greater期望被比较的对象是常量。

最新更新