草庐IT

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

C++ 为成员容器中的项调用for_each中的成员函数

如果我有这样一个类(模仿一些STL的容器):classElem{public:voidprepare();//dosomethingon*this//...};classSelector{public:typedefvectorcontainer_type;typedefcontainer_type::iteratoriterator;iteratorbegin(){returncont_.begin();}iteratorend(){returncont_.end();}voidcheck_all();private:prepare_elem(Elem*p);//dosomethin

在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++ - boost::fusion::for_each 中的函数对象不同于 std::for_each

在升级到更新的编译器并解决编译器错误时,我意识到boost::fusion::for_each要求传入的函数对象具有运算符const。示例来自Boost:structincrement{templatevoidoperator()(T&t)const{++t;}};...vectorvec(1,2);for_each(vec,increment());这当然没有改变。我没有意识到它与std::for_each不同,它不要求运算符是const。structincrement{templatevoidoperator()(T&t)//noconsthere!!!{++t;}};std::v

C++: boost::fusion::for_each 对于许多序列

目前,boost::fusion::for_each迭代单个序列的元素。我正在尝试创建一个函数,该函数将以类似的方式工作,但具有许多序列,并将遍历序列之间的所有可能组合。例如,如果我有三个序列S1、S2、S3,我想创建一个这样的仿函数structmy_functor{templatevoidoperator()(x&el1,y&el2,z&el3){...}}然后调用for_each(s1,s2,s3,my_functor())//appliesthefunctortoallcombinationsofelementsofs1,s2,s3其中s1、s2、s3是S1、S2、S3的实例。我

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++ - 如何在 C++ 中实现类似 for_each 的函数?

我需要实现一个for_each函数,如下所示。我知道std::for_each可以将fn应用于每个元素,但我们不能删除std::for_each中的元素。我需要扩展这个模板函数,以便在fn中,调用者可以同时访问元素和一次删除一个元素。有没有正确的方法来做到这一点?#include#include#includeusingnamespacestd;classA{public:explicitA(){mVec.clear();}~A(){}templatevoidfor_each(Tfn){for(size_ti=0;imVec;};intmain(){Atest;for(inti=0;i

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];