20191123-SWITCH后面跟的参数不能为string类型

  1. #include "stdafx.h" 

  2. #include "cstdio" 

  3. #include "iostream"

  4. #include "algorithm"

  5.  using namespace std;

  6.  int main()

  7.  { 

  8.      string grade="A"; 

  9.      if (grade=="A") 

  10.       cout<<"25"<<endl;

  11. }  

代码2:  

  1. #include "stdafx.h" 

  2. #include "cstdio" 

  3. #include "iostream"

  4. #include "algorithm"

  5.  using namespace std;

  6.  int main()

  7.  { 

  8.      string grade="A"; 

  9.      switch(grade)

  10. {

  11.       case "A": 

  12.       cout<<"25"<<endl; 

  13.         break;

  14.       default:break;

  15. }

  16. }

分析:

    代码1可以正常执行,但代码2不可以,因为switch后面括号里的参数不能为string类型,IF可以,int类型和CHAR类型IF和SWITCH都支持。

原文地址:https://www.cnblogs.com/whcsrj/p/12928244.html