为什么不能将开关语句应用于字符串?编译以下代码并得到type illegal.int main(){
// Compilation error - switch expression of type illegal
switch(std::string("raj"))
{
case"sda":
}}您不能在其中使用字符串。switch或case..为什么?是否有任何解决方案可以很好地支持类似于打开字符串的逻辑?
3 回答
森林海
TA贡献2011条经验 获得超2个赞
switchstd::string
enum string_code {
eFred,
eBarney,
eWilma,
eBetty,
...};string_code hashit (std::string const& inString) {
if (inString == "Fred") return eFred;
if (inString == "Barney") return eBarney;
...}void foo() {
switch (hashit(stringValue)) {
case eFred:
...
case eBarney:
...
}}- 3 回答
- 0 关注
- 573 浏览
添加回答
举报
0/150
提交
取消
