草庐IT

EidosValue_Int_vector

全部标签

c++ - 复制指针 vector

我有一个std::vector我需要使用A::Clone()将其深度复制到另一个vector.我想知道我是否可以使用for_each而不是使用手写循环或任何标准库算法。 最佳答案 合适的算法是std::transform您可以使用std::mem_fun将成员函数调用转换为一元仿函数例子:#include#include#include#includeclassX{public:X*clone();};intmain(){std::vectorvec1,vec2;std::transform(vec1.begin(),vec1.en

接收 std::vector 作为参数的 C++ 模板函数

我需要制作一个模板函数,该函数接收某种类型的std::container作为参数-让我们说std::vector并从该容器中删除所有元素。我需要一个与此等效的函数:for_each(some_vector.begin(),some_vector.end(),[](some_vector_type*element){deleteelement;});调用应该是这样的:delete_all_elements(some_vector);这可能吗?编辑:我想在delete_all_elements中使用第一个代码 最佳答案 为什么不呢?te

c++ - 如何在 C++ 中定义 vector <boost::mutex>?

我想用boost::mutex定义一个vector,例如:boost::mutexmyMutex;std::vectormutexVec;mutexVec.push_back(myMutex);但是,我在Linux上遇到错误:/boost_1_45_0v/include/boost/thread/pthread/mutex.hpp:33:错误:“boost::mutex::mutex(constboost::mutex&)”是私有(private)的/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/

c++ - 指向 vector 的指针

我有这个代码:#include#include#includeusingnamespacestd;vector*vecptr;intveclen;voidgetinput(){stringtemp;for(inti=0;i>temp;vecptr->push_back(temp);}veclen=vecptr->size();}intmain(){getinput();for(inti=0;i我的编译器(G++)抛出一些错误:test2.cpp:28:17:error:nomatchfor'operator怎么了?我该怎么做才能修复它? 最佳答案

C++ int 到字符串的转换

当前源代码:stringitoa(inti){std::strings;std::stringstreamout;out主要是:Gregoriandate=newGregorian("June",5,1991);cout我收到这个错误:mayan.cc:Infunction‘intmain(int,char**)’:mayan.cc:109:51:error:conversionfrom‘Gregorian*’tonon-scalartype‘Gregorian’requested有谁知道为什么int到string的转换在这里失败了?我是C++的新手,但对Java很熟悉,我花了很多时间

c++ - 通过赋值运算符插入索引处的 std::vector

我是C++的新手,很好奇这是否是插入std::vector的首选方式std::vectormyVector;voidsetAt(intx,Object_I_madeupo){myVector[x]=o;}//setthearraylocationatxtobeo.我问是因为我看到很多关于使用push_back或高度困惑的insert()的事情。这种类似Java的方式有效吗?我宁愿这样做... 最佳答案 myVector[x]=o;只有当x时它才是明确定义的.否则,它会调用未定义的行为,因为在这种情况下,它会尝试访问vector边界之

c++ - 是否可以在 int 中存储前导零?

我有一个编程作业,我需要加密用户输入的4位整数。我已将int拆分为四个单独的值,并且加密和解密函数起作用。我的问题是当我将四个单独的整数放回一起时,一些数字加密为零(例如输入:1234输出:0189)并且我想将输出存储到一个整数中以供其他函数使用。现在我有一个半生不熟的解决方案,如果第一个int为0,则首先打印0。voidjoinInt(){if(int1==0){cout我的目标是返回连接(带前导零)而不是仅在函数内打印它。 最佳答案 这样做:#include#includestd::cout

c++ - Unique_ptr 的 STL vector - 如何重置?

我创建了两个标准的unique_ptrvector:std::vector>students;std::vector>teachers;然后,我创建一个新对象并将其放入vector中:students.push_back(std::unique_ptr(newStudent()));teachers.push_back(std::unique_ptr(newTeacher()));完成所有操作后,如何删除vector?Whitoutunique_ptr我不得不做一个循环并删除每个对象:while(!students.empty()){deletestudents.back();stud

c++ - 将int转换为字符串的有效方法

我正在创建一个游戏,其中有一个主循环。在此循环的一个周期内,我必须将int值转换为string大约50-100次。到目前为止,我一直在使用这个功能:std::stringUtil::intToString(intval){std::ostringstreams;s但它似乎不是很有效,因为我遇到FPS从~120(不使用此功能)下降到~95(使用它时)。有没有其他方法可以将int转换为string比我的函数更有效? 最佳答案 It's1-72range.Idon'thavetodealwithnegatives.预先创建一个包含73个s

c++ - C++ 中的 vector 和多态性

我有一个棘手的情况。它的简化形式是这样的classInstruction{public:virtualvoidexecute(){}};classAdd:publicInstruction{private:inta;intb;intc;public:Add(intx,inty,intz){a=x;b=y;c=z;}voidexecute(){a=b+c;}};然后在一节课上我做了类似...voidsome_method(){vectorv;Instruction*i=newAdd(1,2,3)v.push_back(*i);}在另一个类(class)...voidsome_other_