使用C++的STD::用LAMBDA比较器排序
我正在尝试使用std::sort-使用C++的STD::用LAMBDA比较器排序,c++,sorting,c++11,lambda,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())
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;; }
);
它似乎工作得很好
它很长,所以我只发布其中的一部分-
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
显然,问题不在于比较,而在于传递给std::sort的内容。申报的目的是什么
convexHull.getSet()
?
例如,如果它返回一个常量点*,那么您就有一个常量正确性问题,因为std::sort需要能够写入以重新排列元素。Point&Point::operator=const Point&rval可能会解决这个问题。@πάνταῥεῖ 这不是关于重载的问题,而是关于使用排序的问题。我的运算符工作正常。@n.m.关于我的赋值运算符,您的编译器错误显示您有Point&operator=Point&rval;但您声称您拥有Point&operator=const Point&rval;。检查您的源是否是最新的。错误消息现在不同了。此外,现在您的编译器抱怨[]点&a,点&b->bool,而您声称您有[]常数点&a,常数点&b->bool。除非你提供一份报告,否则就不可能弄清楚到底发生了什么。完全同意你对OP的分析,一切正常:!但这不是对一个不完整问题的评论,而不是一个明确的答案吗?这似乎是问题所在,我更改了getSet的签名以返回一个非常量指针,现在我的代码已经编译好了。
/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
convexHull.getSet()