编译以下代码并得到错误type illegal.
int main()
{
// Compilation error - switch expression of type illegal
switch(std::string("raj"))
{
case"sda":
}
}
Run Code Online (Sandbox Code Playgroud)
你不能在任何一个switch或中使用字符串case.为什么?是否有任何解决方案可以很好地支持类似于切换字符串的逻辑?
我在几个不同的地方读过,使用C++ 11的新字符串文字,可以在编译时计算字符串的哈希值.然而,似乎没有人准备出来说它将是可能的或如何完成.
我特别感兴趣的是这样的用例.
void foo( const std::string& value )
{
switch( std::hash(value) )
{
case "one"_hash: one(); break;
case "two"_hash: two(); break;
/*many more cases*/
default: other(); break;
}
}
Run Code Online (Sandbox Code Playgroud)
注意:编译时哈希函数不必像我编写的那样完全.我尽力猜测最终解决方案的样子,但meta_hash<"string"_meta>::value也可能是一个可行的解决方案.
有没有投一个通用的方法int,以enum在C++?
如果int落在一个范围内enum它应该返回一个enum值,否则抛出一个exception.有没有办法一般地写它?不止一个enum type应予以支持.
背景:我有一个外部枚举类型,无法控制源代码.我想将此值存储在数据库中并检索它.
在C中有一个switch构造,它使一个人能够根据一个测试整数值执行不同的条件代码分支,例如,
int a;
/* Read the value of "a" from some source, e.g. user input */
switch ( a ) {
case 100:
// Code
break;
case 200:
// Code
break;
default:
// Code
break;
}
Run Code Online (Sandbox Code Playgroud)
如何为字符串值获得相同的行为(即避免所谓的" if- else阶梯"),即char *?
可能重复:
C/C++:切换非整数
嗨,我需要在开关盒中使用一个字符串.到目前为止,我的解决方案是用哈希函数计算字符串的哈希值.问题是我必须手动预先计算字符串的所有哈希值.有更好的方法吗?
h=_myhash (mystring);
switch (h)
{
case 66452:
.......
case 1342537:
........
}
Run Code Online (Sandbox Code Playgroud) 我们每个人(可能)都有童年的写作梦想:
switch(my_std_string) {
case "foo": do_stuff(); break;
case "bar": do_other_stuff(); break;
default: just_give_up();
}
Run Code Online (Sandbox Code Playgroud)
但这是不可能的,正如古代(2009年)对这个问题的答案所解释的那样:
从那时起,我们已经看到了C++ 11的出现,它让我们走得更远:
switch (my_hash::hash(my_std_string)) {
case "foo"_hash: do_stuff(); break;
case "bar"_hash: do_other_stuff(); break;
default: just_give_up();
}
Run Code Online (Sandbox Code Playgroud)
如在描述答案来编译时间字符串哈希 -这是没有那么糟糕,但它实际上并没有做正是我们想要的-有碰撞的机会.
我的问题是:从那时起语言的发展(我认为主要是C++ 14)是否影响了编写一个字符串case语句的方式?或简化实现上述目的的螺母和螺栓?
具体而言,与结束C++ 17标准之中指日可待 -我很感兴趣,鉴于我们可以假设标准将包含答案.
注意:这不是关于使用switch语句的优点的讨论,也不是关于meta的线程的讨论.我问的是一个内容丰富的问题,所以请在此基础上回答/ up/downvote.
可能重复:
在C中打开字符串的最佳方法
字符串(c字符数组)和switch语句一起使用的一般方法是什么?我正在查询数据库中存储为的货币
"USD"
"EUR"
"GBP"
Run Code Online (Sandbox Code Playgroud)
等等。来自PHP背景,我只需执行以下操作:
switch ($string) {
case "USD":
return "$";
break;
case "EUR":
return "€";
break;
case "GBP":
return "£";
break;
default:
return "$";
}
Run Code Online (Sandbox Code Playgroud)
在C中,case值必须是整数。我将如何在C中实现类似的功能?我最终会在一个很大的if / else块中写很多strcmp吗?另请注意,我不能简单地比较货币的第一个字符,因为某些字符(尽管在本示例中不是)以相同的字符开头。
在C++中switch使用a 有什么替代方法wstring吗?在Java中没有问题 - switch接受String.
如何使用switch-case比较c ++中的char数组?这是我的代码的一部分:
char[256] buff;
switch(buff){
case "Name": printf("%s",buff);
break;
case "Number": printf("Number "%s",buff);
break;
defaul : break
}
Run Code Online (Sandbox Code Playgroud)
我收到错误:"错误:切换数量不是整数".如何解决?
这里发生了什么?
#include <stdio.h>
int main (void)
{
int x = 'HELL';
printf("%d\n", x);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
打印 1212501068
我期待编译错误.
欢迎解释=)
void main()
{
char day[20];
printf("Enter the short name of day");
scanf("%s", day);
switch(day)
{
case "sun":
printf("sunday");
break;
case "mon":
printf("monday");
break;
case "Tue":
printf("Tuesday");
break;
case "wed":
printf("wednesday");
break;
case "Thu":
printf("Thursday");
break;
case "Fri":
printf("friday");
break;
case "sat":
printf("saturday");
break;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的代码.我在开关盒part.switch的情况下得到一个错误,没有检查这些情况.请帮助我.提前致谢.
我的代码在我想运行操作结果的地方没有按预期工作有什么问题?运行结果告诉我“编译器错误”,但我找不到错误
int main(void) {
int operation,num1,num2;
printf("please enter operation (sum,sub,mul,div) : ");
scanf("%d",& operation);
printf("please enter two number: ");
scanf("%d%d",& num1,& num2);
switch (operation)
?{
case sum :
printf("The summation is : %d", num1+num2);
break;
case sub :
printf("The submition is : %d", num1-num2);
break;
case mul :
printf("The multiplication n is : %d", num1*num2);
break;
case div :
printf("The division is : %d", num1/num2);
break;
default:
printf("Not valid");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)