C ++ function ing

Source: Internet
Author: User


When we encounter multiple statement branches, do we first think of switc case and if else if...

These two methods are simple in coding, but when the number of branches reaches a certain level, especially when the branches have nested large segmentsCodeOr nested branches,

The code is very bloated and difficult to maintain. If else if statements with too many branches bring too many judgment sentences, which will inevitably affect the efficiency.

Three alternative methods:
1.To use map, you need to build trees and nodes, which consume more memory than the array method. The query time complexity is log (N), but it is easy to expand.


2.Using arrays to query and direct index positioning. Generally, we Initialize an array consecutively, which means that the ing from the index (type_func) to the function must be continuous,

Therefore, it is a little effort to use array indexes for extension: for example, adding or deleting elements is a little effort-consuming.


3. Use the features of C ++-Abstract inheritance. This article only describes the use of the first two methods. This method will be supplemented later.

I prefer to use code in combination with the actual situation. I will explain how to use these mappings with a piece of example code:

// The animal will perform some actions: {type_begin =-1, type_eat, type_sleep, type_walk, type_run, type_smile, type_cry, type_jump, success,}; classcanimal {public: typedefint (canimal :: * ptr_func) (bool); protected: static Map <type_func, ptr_func> s_map; static ptr_funcs_array [type_max_size]; public: canimal () {memset (s_array, 0, sizeof (s_array); Init () ;}// the returned values and parameters of the ing function must be unified inteat (bool = true) {return printf ("eatn"), 1 ;} intsleep (Bool = true) {return printf ("sleepn"), 1 ;}intwalk (bool = true) {return printf ("walkn"), 1 ;}intrun (bool = true) {return printf ("runn"), 1;} intsmile (bool = true) {return printf ("smilen"), 1;} intcry (bool = true) {return printf ("cryn"), 1 ;}intjump (bool = true) {return printf ("jumpn"), 1 ;}// initialize voidinit () {s_map [type_eat] = & canimal: Eat; s_map [type_sleep] = & canimal: sleep; s_map [type_walk] = & canimal: Walk; s_map [type_run] = & Amp; 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;} // the general practice is switc case or if else... // actually it looks like It's not too bad. On the one hand, I encapsulate the content of each module into the corresponding function. // It will look relatively concise in the branch. The actual encoding may not be the way you see it now. Voidprocess (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 ;}} // feel familiar with it! Voidprocess2 (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 );}} // map void processbyusearray (INT key, bool Val) {// array if (type_begin <key & type_max_size> key) {ptr_func pfun = s_array [Key]; if (pfun) (This-> * pfun) (VAL) ;}// map using map [] (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 ;} // map 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_funccanimal: s_array [type_max_size]; //////////////////////////////////////// /// // voidfunc_eat (Int = 0) {} voidfunc_run (Int = 0) {} voidfunc_walk (Int = 0) {} voidfunc_cry (Int = 0) {} typedef void (* ptrfun) (INT); Map <type_func, ptrfun> g_map; ptrfung_array [type_max_size]; int _ tmain (INT argc, _ tchar * argv []) {/////////////////////////////////////// /// // For ease of instruction, the following code does not perform security check // non-member function ing. // initg_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; // usingg_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 ); //////////////////////////////////////// /// // member function ing 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 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.