参考:http://stackoverflow.com/questions/9647110/using-struct-as-key-and-value-for-map-find-operation-giving-error 【参考2 Answers】
示例:
struct IPPOINT
{
string ip;
string port;
};
inline bool operator<(IPPOINT const& left, IPPOINT const& right) {
if (left.ip < right.ip) { return true; }
if (left.ip > right.ip) { return false; }
return left.port < right.port;
}
int main()
{
IPPOINT i;
i.ip = "12";
i.port = "22";
IPPOINT j;
j.ip = "12";
j.port = "222";
IPPOINT k;
k.ip = "12";
k.port = "22";
map<IPPOINT,int> lst;
lst.insert(make_pair<IPPOINT,int>(i,1));
map<IPPOINT,int>::iterator l_it = lst.find(k);
if(l_it!=lst.end())
cout<<lst[k];
l_it = lst.find(j);
if(l_it!=lst.end())
cout<<lst[j];
lst.insert(make_pair<IPPOINT,int>(j,2));
l_it = lst.find(j);
if(l_it!=lst.end())
cout<<lst[j];
return 0;
}