Why the switch statement cannot be applied on strings?

前端 未结 20 2807
离开以前
离开以前 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:31

    In c++ strings are not first class citizens. The string operations are done through standard library. I think, that is the reason. Also, C++ uses branch table optimization to optimize the switch case statements. Have a look at the link.

    http://en.wikipedia.org/wiki/Switch_statement

提交回复
热议问题