错误:没有运算符"<"与这些操作数匹配
error: no operator "<" matches these operands
我收到的错误:
/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; }
我正在使用QVector3D
与std::set
和std::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_bound
和std::upper_bound
自定义比较功能,请参阅 https://en.cppreference.com/w/cpp/algorithm/lower_bound
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 控制允许动态运行c++的并发操作数
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 为什么C++逐位AND运算符在不同大小的操作数中表现为这样
- <<操作员在下面的行中工作
- 错误 C2679:二进制"<<":未找到采用类型 'std::string_view' 的右侧操作数的运算符(或者没有可接受的转换)
- C++ 带有向量的 IF 计算操作数
- 是否所有C++运算符都接受操作数?
- 一元*运算符的操作数是否期望一个 prvalue
- 与'operator='不匹配(操作数类型'String'且"void")
- 不正确的操作数类型 MSVC
- C2678 二进制 '==':未找到采用 'Card' 类型左操作数的运算符(或者没有可接受的转换)
- 错误:类型"double()"和"double()"的操作数无效到二进制&quo
- 我的运算符重载是否有效<<(流插入)左操作数不是 ostream
- 没有运算符"="与这些操作数匹配,JUCE 窗口演示应用程序
- 如果我在指针中使用 ++ 操作数,我的值就会出错
- 二进制表达式的操作数无效 - 使用 for 和迭代器
- C2679 二进制"<<":未找到采用右操作数类型的运算符
- CPP 运算符重载操作数位置 [] 与 ==