草庐IT

Switch-case

全部标签

c++ - Switch 语句 C++ 中的字符

求助!我无法生成我的程序的输出。这是条件:构造一个程序,如果购买的衬衫是XL并且价格大于500,则提供100比索的折扣;购买L号衬衫,价格大于600可享50比索优惠。#includeusingnamespacestd;intmain(){intp;ints;cout>p;cout>s;switch(s){case'XL':case'xl':{if(p>500){cout600){cout程序的输出:Inputprice:500Inputsize:XLProcessreturned0(0x0)executiontime:5.750sPressanykeytocontinue.附言如何删除

c++ - if-cases 泄漏中定义的变量? (也就是为什么这甚至可以编译?)

似乎在if语句中声明的变量会泄漏到以下语句?我复制粘贴了一些代码,令我惊讶的是它在不应该编译的时候编译了!我正要提交代码,幸运的是我在那之前发现了错误。下面是一个显示问题的工作(?)程序。以下代码片段调用一个不存在的对象上的函数:#includeclassA{public:virtual~A(){}};classB:publicA{public:voidfooB(){std::cout(a)){b->fooB();}elseif(C*c=dynamic_cast(a)){c->fooC();b->fooB();}return0;}它编译并且输出是这样的:fooCfooB这肯定是错误的。

c++ - 在 switch 语句中从 int 到 enum 类的隐式转换

enumclasspid{Alpha,Beta,Gamma};intmain(){intpropId=2;switch(propId){casepid::Alpha:casepid::Beta:casepid::Gamma:break;}}以上片段在msvc2012中编译良好(并且有效)但在clang-3.4和g++-4.8中失败。这些需要static_cast(propId)在switch子句中使用。顺便说一下,没有显式转换的简单赋值,例如pida=propId;在每个编译器中给出错误。谁做对了? 最佳答案 标准第4条,“标准转换

c++ - "Why switch statement cannot be applied on strings?"的答案是否仍然正确,即使使用 C++11/14?

我遇到了这个问题:Whyswitchstatementcannotbeappliedonstrings?并想知道答案是否:Thereasonwhyhastodowiththetypesystem.C/C++doesn'treallysupportstringsasatype.Itdoessupporttheideaofaconstantchararraybutitdoesn'treallyfullyunderstandthenotionofastring.仍然适用,即使在C++11/14中使用std:string。是否有多个elseif(...)的替代方案?

c++ - 在 switch 语句中使用类类型 : is it better than using typeid operator?

我在下面看到了有关C++标准$6.4.2中switch语句的内容。Switch语句可以带一个条件。Theconditionshallbeofintegraltype,enumerationtype,orofaclasstypeforwhichasingleconversionfunctiontointegralorenumerationtypeexists(12.3).Iftheconditionisofclasstype,theconditionisconvertedbycallingthatconversionfunction,andtheresultoftheconversion

c++ - "virtual base class in the case of multilevel inheritance"有意义吗

考虑以下显示多级继承的示例代码:案例1:这里类derived1是通过虚拟继承从类base派生的,类derived2是从类派生的直接类derived1。classbase{};classderived1:virtualpublicbase{};classderived2:publicderived1{};Case2:与Case1相同,只是不涉及虚拟继承classbase{};classderived1:publicbase//novirtualinheritance{};classderived2:publicderived1{};假设我在这两种情况下都创建了derived2类的对象。C

c++ - 哪些提升类型用于 switch-case 表达式比较?

以下程序在使用不同的编译器编译时打印“unknown”。为什么会这样?#include"stdio.h"constcharOPTION=(char)(unsignedchar)253;intmain(intargc,char*argv[]){unsignedcharc=253;switch(c){caseOPTION:printf("option\n");break;default:printf("unknown\n");break;}return0;}在查看C++标准(N36902013-05-05)时,我看到了switch的子句:6.4.2Theswitchstatement2Th

c++ - 我可以在 C++ switch 语句中匹配范围而不是单个值吗?

我是编程新手。是否可以使用,>在开关盒中?例如,.........inti;cin>>i;......switch(i){case20 最佳答案 C++不提供用于匹配范围的switch语法。当范围相对较小时,您可以提供case标签,并依赖fall-through:switch(i){case20:case21:case22:case23:case24:case25:doSomething();break;case26:case27:case28:case29:doSomethingElse();break;...}对于中等大小的范​

c++ - "constexpr if"比 switch 语句好吗?

C++17引入了根据编译时条件实例化的“constexprif”。这是否意味着在模板函数中使用“constexprif”比使用switch语句更好?例如:templatevoidfunc(){ifconstexpr(val==0){}elseifconstexpr(val==1){}else...ifconstexpr(val==k){}else{}}//vstemplatevoidfunc(){switch(val){case0:break;case1:break;...casek:break;default:break;}} 最佳答案

php - 在 switch 语句中重复代码

我有这样的switch语句:switch(x){casea:executeSth();executeA();break;caseb:executeSth();executeB();break;...}所以executeSth();除了在默认情况下应该总是执行,但在它之后调用一些特定情况的代码(executeA();或executeB()等等)。(所以简单地把它放在开关前面是行不通的)。有没有一种有效的方法来减少“executeSth();”的数量?不牺牲性能?我只能想象将它分成两个开关(一个执行executeSth()和一个执行特定代码),但这会牺牲性能。也许您有更好的想法?我基本上对