C++ map struct find

参考: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;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值