欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

深入C++ 函數(shù)映射的使用詳解

 更新時間:2013年07月20日 16:08:49   作者:  
我比較喜歡用代碼結(jié)合實(shí)際來講解,下面我將以一段事例代碼來講解如何使用這幾種映射

想想我們在遇到多語句分支時是不是首先想到的是 switc case 和 if else if ...
這2種方式在編碼方面確實(shí)簡單少,但是當(dāng)分支達(dá)到一定數(shù)量后,特別是分支內(nèi)部有嵌套大段代碼或者再嵌套分支,代碼會顯得異常臃腫,十分難以維護(hù),對于if else if 語句過多的分支帶來過多的判定句,勢必會影響效率。

3種替代方法簡述:
1.使用map,需要構(gòu)建樹和節(jié)點(diǎn),比數(shù)組的方式消耗更多的內(nèi)存,查詢時間復(fù)雜度為Log(N),但擴(kuò)展起來方便。

2.使用數(shù)組,查詢直接索引定位, 一般來講我們是連續(xù)的初始化數(shù)組,也就意味索引(type_func)到函數(shù)的映射要連續(xù),
所以使用數(shù)組索引在擴(kuò)展上來講:例如增刪元素是稍微麻煩點(diǎn)的。

3. 使用C++的特性---抽象繼承來實(shí)現(xiàn),本文只講前2種的使用,這種方式以后再補(bǔ)充。

復(fù)制代碼 代碼如下:

// 動物會一些動作
enum type_func
{
 type_begin = -1,
 type_eat,
 type_sleep,
 type_walk,
 type_run,
 type_smile,
 type_cry,
 type_jump,
 type_max_size,
};
class CAnimal
{
public:
 typedef int (CAnimal::*ptr_func)(bool);
protected:
 static map<type_func,ptr_func> s_map;     
 static ptr_func     s_array[type_max_size];   
public:
 CAnimal()
 {
  memset(s_array,0,sizeof(s_array));
  Init(); 
 }
 // 需要映射函數(shù)的返回值 和 參數(shù)必須 統(tǒng)一
 int  eat  (bool= true)  { return printf("eatn") ,1; }
 int  sleep (bool= true)  { return printf("sleepn"),1; }
 int  walk (bool= true)  { return printf("walkn") ,1; }
 int  run  (bool= true)  { return printf("runn") ,1; }
 int  smile (bool= true)  { return printf("smilen"),1; }
 int  cry  (bool= true)  { return printf("cryn") ,1; }
 int  jump (bool= true)  { return printf("jumpn") ,1; }
 // 初始化
 void Init ()
 {
  s_map[type_eat]  = &CAnimal::eat;
  s_map[type_sleep] = &CAnimal::sleep;
  s_map[type_walk] = &CAnimal::walk;
  s_map[type_run]  = &CAnimal::run;
  s_map[type_smile] = &CAnimal::smile;
  s_map[type_cry]  = &CAnimal::cry;
  s_map[type_jump] = &CAnimal::jump;
  s_array[type_eat] = &CAnimal::eat;
  s_array[type_sleep] = &CAnimal::sleep;
  s_array[type_walk] = &CAnimal::walk;
  s_array[type_run] = &CAnimal::run;
  s_array[type_smile] = &CAnimal::smile;
  s_array[type_cry] = &CAnimal::cry;
  s_array[type_jump] = &CAnimal::jump;
 }
 // 一般做法是switc case 或者 if else... 
 // 其實(shí)這里看起來還不算糟糕,一方面這里我把每個模塊內(nèi)容都封裝到相應(yīng)函數(shù)了
 // 分支內(nèi)部才會看起來相對簡潔,實(shí)際編碼中可能就不是你現(xiàn)在所看到的方式。
 void Process (type_func type)
 {
  switch (type)
  {
  case type_eat:  eat();  break;
  case type_sleep: sleep(); break;
  case type_walk:  walk();  break;
  case type_run:  run();  break;
  case type_smile: smile(); break;
  case type_cry:  cry();  break;
  case type_jump:  jump();  break;
  }
 }
 // 很熟悉的感覺吧! :)
 void Process2(type_func type)
 {
  if (type_eat == type)
  {
   eat();
  }
  else if (type_sleep == type)
  {
   sleep();
  }
  else if (type_walk == type)
  {
   walk();
  }
  else if (type_run == type)
  {
   run();
  }
  else if (type_smile == type)
  {
   smile();
  }
  else if (type_cry == type)
  {
   cry();
  }
  else if (type_jump == type)
  {
   jump();
  }
 }
 // 使用map 映射
 void ProcessByUseMap(int key, bool val)
 {
  map<type_func,ptr_func>::iterator it =  s_map.find((type_func)key);
  if (it != s_map.end())
  {
   ptr_func pFun = it->second;
   if (pFun) 
    (this->*pFun)(val);
  }
 }
 // 使用數(shù)組 映射
 void ProcessByUseArray(int key, bool val)
 {
  // 數(shù)組
  if (type_begin < key && type_max_size > key)
  {
   ptr_func pFun = s_array[key];
   if (pFun) 
    (this->*pFun)(val);
  }
 }
 // 使用map 映射
 int operator[] (int key)
 {
  map<type_func,ptr_func>::iterator it =  s_map.find((type_func)key);
  if (it != s_map.end())
  {
   ptr_func pFun = it->second;
   if (pFun)  return (this->*pFun)(false);
  }
  return NULL;
 }
 // 使用數(shù)組 映射
 int operator() (int key,bool val)
 {
  if (type_begin < key && type_max_size > key)
  {
   ptr_func pFun = s_array[key];
   if (pFun)  return (this->*pFun)(val);
  }
  return NULL;
 }
};
map<type_func, CAnimal::ptr_func> CAnimal::s_map;     
CAnimal::ptr_func     CAnimal::s_array[type_max_size];
//////////////////////////////////////////////////////////////////////////
// 非成員函數(shù)
void func_eat(int = 0) { }
void func_run(int = 0) { }
void func_walk(int =0) { }
void func_cry(int = 0) { }
typedef void (*ptrFun)(int);
map<type_func,ptrFun> g_map;
ptrFun     g_array[type_max_size];
int _tmain(int argc, _TCHAR* argv[])
{
 //////////////////////////////////////////////////////////////////////////
 // 為了便于說明,下面代碼不做安全檢查
 // 非成員函數(shù)映射2種用法
 // init
 g_map[type_eat] = func_eat;
 g_map[type_run] = func_run;
 g_map[type_walk] = func_walk;
 g_map[type_cry] = func_cry;
 g_array[type_eat] = func_eat;
 g_array[type_run] = func_run;
 g_array[type_walk] = func_walk;
 g_array[type_cry] = func_cry;
 // using
 g_map[type_eat](1);
 g_map[type_run](2);
 g_map[type_walk](3);
 g_map[type_cry](4);
 g_array[type_eat](1);
 g_array[type_run](2);
 g_array[type_walk](3);
 g_array[type_cry](4);
 //////////////////////////////////////////////////////////////////////////
 // 成員函數(shù)映射使用
 CAnimal Dog;
 Dog.Process(type_eat);
 Dog.ProcessByUseMap(type_run,true);
 Dog.ProcessByUseArray(type_cry,false);
 Dog[type_walk];
 Dog(type_sleep,true);
 Dog(type_run,false);
 return 1;
}

相關(guān)文章

最新評論