错误:没有运算符"<"与这些操作数匹配

error: no operator "<" matches these operands

本文关键字:操作数 lt 运算符 错误      更新时间:2023-10-16

我收到的错误:

/usr/include/c++/7/bits/stl_function.h:386: 
error: no operator "<" matches these operands
operand types are: const QVector3D < const QVector3D
{ return __x < __y; }

我正在使用QVector3Dstd::setstd::hypot.有没有办法实现重载operator<,以便QVector3D能够在我的代码中使用它?

std::pair<QVector3D, QVector3D> NearestNeighbor::nearest_pair(std::vector<QVector3D> points)
{
// // Sort by X axis
std::sort( points.begin(), points.end(), [](QVector3D const &a, QVector3D const &b) { return a.x() < b.x(); } );
// // First and last points from `point` that are currently in the "band".
auto first = points.cbegin();
auto last = first + 1;
// // The two closest points we've found so far:
auto first_point = *first;
auto second_point = *last;
std::set<QVector3D> band{ *first, *last };
// // Lambda function to find distance
auto dist = [] (QVector3D const &a, QVector3D const &b) { return std::hypot(a.x() - b.x(), a.y() - b.y()); };
float d = dist(*first, *last);
while (++last != points.end()) {
while (last->x() - first->x() > d) {
band.erase(*first);
++first;
}
auto begin = band.lower_bound({ 0, last->y() - d, 0 });
auto end   = band.upper_bound({ 0, last->y() + d, 0 });
assert(std::distance(begin, end) <= 6);
for (auto p = begin; p != end; ++p) {
if (d > dist(*p, *last)) {
first_point = *p;
second_point = *last;
d = dist(first_point, second_point);
}
}
band.insert(*last);
}
return std::make_pair(first_point, second_point);
}

更新

在@CuriouslyRecurringThoughts的帮助下,通过替换解决了问题:

std::set<QVector3D> band{ *first, *last };

跟:

auto customComparator = [](QVector3D const &a, QVector3D const &b) { return a.y() < b.y(); };
std::set<QVector3D, decltype (customComparator)> band({ *first, *last }, customComparator);

我也可以这样做:

auto customComparator = [](QVector3D const &a, QVector3D const &b) { return a.y() < b.y(); };
std::set<QVector3D, decltype (customComparator)> band(customComparator);
band.insert(*first);
band.insert(*last);

我认为你有各种可能性。是的,您可以重载operator<如评论中所述,但我建议不要这样做:对于这个特定的用例,您需要一个特定的比较函数,也许在其他地方您需要不同的排序。除非类型的排序关系很明显,否则我建议避免重载运算符。

您可以提供自定义比较功能,如下所示

auto customComparator = [](QVector3D const &a, QVector3D const &b) { return a.x() < b.x(); };
std::set<QVector3D, decltype(customComparator)> set(customComparator);
set.insert(*first)

对我来说,目前还不清楚band集试图实现什么,但由于您正在调用y()坐标的上限和下限,也许您想在y()上进行比较,但这意味着具有相同y()的两个点将被视为相等,并且std::set不允许重复。

否则,您可以查看不需要排序的std::unordered_set(https://en.cppreference.com/w/cpp/container/unordered_set),而只需要元素具有operator ==和哈希函数。

编辑:另一种选择: 您可以使用std::vector,然后使用免费函数std::lower_boundstd::upper_bound自定义比较功能,请参阅 https://en.cppreference.com/w/cpp/algorithm/lower_bound