草庐IT

remove_these

全部标签

c++ - 使用 remove_if 按索引从 C++ vector 中删除

我们可以在C++中使用remove_if,根据对元素进行操作的谓词,在线性时间内从vector中删除元素。boolcondition(doubled){...}vectordata=...std::remove_if(data.begin(),data.end(),condition);如果我的条件不取决于值,而是取决于指数怎么办?换句话说,如果我想删除所有奇数索引元素,或一些任意索引集等?boolcondition(intindex){//returnswhetherthisindexshouldberemoved}vectordata=...std::remove_if(data.

c++ - 使用 std::remove 删除指针元素

我正在努力remove(pvec.begin(),pvec.end(),NULL);删除vector中的NULL指针(其中pvec是vector)但是,编译器给出:“ISOC++禁止比较指针和整数”如何解决?谢谢! 最佳答案 在C++11中,使用nullptr:remove(pvec.begin(),pvec.end(),nullptr);//^^^^^^^否则,执行对指针值的显式转换。如果你的pvec包含类型为foo*的指针,写:remove(pvec.begin(),pvec.end(),static_cast(NULL));/

c++ - 为什么 Vector 不提供 remove() 成员函数而 list 提供?

如果我想从vector中删除所有具有值的元素,我调用removealgorithm然后调用vector的erase成员函数来物理删除它。但在list的情况下,简单调用remove成员函数,它将删除具有该值的所有元素。我不确定为什么vector不提供removeMF而list提供。对于Exp:我想从vectorv中删除值“4”。vectorv;vector::iteratorItr;for(inti=0;i对于列表:list.remove(4);//willdeletealltheelementwhichhasvalue4 最佳答案

C++11/14 : How to remove a pointer-to-member from a type?

在C++11或C++1y/14中:给定一个TC::*形式的指向成员类型的值,我想得到指向类型例如:#includeusingnamespacestd;structT1{staticvoidf(){coutstructV;templatestructV{staticconstexprT1U1::*pm=&U1::x;};templatestructV{staticconstexprT2U2::*pm=&U2::x;};templatevoidf(Wpm){typedef???T;T::f();}intmain(){f(V::pm);f(V::pm);}有没有办法做到这一点?上面的???是

c++ - std::remove_if 来自 std::vector 的多态 std::unique_ptr

我有一个包含三个类的层次结构,其中Derived源自Selectable和Drawable.然后我有一个std::vector的std::unique_ptr我用Derived填充对象。我确定该vector将仅由同时从两个基派生的对象填充。当我尝试使用指向Selected的指针从vector中删除某个元素时,问题就来了.#include#include#includestructSelectable{virtual~Selectable()=0;};Selectable::~Selectable()=default;structDrawable{virtual~Drawable()=0

c++ - 如何修复 "<hash_map> is deprecated and will be REMOVED. Please use <unordered_map>"?

我正在学习C++,所以我正在阅读Programming:PrinciplesandPracticeusingC++这本书。我正在进行第一个练习,即如何制作“Hello,World!”。使用MicrosoftVisualStudio2015编写的程序。我使用了书中提供的源代码。#include"../../std_lib_facilities.h"//headerfilerecommendedbybookintmain()//C++programsstartbyexecutingthefunctionmain{cout但是,当我尝试构建一个可执行程序时收到两个错误,如下所示:Intell

c++ - STL "erase-remove"习语 : Why not "resize-remove"?

众所周知,从std::vector中完全删除所需项的一种好方法是erase-removeidiom.如以上链接中所述(截至本文发布日期),在代码中,erase-remove习惯用法如下所示:intmain(){//initialisesavectorthatholdsthenumbersfrom0-9.std::vectorv={0,1,2,3,4,5,6,7,8,9};//erase-removeidiomtocompletelyeliminatethedesireditemsfromthevectorv.erase(std::remove(std::begin(v),std::en

c++ - remove_if 的惯用 C++

我有这门课classPoint2D{public:boolisValid();//...private:doublex_,y_;};我有一个std::vector我想删除无效点,现在我这样做:boolinvalid(constPoint2D&p){return!p.isValid();}voidf(){std::vectorpoints;//fillpointspoints.erase(std::remove_if(points.begin(),points.end(),invalid),points.end());//usevalidpoints}是否有一种标准的方法(漂亮地)做到这

C++ 虚函数 : Can the linker remove entries in the virtual function table which aren't called?

这个问题是对eliminateunusedvirtualfunctions的一种跟进,这对我的兴趣来说还不够深入。问题:在定义具有虚函数的类时,编译器为虚函数表分配存储空间,并在表中存储指向函数的指针。这会导致链接器保留这些函数的代码,而不管它们是否被调用过。这可能会导致大量死代码保留在可执行文件中,即使编译器优化设置要求消除死代码也是如此。现在,如果在可执行文件中没有任何地方有特定虚函数的调用(或者换句话说,访问虚函数表的相应槽),则可以从虚函数中省略相应的函数指针表,链接器将删除该函数的代码,并可能进一步省略其他未引用的代码。显然,这不能由编译器完成,因为只有在链接时才会清楚是否调

c++ - "there is no smaller array object that satisfies these constraints"是什么意思?

draftn4659forC++17在第4章中描述了该语言的一般原理。在第4.5章中,C++对象模型[intro.object],我无法理解一句话的意思(强调我的意思)3Ifacompleteobjectiscreated(8.3.4)instorageassociatedwithanotherobjecteoftype“arrayofNunsignedchar”oroftype“arrayofNstd::byte”(21.2.1),thatarrayprovidesstorageforthecreatedobjectif:(3.1)—thelifetimeofehasbegunand