草庐IT

c++ - 没有匹配函数调用 ‘find'

我有以下代码:#include#include#include#includeusingnamespacestd;intmain(){typedefvectorIntContainer;typedefIntContainer::iteratorIntIterator;IntContainervw;IntIteratori=find(vw.begin(),vw.end(),5);if(i!=vw.end()){printf("Find5invector\n");//foundit}else{printf("Couldn'tfind5invector\n");//couldn'tfound

c++ -/usr/bin/ld : cannot find -lGL

我已经安装了qt,当我在qt上运行应用程序时出现此错误。/usr/bin/ld:cannotfind-lGLcollect2:error:ldreturned1exitstatusmake:***[test3]Error114:05:48:Theprocess"/usr/bin/make"exitedwithcode2.我认为这个错误与OpenGL有关。我已经运行了所有这些命令,但没有任何反应sudoapt-getinstalllibgl1-mesa-devsudoapt-getinstalllibgl1-mesa-glxlibgl1-mesa-dev我的pro文件是:#-------

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++ - 来自 std::set insert() 和 find() 的写-写数据竞争?

为了试验线程清理器,我创建了一个微型C++程序,它有意包含一个数据竞争。确实,tsan确实检测到错误,太棒了!但是我对生成的消息感到困惑......它报告了一个写-写数据竞争,而我原以为是一个读-写竞争。我希望find()不会写入我的容器。如果我做进一步的小代码调整,试图获得set::find()的const版本,似乎仍然存在相同的写-写竞争。它显示了在同一地址的4字节原子写入和8字节写入之间的写入冲突。容器类中的同一个字段被两种不同的访问类型访问,这似乎很奇怪。是否可以选择使用不写入STL容器的constfind()?这是经过测试的C++程序:/*******************

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();}恕我直言,这是最好的