[英]Invalid Operands to binary expression(const point and const point
map<pair<int,int>,int>pairOfNumbers;
pairOfNumbers.insert(pair<pair<int,int>,int>({1,2},2));
this is working, but这是有效的,但是
map<pair<point,point>,int>PointsOnLine;
PointsOnLine.insert(pair<pair<point,point>,int>(make_pair(points[i],points[j]),count));
this doesn't.这没有。
point is just a structure of two ints x and y; point 只是两个整数 x 和 y 的结构; I keep getting the error 'Invalid Operands to binary expression(const point and const point' this is the structure of point.
我不断收到错误'二进制表达式的无效操作数(常量点和常量点'这是点的结构。
struct point
{
int x;
int y;
public:
bool operator==(const point& p)
{
if(x==p.x && y==p.y)
return true;
else
return false;
}
bool operator!=(const point& p)
{
if(x==p.x &&y==p.y)
return false;
else
return true;
}
};
how do I insert two points and distance between them in the map?如何在 map 中插入两个点和它们之间的距离? in Xcode I get this error
在 Xcode 我得到这个错误
Your point
type does not support weak-ordering.您的
point
类型不支持弱排序。 It has no method of determining is-less-than.它没有确定 is-less-than 的方法。 You may think you don't need that because your
point
is actually tucked into a std::pair<point,point>
but you do.您可能认为您不需要它,因为您的
point
实际上隐藏在std::pair<point,point>
但您确实需要。
std::pair<T1,T2>
supports weak ordering only if T1
and T2
do. std::pair<T1,T2>
仅在T1
和T2
支持时才支持弱排序。 In your case, they're the same type, so for a std::pair<point,point>
to be used as key in a std::map<std::pair<point,pint>,T>
, point
must support weak ordering.在您的情况下,它们是相同的类型,因此对于
std::pair<point,point>
用作std::map<std::pair<point,pint>,T>
中的键, point
必须支持弱排序。
To support weak ordering, you must either provide an operator<
that compares two of your objects in question, or a comparator functor type that does the same thing.为了支持弱排序,您必须提供一个比较两个有问题的对象的
operator<
,或者提供一个执行相同操作的比较器仿函数类型。 The easiest way for you to do this would be:您执行此操作的最简单方法是:
#include <tuple>
struct point
{
int x;
int y;
bool operator <(const point& p) const
{
return std::tie(x, y) < std::tie(p.x, p.y);
}
bool operator ==(const point& p) const
{
return !(*this < p || p < *this);
}
bool operator !=(const point& p) const
{
return *this < p || p < *this;
}
};
I took liberty to redefine operator ==
and operator !=
to utilize the weak order properties of the proper operator <
.我冒昧地重新定义了
operator ==
和operator !=
以利用正确的operator <
的弱顺序属性。 It wasn't necessary, but ultimately it's just easier if operators root to as basic code as possible.这不是必需的,但最终如果操作员根植到尽可能基本的代码会更容易。 With the above change you should be able to use both
point
and std::pair<point,point>
as key types in std::map
and std::set
.通过上述更改,您应该能够同时使用
point
和std::pair<point,point>
作为std::map
和std::set
中的键类型。 In truth, a strict weak ordering can define all of the basic comparators (<=, >, >=, ,=, ==) as derivations from operator <
in one form or another (or relative to something that does).实际上,严格的弱排序可以将所有基本比较器(<=、>、>=、、=、==)定义为
operator <
以一种或另一种形式(或相对于某种形式)的派生。 I challenge you to consider them, try implementing them, and above all, writing some test harnesses that verify your implementation.我挑战你考虑它们,尝试实现它们,最重要的是,编写一些测试工具来验证你的实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.