map中插入自定義型別的key
阿新 • • 發佈:2021-01-25
技術標籤:C++
struct mycompare{ //寫一個仿函式
bool operator()(MyKey key1, MyKey key2){
return key1.mIndex > key2.mIndex;
}
};
void test02(){
map<MyKey, int, mycompare> mymap; //自動排序,自定資料型別,咋排?
mymap.insert(make_pair(MyKey(1, 2), 10));
mymap.insert(make_pair(MyKey(4, 5), 20));
for (map<MyKey, int, mycompare>::iterator it = mymap.begin(); it != mymap.end();it ++){
cout << it->first.mIndex << ":" << it->first.mID << " = " << it->second << endl;
}
}