首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何对UDT向量进行降序排序?

如何对UDT向量进行降序排序?
EN

Stack Overflow用户
提问于 2022-11-28 08:08:54
回答 1查看 50关注 0票数 -2
代码语言:javascript
复制
#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向量。

这是一个错误:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-28 08:11:24

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

代码语言:javascript
复制
bool operator>(const Point &p1) const

而不是

代码语言:javascript
复制
bool operator>(const Point &p1)

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74597682

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档