C++ C++;如何使用较少的条件语句?
对于我的任务,我将存储用户登录信息。我正在接收一个字符串,它是命令。命令可以是创建、登录、删除等。共有10个选项,即10个不同的字符串。有人能解释一种更有效的方法来写这个而不是10个if和else if语句吗?基本上,除了使用一堆if(string==“one”)、else if(string==“two”)之外,我还应该如何格式化/构造东西。谢谢也许您可以创建一个C++ C++;如何使用较少的条件语句?,c++,string,conditional,C++,String,Conditional,对于我的任务,我将存储用户登录信息。我正在接收一个字符串,它是命令。命令可以是创建、登录、删除等。共有10个选项,即10个不同的字符串。有人能解释一种更有效的方法来写这个而不是10个if和else if语句吗?基本上,除了使用一堆if(string==“one”)、else if(string==“two”)之外,我还应该如何格式化/构造东西。谢谢也许您可以创建一个std::map并使用map lookup来获取传递的命令的代码-您可以稍后打开该号码。或者创建一个enum命令,使用std::ma
std::map
并使用map lookup来获取传递的命令的代码-您可以稍后打开该号码。或者创建一个enum命令
,使用std::map
并使用开关
例如:
enum Command
{
CREATE,
LOGIN,
...
};
std::map<std::string, Command> commandNameToCode;
// fill the map with appropriate values
commandNameToCode["create"] = Command::CREATE;
// somehow get command name from user and store in the below variable (not shown)
std::string input;
// check if the command is in the map and if so, act accordingly
if(commandNameToCode.find(input) != commandNameToCode.end())
{
switch(commandNameToCode[input])
{
case CREATE:
// handle create
break;
...
}
}
enum命令
{
创造
登录,
...
};
std::map commandNameToCode;
//用适当的值填充映射
commandNameToCode[“create”]=Command::create;
//以某种方式从用户处获取命令名并存储在以下变量中(未显示)
std::字符串输入;
//检查该命令是否在地图中,如果在地图中,请相应地采取行动
if(commandNameToCode.find(input)!=commandNameToCode.end())
{
开关(commandNameToCode[输入])
{
案例创建:
//句柄创建
打破
...
}
}
也许您可以创建一个std::map
并使用map lookup来获取传递的命令的代码-您可以稍后打开该号码。或者创建一个enum命令
,使用std::map
并使用开关
例如:
enum Command
{
CREATE,
LOGIN,
...
};
std::map<std::string, Command> commandNameToCode;
// fill the map with appropriate values
commandNameToCode["create"] = Command::CREATE;
// somehow get command name from user and store in the below variable (not shown)
std::string input;
// check if the command is in the map and if so, act accordingly
if(commandNameToCode.find(input) != commandNameToCode.end())
{
switch(commandNameToCode[input])
{
case CREATE:
// handle create
break;
...
}
}
enum命令
{
创造
登录,
...
};
std::map commandNameToCode;
//用适当的值填充映射
commandNameToCode[“create”]=Command::create;
//以某种方式从用户处获取命令名并存储在以下变量中(未显示)
std::字符串输入;
//检查该命令是否在地图中,如果在地图中,请相应地采取行动
if(commandNameToCode.find(input)!=commandNameToCode.end())
{
开关(commandNameToCode[输入])
{
案例创建:
//句柄创建
打破
...
}
}
您可以使用地图进行比较
大概是这样的:
typedef void (*func_t) ();
初始化映射:
std::map<std::string, std::function<void(std::string&)>> map;
map["login"] = std::bind(&Class::DoLogin, this, std::placeholders::_1);
map["create"] = std::bind(&Class::DoCreate, this, std::placeholders::_1);
map.at(rx.msg_type)(rx.msg_data);
void Class::DoLogin(const std::string& data)
{
// do login
}
处理程序:
std::map<std::string, std::function<void(std::string&)>> map;
map["login"] = std::bind(&Class::DoLogin, this, std::placeholders::_1);
map["create"] = std::bind(&Class::DoCreate, this, std::placeholders::_1);
map.at(rx.msg_type)(rx.msg_data);
void Class::DoLogin(const std::string& data)
{
// do login
}
您可以使用一个地图来进行比较 大概是这样的:
typedef void (*func_t) ();
初始化映射:
std::map<std::string, std::function<void(std::string&)>> map;
map["login"] = std::bind(&Class::DoLogin, this, std::placeholders::_1);
map["create"] = std::bind(&Class::DoCreate, this, std::placeholders::_1);
map.at(rx.msg_type)(rx.msg_data);
void Class::DoLogin(const std::string& data)
{
// do login
}
处理程序:
std::map<std::string, std::function<void(std::string&)>> map;
map["login"] = std::bind(&Class::DoLogin, this, std::placeholders::_1);
map["create"] = std::bind(&Class::DoCreate, this, std::placeholders::_1);
map.at(rx.msg_type)(rx.msg_data);
void Class::DoLogin(const std::string& data)
{
// do login
}
我希望您的讲师希望您将函数提取到另一个可重用函数:
string action;
command = CreateAction(action);
command.Do(...);
当然,在CreateAction类中,仍然需要有条件来确定需要创建哪些命令
AbstractCommand CreateAction(action)
{
if (action == "login")
return LoginCommand();
else if (action == "remove")
return RemoveCommand();
..... etc etc
}
如果你真的想去掉所有的条件,你可以创建一些自注册命令,但这需要更多的代码和类
您应该查找和之类的内容,我希望您的讲师希望您将函数提取到另一个可重用函数:
string action;
command = CreateAction(action);
command.Do(...);
当然,在CreateAction类中,仍然需要有条件来确定需要创建哪些命令
AbstractCommand CreateAction(action)
{
if (action == "login")
return LoginCommand();
else if (action == "remove")
return RemoveCommand();
..... etc etc
}
如果你真的想去掉所有的条件,你可以创建一些自注册命令,但这需要更多的代码和类
您应该查找和之类的内容。您可以使用函数指针和查找表
typedef void (*Function_Pointer)(void);
void Create(void);
void Login(void);
void Remove(void);
struct Function_Option_Entry
{
const char * option_text;
Function_Pointer p_function;
};
Function_Option_Entry option_table[] =
{
{"one", Create},
{"two", Login},
{"three", Remove},
};
const unsigned int option_table_size =
sizeof(option_table) / sizeof(option_table[0]);
//...
std::string option_text;
//...
for (i = 0; i < option_table_size; ++i)
{
if (option_text == option_table[i].option_text)
{
option_table[i].p_function();
break;
}
}
typedef void(*函数指针)(void);
无效创建(void);
作废登录(作废);
无效删除(void);
结构函数选项项
{
常量字符*选项文本;
函数\指针p\函数;
};
函数选项输入选项表[]=
{
{“一”,创建},
{“2”,登录},
{“三”,删除},
};
常量无符号整数选项表大小=
sizeof(选项表)/sizeof(选项表[0]);
//...
std::字符串选项\u文本;
//...
对于(i=0;i
您可以使用函数指针和查找表
typedef void (*Function_Pointer)(void);
void Create(void);
void Login(void);
void Remove(void);
struct Function_Option_Entry
{
const char * option_text;
Function_Pointer p_function;
};
Function_Option_Entry option_table[] =
{
{"one", Create},
{"two", Login},
{"three", Remove},
};
const unsigned int option_table_size =
sizeof(option_table) / sizeof(option_table[0]);
//...
std::string option_text;
//...
for (i = 0; i < option_table_size; ++i)
{
if (option_text == option_table[i].option_text)
{
option_table[i].p_function();
break;
}
}
typedef void(*函数指针)(void);
无效创建(void);
作废登录(作废);
无效删除(void);
结构函数选项项
{
常量字符*选项文本;
函数\指针p\函数;
};
函数选项输入选项表[]=
{
{“一”,创建},
{“2”,登录},
{“三”,删除},
};
常量无符号整数选项表大小=
sizeof(选项表)/sizeof(选项表[0]);
//...
std::字符串选项\u文本;
//...
对于(i=0;i
使用一个开关和一个简单的哈希函数。
您需要使用散列函数,因为C和C++只允许转换积分值。
template<size_t N> constexpr char myhash(const char &x[N]) { return x[0] ^ (x[1]+63); }
char myhash(const string& x) { return x.size() ? x[0] ^ (x[1]+63) : 0; }
switch(myhash(s)) {
case myhash("one"):
if(s != "one") goto nomatch;
// do things
break;
case myhash("two"):
if(s != "two") goto nomatch;
// do things
break;
default:
nomatch:
// No match
}
template constepr char myhash(const char&x[N]){return x[0]^(x[1]+63);}
char myhash(const string&x){return x.size()?x[0]^(x[1]+63):0;}
开关(myhash){
案例myhash(“一”):
如果(s!=“一”)转到nomatch;
//做事
打破
案例myhash(“两个”):
如果(s!=“2”)转到nomatch;
//做事
打破
违约:
nomatch:
//没有对手
}
如果您不使用std::string
请使用开关和简单的哈希函数,则需要进行轻微调整。
您需要使用散列函数,因为C和C++只允许转换积分值。
template<size_t N> constexpr char myhash(const char &x[N]) { return x[0] ^ (x[1]+63); }
char myhash(const string& x) { return x.size() ? x[0] ^ (x[1]+63) : 0; }
switch(myhash(s)) {
case myhash("one"):
if(s != "one") goto nomatch;
// do things
break;
case myhash("two"):
if(s != "two") goto nomatch;
// do things
break;
default:
nomatch:
// No match
}
template constepr char myhash(const char&x[N]){return x[0]^(x[1]+63);}
char myhash(const string&x){return x.size()?x[0]^(x[1]+63):0;}
开关(myhash){
案例myhash(“一”):
如果(s!=“一”)转到nomatch;
//做事
打破
案例myhash(“两个”):
如果(s!=“2”)转到nomatch;
//做事
打破
违约:
nomatch:
//没有对手
}
如果您不使用std::string
我建议您为每个特定字符串创建一个函数,则需要进行轻微调整。例如,如果收到字符串“create”,则调用函数doCreate(),