【发布时间】:2014-05-04 23:31:39
【问题描述】:
大家好,
所以我正在尝试对字符串使用 Case 操作,Case 运算符是否仅适用于整数? 因为编译错误:
switch quantity not an integer
我以这段代码为例:
#include <iostream>
using namespace std;
int main(){
string type;
cout<<"Please enter your computer type (PC ,Laptop, Smartphone) : "<<endl;
getline(cin, type);
switch(type){
case "PC":
cout<<"Ok so you'vr got a "<<type;
break;
case "Laptop":
cout<<"Aha ,u got a "<<type;
break;
case "Smartphone":
cout<<"That s not bad ,u ve got :"<<type;
break;
}
return 0;
}
我知道我可以使用其他逻辑运算符,例如 IF ;但我正在研究 Case 运算符,所以我很好奇我是否可以使用字符串。
【问题讨论】:
-
否,但您可以打开字符串的哈希值,您可以在编译时计算该哈希值。 stackoverflow.com/a/19123540/179910