草庐IT

if-cases

全部标签

C++, 'if' 表达式中的变量声明

这是怎么回事?if(inta=Func1()){//Works.}if((inta=Func1())){//Failstocompile.}if((inta=Func1())&&(intb=Func2()))){//Dostuffwithaandb.//ThisiswhatI'dreallyliketobeabletodo.}2003标准中的第6.4.3节解释了在选择语句条件中声明的变量如何具有扩展到条件控制的子语句末尾的范围。但我看不出它在哪里说了不能在声明两边加上括号,也没有说每个条件只能有一个声明。即使在条件中只需要一个声明的情况下,这种限制也很烦人。考虑一下。boola=fal

c++ - "warning: operation of ... may be undefined"用于三元运算——不是 if/else block

这个问题在这里已经有了答案:Undefinedbehaviorandsequencepoints(5个答案)关闭6年前。这是我的代码:intmain(){staticinttest=0;constintanotherInt=1;test=anotherInt>test?test++:0;if(anotherInt>test)test++;elsetest=0;return0;}这是我构建它时产生的警告:../main.cpp:15:40:warning:operationon‘test’maybeundefined[-Wsequence-point]test=anotherInt>te

c++ - constexpr if 和 static_assert

P0292R1constexprif一直included,在C++17的轨道上。它似乎很有用(并且可以替代SFINAE的使用),但是关于static_assert的评论是错误的,不需要诊断在false分支中吓到我了:Disarmingstatic_assertdeclarationsinthenon-takenbranchofaconstexprifisnotproposed.voidf(){ifconstexpr(false)static_assert(false);//ill-formed}templatevoidg(){ifconstexpr(false)static_asser

c++ - std::remove_if 和 erase 不从 std::vector 中移除元素

我正在练习leetcodeeasy问题。我想使用lambda从vector中删除_if(这是第一次,太棒了)。我得到一个指向new_end的负指针。#include#include#include#include//std::greaterusingnamespacestd;intmain(){vectora={2,7,11,15};inttarget=9;autonew_end=std::remove_if(a.begin(),a.end(),[&a,target](constintx){returnstd::count(a.begin(),a.end(),x)>target;});

c++ - C++17 中的 "If constexpr"在非模板函数中不起作用

我尝试使用C++17标准。我尝试使用C++17ifconstexpr的功能之一。我有一个问题......请看下面的代码。这编译没有错误。在下面的代码中,我尝试使用ifconstexpr来检查它是否是一个指针。#include#includetemplatevoidprint(Tvalue){ifconstexpr(std::is_pointer_v)std::cout但是当我重写上面的代码时,如下所示,其中ifconstexpr在main函数中:#include#includeintmain(){autovalue=100;ifconstexpr(std::is_pointer_v)s

在if语句中使用字符串

我需要一种方法让用户输入在if陈述:print("Hello,World!")name=input("Whatisyourname?")hobby=input("Cool,so"+name+"whatdoyoudoyouforfun?Youcansaysomethinglikeplay,work,learn,etc.")play='play'work='work'learn='learn'ifhobby=playprint('awesome')elifhobby=workprint('mustbebusy')elifhobby=learnprint('ha,metoo')看答案这if,elif

c++ - 对于 CLang 中的 enable_if 错误(错误 11723)是否有更好的解决方法?

理想情况下,我们可以使用enable_if做类似的事情:#includenamespacedetail{enumclassenabler_t{DUMMY};}templateusingenable_if_u=typenamestd::enable_if::type;templateusingdisable_if_u=typenamestd::enable_if::type;template::value>...>inta(){return0;}template::value>...>doublea(){return0.0;}intmain(){autox=a();}恕我直言,这是最好的

c++ - 简单 if 是否会降低性能?

比如我有一个类classPoint{public:floatoperator[](inti)const{if(i==0)returnm_x;//simpleifs,performancereduction??if(i==1)returnm_y;returnm_z;}private:floatm_x;floatm_y;floatm_z;};与访问std::array的元素相比,性能是否有任何降低??如果是这样,我该如何删除它。我想使用数组以外的字段x、y、z。 最佳答案 Isthereanyperformancereduction?我

c++ - if 语句不起作用并被跳到 else 部分

//thisismysourcefile,.cpp#include#include#include"kingdom.h"namespacewesteros{voiddisplay(KingdompKingdom[],intkingdomElement,stringKingdomName){cout#include"kingdom.h"#includeusingnamespacestd;usingnamespacewesteros;intmain(void){intcount=0;Kingdom*pKingdoms=nullptr;pKingdoms=newKingdom[count];

C++:一种在分隔变量定义和测试的 if 语句中声明一个变量(或多个变量)的方法?

可以这样做:caseWM_COMMAND:if(WORDwNotifyCode=HIWORD(wparam)){...}可以这样做:caseWM_COMMAND:{WORDwNotifyCode=HIWORD(wparam);if(wNotifyCode>1){...}}但是不能这样做:caseWM_COMMAND:if((WORDwNotifyCode=HIWORD(wparam))>1){...}我认为在这里使用for语句是误导性的:caseWM_COMMAND:for(WORDwNotifyCode=HIWORD(wparam);wNotifyCode>1;wNotifyCode