hashmap boost

unordered_map
#include <boost/unordered_map.hpp>
typedef struct
{
	std::string pswd;
	bool inSetingPswd;
}Pswd_t;
typedef boost::unordered_map<uint32,Pswd_t> IdPswdMap; //设备信息表

IdAddrMap m_devIdAddrMap;


m_devIdAddrMap.clear();

//添加元素
Addr_t dev;
memset(&dev,0,sizeof(dev));
dev.addr=addr;
dev.cntdown=MAXCNTDOWN;
m_devIdAddrMap[devId]=dev;


//查找元素
struct sockaddr_in *pdevAddr;
IdAddrMap::iterator it2 = m_devIdAddrMap.find(devId);
if (it2 == m_devIdAddrMap.end())
	pdevAddr=0;
else
	pdevAddr =&it2->second.addr;

//删除元素
IdAddrMap::iterator it = m_devIdAddrMap.find(value.head.dst.id);
if (it != m_devIdAddrMap.end() )
   m_devIdAddrMap.erase(it);

//遍历map
for(IdAddrMap::iterator it = pPlug->m_devIdAddrMap.begin(); it != pPlug->m_devIdAddrMap.end(); it++)
{
	//cout<<it->first;
	//printf(" usr  %d\n",it->second.cntdown);
	if(it->second.cntdown>0)
	{
	it->second.cntdown--;
	}
	else if(it->second.cntdown<=0)
	{
	pPlug->m_devIdAddrMap.erase(it);
	}
}
unordered_multimap
#include <boost/unordered_map.hpp>
typedef boost::unordered_multimap<uint32,uint32> IdMap;//用户id和设备id绑定表,first is usrId

IdMap m_devusrMap;//find usr by dev :devid-usrid
//添加元素
m_devusrMap.insert(make_pair(devId,usrId));

//查找元素
 for(IdMap::iterator it = m_devusrMap.find(devId); it !=m_devusrMap.end(); it++)//遍历绑定过该设备的所有用户
{
//	std::cout<<__LINE__<<" "<<it->first<<","<<it->second<<endl;

	usrId=it->second;
	it2 = m_usrIdAddrMap.find(usrId);//在所有这些用户中找到第一个在线的用户
	if (it2 != m_usrIdAddrMap.end())
	{
		pusrAddr =&it2->second.addr;
		break;
	}
	usrId=0xffffffff;
	pusrAddr=0;
}


// 查找元素,并从m_devusrMap解除
 for(IdMap::iterator it = m_devusrMap.find(devId); it !=m_usrdevMap.end(); it++)
{
	 std::cout<<it->first<<","<<it->second<<endl;
	 if(it->second==usrId)
	 {
		 m_devusrMap.erase(it);

		 err= SMT_SUCCESS;
		 res.value.szValue[0]=err;
		 break;
	 }
	 if(it->first!=devId)
	 {
		 break;
	 }
}
 
//遍历所有元素
for(IdMap::iterator it = m_devusrMap.begin();it !=m_usrdevMap.end(); ++it)
{
	 std::cout<<it->first<<","<<it->second<<endl;
}


posted on 2013-06-09 16:39  _song  阅读(738)  评论(0编辑  收藏  举报