使用C ++ std :: sort和lambda比较器

时间:2016-09-04 09:53:02

标签: c++ sorting c++11 lambda

我正在尝试使用std :: sort -

对对象数组进行排序
sort(convexHull.getSet(), convexHull.getSet()+convexHull.size(),
     [](const Point & a, const Point & b) -> bool
     {             if (a.getX() < b.getX())
         return true;
     else if (a.getX() == b.getX())
         return a.getY() < b.getY();
     else
         return false;; }
);

其中convexHull.getSet()返回指向数组开头的指针。

解释here

但是我的编译器(Clion)出现了一个非常长的错误,关于我的赋值运算符 -

#include <cmath>
#include <string>

using namespace std;

class Point
{

private:
    int _x;
    int _y;
    double _arg;

    void setArg()
    {        
       if(sqrt(_x*_x + _y*_y) == 0)
            _arg = 5;
        else
            _arg = _y / sqrt(_x*_x + _y*_y);
    }

public:
    Point () : Point(0, 0) {}

Point (const int x, const int y) : _x(x), _y(y)
{
    setArg();
}

int getX () const
{ return _x; }

int getY () const
{ return _y; }


    ~Point () {}

    Point& operator= (const Point &rval);
};

(其中setArg只是计算与正x轴的点角度)。

我已经使用此代码测试了运算符 -

Point p(5,5);
Point t(3,3);
t=p;
t.setXY(7,7);

似乎工作正常。

它很长,所以我只发布它的一部分 -

   In file included from /usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/algorithm:62:0,
                 from /cygdrive/c/Users/o/Desktop/CPP/ex1/cppex1/ex1/ConvexHull.cpp:7:
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/stl_algo.h: In instantiation of 'void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = const Point*; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(const Point&, const Point&)> >]':
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/stl_algo.h:1880:25:   required from 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = const Point*; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(const Point&, const Point&)> >]'
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/stl_algo.h:1966:31:   required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = const Point*; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(const Point&, const Point&)> >]'
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/stl_algo.h:4729:18:   required from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = const Point*; _Compare = main()::<lambda(const Point&, const Point&)>]'
/cygdrive/c/Users/o/Desktop/CPP/ex1/cppex1/ex1/ConvexHull.cpp:75:5:   required from here
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/stl_algo.h:1847:17: error: passing 'const Point' as 'this' argument discards qualifiers [-fpermissive]
        *__first = _GLIBCXX_MOVE(__val);
                 ^
In file included from /cygdrive/c/Users/o/Desktop/CPP/ex1/cppex1/ex1/PointSet.h:9:0,
                 from /cygdrive/c/Users/o/Desktop/CPP/ex1/cppex1/ex1/ConvexHull.cpp:4:
/cygdrive/c/Users/o/Desktop/CPP/ex1/cppex1/ex1/Point.h:45:12: note:   in call to 'Point& Point::operator=(const Point&)'

这可能也很重要 -

/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/predefined_ops.h:123:46: note: candidate: bool (*)(Point&, Point&) <conversion>
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/predefined_ops.h:123:46: note:   conversion of argument 3 would be ill-formed:
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/predefined_ops.h:123:46: error: binding 'const Point' to reference of type 'Point&' discards qualifiers
/cygdrive/c/Users/o/Desktop/CPP/ex1/cppex1/ex1/ConvexHull.cpp:68:38: note: candidate: main()::<lambda(Point&, Point&)> <near match>
          [](Point & a, Point & b) -> bool

1 个答案:

答案 0 :(得分:1)

显然问题不在于比较,而在于您传递给std::sort的问题。什么是

的声明
convexHull.getSet()

如果例如返回const Point *那么你就会遇到const正确性问题,因为std::sort需要能够写入重新排列元素。