struct #include<stdio.h> #include<string.h> #include <iostream> #include <map> using namespace std; struct D { int a; int b; bool operator < (D const &A) const { if (a<A.a) return 1;//从小到大 else if (a==A.a) { if (b>A.b) return 1; } return 0; } }t[10]; int main () { map<struct D,int> m; t[1].a=1; t[1].b=10; t[2].a=1; t[2].b=9; t[3].a=2; t[3].b=13; m[t[1]]=11; m[t[2]]=22; m[t[3]]=33; map<struct D,int>::iterator it; for (it=m.begin();it!=m.end();it++) { cout<<it->first.a<<' '<<it->first.b<<endl; } } class #include <iostream> #include <map> using namespace std; class D { private: int a; int b; public: void mod(int a,int b) { this->a=a; this->b=b; } bool operator < (D A)const //由于map的key是const 所以用到map的函数都要加const { if (a==A.a) return b<A.b; else return a<A.a; } void print()const { cout<<a<<' '<<b<<endl; } }t[10]; int main () { map<class D,int> m; t[1].mod(1,10); t[2].mod(1,9); t[3].mod(2,13); // --------------------------------------- m[t[1]]=11; m[t[2]]=22; m[t[3]]=33; map<class D,int>::iterator it; for (it=m.begin();it!=m.end();it++) { (it->first).print(); } //-------------------------------------------- }