zoukankan      html  css  js  c++  java
  • Using Input String to Call Function

    #include <iostream>
    #include 
    <string>
    using namespace std;

    void male()
      {
      cout 
    << "I'm a man.\n";
      }

    void female()
      {
      cout 
    << "I'm a woman.\n";
      }

    struct
      {
      (
    void)(*fn)();
      
    const char* key;
      }
    function_lookup_table[] 
    =
      {
      { 
    &male,   "male"   },
      { 
    &female, "female" },
      { NULL,    NULL     }
      };

    bool lookup_and_call( const string& name )
      {
      
    for (int i = 0; function_lookup_table[ i ].fn; i++)
        
    if (name == function_lookup_table[ i ].key)
          {
          (
    *(function_lookup_table[ i ].fn))();
          
    return true;
          }
      
    return false;
      }

    int main()
      {
      
    string users_gender;
      cout 
    << "Enter male or female> " << flush;
      getline( cin, users_gender );
      
    if (!lookup_and_call( users_gender ))
        {
        cout 
    << "What?\n";
        }
      
    return 0;
      } 
  • 相关阅读:
    python基础 列表生成式
    docker 基础
    xpath例子
    redis删除以什么开头的key
    redis 关闭持久化
    python爬虫 保存页面
    python爬虫操作cookie
    SQl函数的写法
    加料记录(大屏幕)
    ios 调试
  • 原文地址:https://www.cnblogs.com/smartvessel/p/2015994.html
Copyright © 2011-2022 走看看