草庐IT

vector_c

全部标签

C++ 使用指向非 const 的指针编写 const vector

我正在编写一个CustomVector类,使用标准vector在内部存储数据:templateclassCustomVector{friendclassCustomVector_ref;public:...private:std::vector_data;};然后,为了从CustomVector中提取子vector,我使用了一个类来存储指向每个数据元素的指针:templateclassCustomVector_ref{public://ReturnsthevaluestoredinCustomVector//andpointed-toby_data_refT&operator[](si

c++ - 通过 const_cast 修改 const std::vector<T> 的元素

下面的程序是否有未定义的行为?#include#includestructFoo{conststd::vectorx;};intmain(){std::vectorv={1,2,3};autof=newFoo{v};const_cast(f->x[1])=42;//Legal?std::coutx[1]请注意,它不是使用const_cast从f->x中剥离常量,而是从f->中剥离常量x[x],大概由一个单独的数组表示。或者是否允许翻译假定f->x[1]在创建后是不可变的? 最佳答案 您的示例中没有未定义的行为。上面的代码不会调用未定

c++ - 拥有指向保留 vector 元素的指针是否合法?

这个问题在这里已经有了答案:Usestd::vector::dataafterreserve(4个答案)关闭去年。我很好奇这种事情是否合法:std::vectorvec;vec.reserve(10);some_class_type*ptr=vec.data()+3;//thatobjectdoesn'texistyet请注意,我并不是要访问指向的值。这是标准关于data()的说法,但我不确定它是否相关:Returns:Apointersuchthat[data(),data()+size())isavalidrange.Foranon-emptyvector,data()==&fro

C++ std::vector initializer_list 重载歧义 (g++/clang++)

考虑以下代码:#include#defineBROKENclassVar{public:#ifdefBROKENtemplateVar(Tx):value(x){}#elseVar(intx):value(x){}#endifintvalue;};classClass{public:Class(std::vectorarg):v{arg}{}std::vectorv;};无论BROKEN是否被定义,Clang++(7.0.1)编译它没有错误,但是如果BROKEN被定义,g++(8.2.1)会引发错误:main.cpp:9:20:error:cannotconvert‘std::vect

c++ - std::vector 比 std::unordered_set 更快?

在我的自定义物理引擎中,最大的瓶颈是从空间分区(二维网格)获取所有物体并返回仅包含指向物体的唯一指针的集合的方法。templateboolcontains(constT&mContainer,constV&mValue){returnstd::find(std::begin(mContainer),std::end(mContainer),mValue)!=std::end(mContainer);}constvector&GridInfo::getBodiesToCheck(){bodiesToCheck.clear();for(auto&query:queries)for(auto

c++ - vector::insert 的异常安全保证是什么?

我想知道,std::vector::insert的异常安全保证究竟是什么??我对这个函数的单参数和范围重载都感兴趣。 最佳答案 在C++1123.3.6.5中给出了确切的保证:Ifanexceptionisthrownotherthanbythecopyconstructor,moveconstructor,assignmentoperator,ormoveassignmentoperatorofTorbyanyInputIteratoroperationtherearenoeffects.Ifanexceptionisthrown

c++ - 清除 vector 或定义新 vector ,哪个更快

哪种方法更快且开销更少?方法一:voidfoo(){std::vectoraVector;for(inti=0;i方法二:voidfoo(){for(inti=0;iaVector;aVector.push_back(i);}}你可能会说这个例子毫无意义!但这只是我的大代码中的一个片段。总之我想知道是不是更好“创建一个vector并清除它以供使用”或“每次都创建一个新vector”更新谢谢你的建议,我都测试了,这是结果方法一:$time./test1real0m0.044suser0m0.042ssys0m0.002s方法二:$time./test2real0m0.601suser0m

c++ - SIMD/SSE : How to check that all vector elements are non-zero

我需要检查所有vector元素是否非零。到目前为止,我找到了以下解决方案。有一个更好的方法吗?我在Linux/x86_64上使用gcc4.8.2,指令高达SSE4.2。typedefcharChrVect__attribute__((vector_size(16),aligned(16)));inlinebooltestNonzero(ChrVectvect){constChrVectvzero={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};return(0==(__int128_t)(vzero==vect));}更新:上面的代码被编译为以下汇编代码(当编译为非

c++ - std::vector 的第二个参数

查看vector,我意识到我在创建vector时从未使用过第二个参数。std::vectormyInts;//thisiswhatIusuallydostd::vectormyOtherInts;//butisthereasecondargumentthere?查看上面的链接,它说它用于:Allocatorobjecttobeusedinsteadofconstructinganewone.或者,至于thisone:Allocator:Typeoftheallocatorobjectusedtodefinethestorageallocationmodel.Bydefault,thea

php - 如何使用 SWIG 在 PHP 中使用 std::vector

我正在使用SWIG在PHP中包装C++API。我大部分时间都在那里,但我遇到了返回vector的函数的问题。header看起来像这样:#includenamespaceSTF{classMyClass{public:conststd::vector&getList();};}接口(interface)文件如下所示:%include%import"STF_MyOtherClass.i"%{#include"STF_MyOtherClass.h"#include"STF_MyClass.h"%}%include"STF_MyClass.h"我似乎能够很好地调用该函数,但它返回的是PHP资源