首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误:没有运算符"<“匹配这些操作数

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

Stack Overflow用户
提问于 2019-07-06 05:19:04
回答 1查看 546关注 0票数 1

我收到的错误:

/usr/include/c++/7/bit/stl_function.h.h:386: error: no操作符"<“匹配这些操作数操作数类型为: const QVector3D < const QVector3D {返回__x < __y;}

我用的是QVector3Dstd::setstd::hypot。是否有任何方法来实现重载的operator<以便QVector3D能够在我的代码中使用它?

代码语言:javascript
复制
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帮助解决问题,方法是替换:

代码语言:javascript
复制
std::set<QVector3D> band{ *first, *last };

通过以下方式:

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

我也可以这样做:

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

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-06 06:46:32

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

您可以提供一个自定义比较函数,如下所示

代码语言:javascript
复制
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 (set),它不需要排序,只需要元素具有operator ==和哈希函数。

编辑:另一种选择:您可以使用std::vector,然后使用带有自定义比较函数的免费函数std::lower_boundstd::upper_bound,请参阅bound

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

https://stackoverflow.com/questions/56911430

复制
相关文章

相似问题

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