草庐IT

Layer-backed

全部标签

c++ - 模拟条件 back_inserter 之类的最佳方法?

我想用以下代码中的算法替换循环intnumbers[]={...};vectoroutput;for(int*it=numbers+from;it!=numbers+to;++it){intsquare=func(*it);if(predicate(square)){output.push_back(square);}}该程序旨在转换值并在条件发生时将它们复制到目的地。我无法使用std::copy_if,因为那样不会应用转换。我无法使用std::transform因为它缺少谓词因为转换变量的中间拷贝,编写transform_copy_if()甚至不是一个好主意。看来我唯一的希望是创建一

c++ - 指向 deque<int>::push_back 的指针

#include#includeusingnamespacestd;main(){typedefvoid(deque::*func_ptr)(int);func_ptrfptr=&deque::push_back;}我试图获取指向该函数的指针,但出现编译错误error:cannotconvert‘void(std::deque::*)(constvalue_type&){akavoid(std::deque::*)(constint&)}’to‘func_ptr{akavoid(std::deque::*)(int)}’ininitializationfunc_ptrfptr=&deq

c++ - push_back() "new"一个对象在添加到 c++ 中的 std::list 之前

我是C++标准库的新手。我想使用std::list。我知道如果我自己创建一个列表而不是使用STL,我应该为一个新对象分配内存,然后将它添加到列表中。A类的C风格列表:A*ptrA=newA();ptrA->setElement(value);ptrA->next=null;currentPositionMyCstyleList->next=ptrA;ptrA->prev=currentPositionMyCstyleList;如果我使用STL,是否有必要“新建”一个对象?push_back()在添加到c++中的std::list之前是否“新建”了一个对象?下面的代码是否正确?AaObj

c++ - 为什么 list::push_back 在 VC++ 中比在 g++ 中慢得多?

此代码在我的VS2012中大约需要20秒,但在G++中仅需1.x秒。均在win8x64中并使用默认选项编译。listitems;for(inti=0;i是关于内存分配的吗?在我的机器上用VC++输出后释放内存需要3~5秒,而在我friend的(win7x64)上甚至超过1分钟。 最佳答案 嗯...我扩展了您的代码以包含计时:#include#include#include#includeintmain(){std::listitems;clock_tstart=clock();for(inti=0;i我用VC++编译使用:cl/O2

c++ - Back_inserter 或 push_back

只是一个简单的问题-将字符串添加到vector的末尾哪个更好?,back_inserter或push_back?主要是,哪个工作得更快(我正在处理大量数据,所以边际差异实际上很重要),主要差异是什么? 最佳答案 两者并不等价。您使用std::back_inserter例如,当您需要将输入迭代器传递给算法时。std::vector::push_back在这种情况下不是一个选择。例如std::vectora(100,"Hello,World");std::vectorb;std::copy(a.begin(),a.end(),std::

c++ - 从 back_insert_iterator 中提取容器的 value_type 的特征类

std::back_insert_iterator的value_type等于void,但它还有一个protected成员container包含指向底层Container的指针。我正在尝试编写一个traits类来提取容器的value_type,如下所示:#include#include#includetemplatestructoutit_vt:OutputIt{usingself_type=outit_vt;usingvalue_type=typenamestd::remove_pointer_t().container)>::value_type;};intmain(){std::v

C++ Vector,从另一个线程崩溃的 push_back?

我使用已检查的STL实现在我的代码中出现意外的断言失败。经过一些研究,我将问题缩小到一个vector中的push_back,该vector是从与创建该vector的线程不同的线程调用的。重现此问题的最简单代码是:classSomeClass{private:std::vectortheVector;public:SomeClass(){theVector.push_back(1);//Ok}voidadd(){theVector.push_back(1);//Crash}};唯一的区别是SomeClass是从我的主线程实例化的,而add是从另一个线程调用的。但是,没有并发问题:在我用于

c++ - vector 修改后引用 vector.back() 的奇怪行为

让我们从这个C++示例代码开始:#include#includeintmain(){std::vectorvec;vec.push_back(0);for(inti=1;i代码是用g++test.cc-std=c++11-O0编译的,下面是结果:Before:0,After:0Before:1,After:0Before:2,After:2Before:3,After:3我期待第二行输出是Before:1,After:1因为x是vector中一个项目的引用,不应通过将项目附加到vector来修改它。但是我目前还没有阅读反汇编代码或进行任何其他调查。我也不知道这是否是语言标准中未定义的行

c++ - push_back 到 std::vector,复制构造函数被重复调用

这个问题在这里已经有了答案:vectorpush_backcallingcopy_constructormorethanonce?(5个答案)关闭4年前。使用is代码,我得到以下输出:A::A()iscalledtest#1A::A(constA&other)iscalledtest#2A::A(constA&other)iscalledA::A(constA&other)iscalledtest#3A::A(constA&other)iscalledA::A(constA&other)iscalledA::A(constA&other)iscalled在调试代码时,对于3个测试用例,

c++ - 为什么 vector.push_back(auto_ptr) 无法编译?

我了解到STL可以禁止程序员将auto_ptr放入容器中。例如下面的代码不会编译:auto_ptra(newint(10));vector>v;v.push_back(a);auto_ptr有拷贝构造函数,为什么这段代码还能通过? 最佳答案 查看thedefinitionofstd::auto_ptr:namespacestd{templatestructauto_ptr_ref{};templateclassauto_ptr{public:typedefXelement_type;//20.4.5.1construct/copy/