多索引容器boost::multi_index_container储存共享智能指针boost::shared_ptr

本人也是搞游戏编程的.由客户端转向服务端近一年多来一直在从事服务器的开发.

拜读了本站一篇博文 [HengStar-Boost讲堂]多索引容器multi_index_container实战后我觉得有必要再补充一个multi_index_container存储智能指针的范例.

可以说multi_index_container和boost::shared_ptr是一对完美的搭档,对维护的数据即达到了多索引的功能又方便以指针的方式安全操作.话不多说,贴代码:

 

#include <iostream>
#include <boost/smart_ptr.hpp>
#include <boost/multi_index_container.hpp>
#include "boost/multi_index/member.hpp"  
#include "boost/multi_index/ordered_index.hpp"  

using boost::multi_index_container;
using namespace boost::multi_index;

class CUser
{
public:
 CUser( std::string _name, unsigned int _id, int _money )
  :name(_name)
  ,id(_id)
  ,money(_money)
 {
 }
 ~CUser()
 {
 };
public:
 std::string  name;
 unsigned int id;
 int    money;
};


struct ID{};
struct NAME{};
struct MONEY{};

typedef 
boost::multi_index_container< 
boost::shared_ptr<CUser>, 
indexed_by< 
ordered_unique< 
tag<ID>,  BOOST_MULTI_INDEX_MEMBER(CUser,unsigned int,id)>,  
ordered_unique< 
tag<NAME>,BOOST_MULTI_INDEX_MEMBER(CUser,std::string,name)>, 
ordered_non_unique< 
tag<MONEY>, BOOST_MULTI_INDEX_MEMBER(CUser,int,money)>   

> UserContainer;

int main()
{
 UserContainer uc;

 {//用花括号、使智能指针出栈引用计数-1
  boost::shared_ptr<CUser> u1 = boost::shared_ptr<CUser>( new CUser( "name1", 1, 10 ) );
  boost::shared_ptr<CUser> u2 = boost::shared_ptr<CUser>( new CUser( "name2", 2, 887 ) );
  boost::shared_ptr<CUser> u3 = boost::shared_ptr<CUser>( new CUser( "name3", 3, 887 ) );
  boost::shared_ptr<CUser> u4 = boost::shared_ptr<CUser>( new CUser( "name4", 3, 60 ) );
  uc.insert( u1 );
  uc.insert( u2 );
  uc.insert( u3 );
  //由于u3、u4的id相同所以插入会失败
  std::pair<UserContainer::iterator,bool> ret = uc.insert( u4 );
  if ( ! ret.second )
   printf_s( "insert User fail\n" );
 }
 printf_s( "User amount:%d\n", uc.size() );
 //为所有的用户增加1数量的money
 for ( UserContainer::iterator it = uc.begin(); it != uc.end(); it++ )
 {
  boost::shared_ptr<CUser> user = *it;
  user->money += 1;
 }
 //查找name为"name2"的用户
 UserContainer::index<NAME>::type& indexOfName = uc.get<NAME>();
 UserContainer::index<NAME>::type::iterator  it_name  = indexOfName.find( "name2" );
 if ( it_name != indexOfName.end() )
 {
  boost::shared_ptr<CUser> user = *it_name;
  printf_s( "id:%d\tmoney:%d\n", user->id, user->money  );
 }
 //查找id为3的用户
 UserContainer::index<ID>::type& indexOfId = uc.get<ID>();
 UserContainer::index<ID>::type::iterator it_id  = indexOfId.find( 3 );
 if ( it_id != indexOfId.end() )
 {
  boost::shared_ptr<CUser> user = *it_id;
  printf_s( "name:%s\tmoney:%d\n", user->name.c_str(), user->money  );
 }
 //查找money为888的用户
 UserContainer::index<MONEY>::type& indexOfMoney = uc.get<MONEY>();
 UserContainer::index<MONEY>::type::iterator it_moneyL = indexOfMoney.lower_bound( 888 ); 
 UserContainer::index<MONEY>::type::iterator it_moneyU = indexOfMoney.upper_bound( 888 ); 
 while( it_moneyL != it_moneyU ) 
 { 
  boost::shared_ptr<CUser> user = *it_moneyL;
  
  printf_s( "name:%s\tid:%d\n", user->name.c_str(), user->id  );
  it_moneyL++; 
 }

 system( "pause" );
 return 0;
}

 

你可能感兴趣的:(多索引容器boost::multi_index_container储存共享智能指针boost::shared_ptr)