ACE_Hash_Map的使用樣本

來源:互聯網
上載者:User

一位網友說他不能在ACE中正常地使用嵌套使Hash_Map,我寫下下面的代碼作了簡單測試,發現在ACE使用Hasp_Map還是可以的.

//  TestHaspMap.cpp : Defines the entry point for the console application.
//

#include  " stdafx.h "
#include  " ACE/Basic_Types.h "
#include  " ACE/Map_Manager.h "
#include  " ACE/SString.h "
#include  " ACE/Log_Msg.h "
#include  " ACE/Null_Mutex.h "

class  PhoneNumber
{
public :
    PhoneNumber( const   char *  number):number_(number)
    {

    }
     const   char *  getNumber( void )
    {
         return  number_.c_str();
    }
private :
    ACE_SString  number_; 
};

typedef ACE_UINT16 CONNECTION_ID;

typedef ACE_Null_Mutex MAP_MUTEX;

typedef ACE_Map_Manager < CONNECTION_ID,PhoneNumber * , MAP_MUTEX >
   CONNECTION_MAP;

typedef ACE_Map_Iterator < CONNECTION_ID, PhoneNumber * , MAP_MUTEX >
     CONNECTION_MAP_ITERATOR;

typedef ACE_Map_Entry < CONNECTION_ID, PhoneNumber *>
    CONNECTION_MAP_ENTRY;

typedef ACE_Map_Manager < CONNECTION_ID,CONNECTION_MAP * ,MAP_MUTEX >
     MAP_MAP;

typedef ACE_Map_Iterator < CONNECTION_ID, CONNECTION_MAP * , MAP_MUTEX >
MAP_MAP_ITERATOR;

typedef ACE_Map_Entry < CONNECTION_ID, CONNECTION_MAP *>
MAP_MAP_ENTRY;

int  ACE_TMAIN( int  argc, _TCHAR *  argv[])
{

    CONNECTION_MAP connection_map;
    PhoneNumber  me( " 13717928*** " );
    PhoneNumber  office( " 010-82378*** " );

    PhoneNumber  home( " 13366686** " );
     int  result  =   - 1 ;
    result  =  connection_map.bind( 1 , & me);
     ACE_ASSERT(result  ==   0 );
    result  =   connection_map.bind( 2 , & office);
    ACE_ASSERT(result  ==   0 );
    result  =   connection_map.bind( 3 , & home);
    ACE_ASSERT(result  ==   0 );
    result  =  connection_map.bind( 5 , & home);
    ACE_ASSERT(result  ==   0 );

    connection_map.unbind( 3 );
    CONNECTION_MAP_ITERATOR it(connection_map);
  
     for  (CONNECTION_MAP_ENTRY  * i  =   0 ;
        it.next (i)  !=   0 ;
        it.advance ())
    {
        PhoneNumber *  phone  =  i -> int_id_;
        ACE_DEBUG((LM_INFO,ACE_TEXT( " 串連號碼 %d, 電話號碼 %s/n " ),
            i -> ext_id_,
            phone -> getNumber()));
    }

   PhoneNumber *  any_phone  =   0 ;
   CONNECTION_ID any_id  =   5 ;

    result  =  connection_map.find(any_id,any_phone);
    if ( result  !=    - 1 )
   {
       ACE_DEBUG((LM_INFO,ACE_TEXT( " 串連號碼 %d, 電話號碼 %s/n " ),
           any_id,
           any_phone -> getNumber()));
   } 
    else
   {
       ACE_DEBUG((LM_INFO,ACE_TEXT( " 串連號碼 %d, 電話號碼 %s/n " ),
           any_id,
            " not found. " ));
   }

   MAP_MAP map_map;
   map_map.bind( 1 , & connection_map);
   size_t n  =  map_map.current_size();
   MAP_MAP_ITERATOR map_it(map_map);

    for  (MAP_MAP_ENTRY  * j  =   0 ;
       map_it.next (j)  !=   0 ;
       map_it.advance ())
   {
       CONNECTION_MAP *  cur_map  =  j -> int_id_;
        size_t count  =  cur_map -> current_size();
         PhoneNumber *  any_phone2  =   0 ;
          if  (cur_map -> find( 2 ,any_phone2) !=- 1 )
         {
             ACE_DEBUG((LM_INFO,ACE_TEXT( " 內部map中電話號碼 %s/n " ),
                 any_phone2 -> getNumber()));
         }
   }

     return   0 ;
}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.