C++实用程序,用于将长开关语句转换为封装开关案例阶梯的简洁函数调用
C++ Utility to convert long switch statement into the concise function call which encapsulates the switch case ladder
我想编写一个可以执行以下操作的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;
}
相关文章:
- C 和 C++ 中开关语句的案例标签的常量值,但显示不同的行为
- 正在读取开关案例中的.txt文件
- C++实用程序,用于将长开关语句转换为封装开关案例阶梯的简洁函数调用
- 执行默认值:在C 中的开关案例选择语句中
- 在开关案例节点中,将const int int指向int
- 使用开关语句案例中的随机数
- 案例和开关问题
- 如何在 c++ 中使用开关中的案例条件作为变量
- 如果使用较小的数字,则开关案例更快
- 我可以在c 中的语句中使用开关案例的情况吗?
- 没有默认标签的开关案例中的无效值
- 在开关案例内将字符串化
- 如何从案例 4 调用开关案例 5
- 状态机:使用转换矩阵而不是嵌套的开关案例C++
- 在 c++ 开关案例中引入循环
- C++、开关案例和编译器
- 新设置开关案例arduino
- 在开关案例语句中,它说"duplicate case value"出现错误。有人知道为什么吗?
- C++中的动态开关案例
- 抽象/重构此开关/案例的好方法是什么?