#include <iostream>#include<map>using namespacestd;voidFuninta[]) {a[0] = A;}structcompare{BOOL operator()(Const Char* S1,Const Char* S2)Const { returnstrcmp (S1, S2) <0; }};intMain () {map<Char*,Char*, compare>student; Charid1[ One] ="1395682451"; Charname1[ -] ="xiaoming"; Student.insert (Pair<Char*,Char*>(ID1, name1)); cout<< Student[id1] <<Endl; Charid2[ One] ="1395682451"; Charname2[ -] ="Lisi"; Student.insert (Pair<Char*,Char*>(Id2, name2)); cout<< Student[id2] <<Endl; STUDENT[ID1]=name2; cout<< Student[id2] <<Endl; Map<Char*,Char*>teacher; Teacher.insert (Pair<Char*,Char*>(ID1, name1)); Teacher.insert (Pair<Char*,Char*>(Id2, name2)); cout<<"Teacher 1 is:"<< Teacher[id1] <<Endl; cout<<"Teacher 2 is:"<< Teacher[id2] <<Endl; intb[2] = {1,2 }; Fun (b); cout<< b[0] <<Endl; return 0;}
The default is to use a memory address comparison, the above teacher is using the default comparison method, you will find that although the ID1 and ID2 content exactly the same, but still inserted successfully.
Student uses the string content comparison, insert operation insertion was unsuccessful because the same string was encountered, so returns, which is not overwritten by the original value, if the subscript override method is used, the original values will be overwritten.
C + + Map Custom comparison function