#include<bits/stdc++.h>
using namespace std;
struct segment{
int a;
int b;
int c;
bool const operator<(const segment &o) const {
return a < o.a;
}
};
int main()
{
map<segment,int> myMap;
map<segment,int>::iterator it;
struct segment x,y,z;
x.a=2;
x.b=4;
x.c=6;
y.a=2;
y.b=5;
y.c=8;
z.a=2;
z.b=4;
z.c=6;
myMap[y]++;
myMap[z]++;
myMap[x]++;
for(it =myMap.begin(); it != myMap.end(); it++)
cout<<(*it).first.a<<" "<<(*it).second<<endl;
return 0;
}
它給出的結果比較運營商在C++地圖結構關鍵
2 3
,但我想它打印
2 1
2 2
總之我希望增加的地圖,如果價值提供完全相同的結構實例,而不是製作新副本
然後你必須改變少運算符和比較所有成員。 – Tim3880
我沒有得到你所說的。你可以給比較器的代碼嗎? – Maroof
我試過一個嵌套的if else語句,但它不能正常工作 – Maroof