C++实用程序,用于将长开关语句转换为封装开关案例阶梯的简洁函数调用

C++ Utility to convert long switch statement into the concise function call which encapsulates the switch case ladder

本文关键字:开关 案例 封装 函数调用 简洁 转换 实用程序 用于 语句 C++      更新时间:2023-10-16

我想编写一个可以执行以下操作的C++实用程序:

当前工作流程

int num;
std::string str;
switch(num){
case 2:
str = "two";
break;
case 5:
str = "five";
break;
case 7:
str = "seven";
break;
}

我想实现这一点:

//key is always guaranteed to be int but value can be anything
DECLARE_SWITCH( {
{2 , "two"},
{5 , "five"},
{7 , "seven"}
})
int num = 5;
std::string str;
match(num,str,ARR);
//here str == "five"

我想编写DECLARE_SWITCH和匹配函数。对语言结构没有限制 - 预处理器宏,模板任何东西都可以做。但是,如果有一些简单的解决方案或技巧,那就太好了。我知道关联数据结构,但我不想使用任何数据结构。这个问题是专门关于使用开关大小写的。

使用地图,您甚至可以使代码看起来与原始示例非常相似

#include <map>
#include <string>
#include <iostream>
int main()
{
std::map<int,std::string> DECLARE_SWITCH {
{2 , "two"},
{5 , "five"},
{7 , "seven"}
};
int num = 5;
std::string str = DECLARE_SWITCH[num];
std::cout << str << 'n';
return 0;
}

请注意,如果运算符 [] 不存在,它将在映射中插入新条目。 为避免此类行为,您必须使用 find

#include <map>
#include <string>
#include <iostream>
std::string match(int number, const std::map<int,std::string>& DECLARE_SWITCH )
{
auto q = DECLARE_SWITCH.find(number);
if (q==DECLARE_SWITCH.end()) {
return "";
}
else
return q->second;
}
int main()
{
const std::map<int,std::string> DECLARE_SWITCH {
{2 , "two"},
{5 , "five"},
{7 , "seven"}
}; //now we can declare map as const
int num = 5;
std::string str = match(num,DECLARE_SWITCH);
std::cout << str << 'n';
return 0;
}

std::map/std::unordered_map非常适合此用例。

映射是关联容器,用于存储由值和映射值的组合形成的元素,遵循特定顺序。

在您的情况下,您需要int和映射值std::string

这是一个例子

std::map<int, std::string> t;
t[1] = "one";
t[2] = "two";
t[3] = "three";
std::cout << "Value for '2': " << t[2] << std::endl;
for (auto& it : t)
{
std::cout << it.first << ": " << it.second << std::endl;
}
for (auto& it : t)
{
if (it.second == "one")
{
std::cout << "Value mapped to 'one' is: " << it.first << std::endl;
}
}

输出

Value for '2': two
1: one
2: two
3: three
Value mapped to 'one' is: 1

在您的情况下

std::map<int, std::string> DECLARE_SWITCH
{
{2 , "two"},
{5 , "five"},
{7 , "seven"}
};
int num = 2;
std::string str = DECLARE_SWITCH[num];
std::cout << str << 'n';

嗯,你在寻找X宏

警告概念代码不是测试代码

#define LIST_OF_VARIABLES 
X(2 , "two") 
X(5 , "five") 
X(7 , "seven")

template<class RetValue>
RetValue match(int key) {
#define SWITCH(CASE, VALUE) case CASE : return VALUE; 
#define X(PAIR) SWITCH(PAIR)
// makes the actual switch case
switch(key) {
LIST_OF_VARIABLES 
default: throw "fail";
}
#undef SWITCH
#undef X
}    
int main()
int num = 5;
std::string str;
str = match<std::string>(num);
std::cout << str;
}