xiaozhangma @ 2023-11-16 15:48:07
题解里都是这样写的:
struct Info{
int dis;
int pos;
bool operator <(const Info &x)const{
return x.dis < dis;
}
};
蓝书上则是这样:
struct Info{
int dis;
int pos;
};
bool operator <(const Info &x, const Info &y){
return x.dis < y.dis;
}
但用蓝书上的写法会出错:(
by 5t0_0r2 @ 2023-11-16 15:50:41
@xiaozhangma 你仔细分析就会发现下面的代码刚好是上面的反过来。(注意 x 的位置)
by wangbinfeng @ 2023-11-16 15:51:38
by wangbinfeng @ 2023-11-16 15:52:17
5t0_0r2说的没啥问题
by 5t0_0r2 @ 2023-11-16 15:53:16
@xiaozhangma 上面的代码如果按照下面写应该是:
struct Info{
int dis;
int pos;
bool operator <(const Info &x)const{
return dis < x.dis;
}
};
by xiaozhangma @ 2023-11-16 15:57:30
@5t0_0r2 能解释下括号后面的const是什么意思吗?
by Imiya @ 2023-11-16 15:59:04
@xiaozhangma 你的 dis,pos
只读。
by xiaozhangma @ 2023-11-16 16:01:08
@Imiya
struct Info{
int dis;
int pos;
bool operator <(const Info &x)const//我指的是这个{
return x.dis < dis;
}
};
by 5t0_0r2 @ 2023-11-16 16:01:30
@xiaozhangma 这个我也不知道,反正去了会 CE。
by xiaozhangma @ 2023-11-16 16:02:03
第二种写法在括号后面加const就会报错
by xiaozhangma @ 2023-11-16 16:02:59
@5t0_0r2 第二种写法把“<”改成“>”就对了