Why the switch statement cannot be applied on strings?

前端 未结 20 2569
离开以前
离开以前 2020-11-22 03:58

Compiling the following code and got the error of type illegal.

int main()
{
    // Compilation error - switch expression of type illegal
    sw         


        
20条回答
  •  醉话见心
    2020-11-22 04:20

    Switches only work with integral types (int, char, bool, etc.). Why not use a map to pair a string with a number and then use that number with the switch?

提交回复
热议问题