标签:
#include <map> #include <iostream> using namespace std; class Person { public : int age; int ID; Person(int age, int id) { this->age = age; this->ID = id; } }; struct CmpByKey { bool operator()(const Person * k1, const Person * k2) { return k1->age < k2->age; } }; map<Person * , int, CmpByKey> iMap; int main() { Person * pPerson; for(int i = 10; i > 0; i--) { pPerson = new Person(i, i); iMap.insert(pair<Person *, int>(pPerson, i)); } map<Person *,int, CmpByKey>::iterator iBegin = iMap.begin(); map<Person *,int, CmpByKey>::iterator iEnd = iMap.end(); while(iBegin != iEnd) { cout<<iBegin->first->ID << ": "; cout << iBegin->first->age << endl; iBegin++; } cout<<"####################"<<endl; pPerson = new Person(1, 10); pair<map<Person * , int, CmpByKey>::iterator, bool> Insert_Pair; Insert_Pair = iMap.insert(pair<Person *, int>(pPerson, 10)); if(Insert_Pair.second == true) { cout<<"Insert Successfully"<<endl; } else { cout<<"Insert Failure"<<endl; } iBegin = iMap.begin(); while(iBegin != iEnd) { cout<<iBegin->first->ID << ": "; cout << iBegin->first->age<<endl; iBegin++; } }
标签:
原文地址:http://www.cnblogs.com/kisstherain/p/4292286.html