我用dev c++ 执行如下代码:
#include<iostream>
#include<cstdio>
#include<map>
using namespace std;
struct posn
{
int x,y,dis;
bool operator < (const posn& b)const{
return dis<b.dis;
}
};
map<posn,bool>vis;
int main()
{
posn test;
test.x=1; test.y=1; test.dis=1;
cout<<vis[test]<<endl;
vis[test]=true;
cout<<vis[test]<<endl;
test.x=2;
cout<<vis[test]<<endl;
}
得到输出结果
0
1
1
我不明白为什么x=2的test对应的value也会变成1;
我注意到也许这和结构体中的重载运算符有关,但是具体原理我就搞不清楚了,求各位解答。
https://www.cnblogs.com/fuqia/p/9511994.html
map的key是一个常量,每次你修改test,在用test索引,都会调用<在map中进行比较,找到与之相等的对象
(我估计内部逻辑应该是!(a<b)&&!(b<a)为true,则相等,反正则不想等),因为你重载了<,所以比较相等时,就会根据<进行判断