且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C ++如何使用较少的条件语句?

更新时间:2023-01-27 15:36:21

您可以使用地图为您进行比较。

You can use a map which does the comparison for you.

类似这样的东西:

初始化地图:

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
}