草庐IT

shared_int

全部标签

c++ - 为什么我不应该使用 vector<vector<vector<int>>>?

我刚刚阅读了一个关于初始化多维vector(question)的问题,ViktorSehr和Sbi推荐使用单个vector并使用my_vector[x+y*100+z*100*100]。为什么是这样?是出于性能原因吗?如果是这样,它如何提高性能?提前致谢,嗯。编辑:当宽度/高度/深度不同并且可以更改时,这些原因是否仍然适用? 最佳答案 只有几个原因:它浪费空间,它很慢(不可预测的内存访问,缓存浪费等),它很麻烦主要性能缺点可能是缓存。使用平面阵列,您可以保证内存是连续的——缓存很高兴。使用vector中的vector-谁知道呢!

c++ - 没有匹配函数调用 ‘begin(int**&)’

我写了一个c++程序作为fllow(3.43.cpp):#includeusingstd::cout;usingstd::endl;voidversion_1(int**arr){for(constint(&p)[4]:arr){for(intq:p){cout然后我用:gccmy.cpp-std=c++11编译它,有一个我无法处理的错误。信息:3.43.cpp:6:30:error:nomatchingfunctionforcallto‘begin(int**&)’for(constint(&p)[4]:arr){^3.43.cpp:6:30:note:candidatesare:I

c++ - 使用 shared_ptr 启动 std::thread

当你构造一个新线程时,提供的函数对象被复制到属于新创建线程的存储中。我想在一个新线程中执行一个对象方法。不应复制该对象。所以我将对象的shared_ptr传递给std::thread构造函数。如何使用std::shared_ptr()对象启动新线程?例如classFoo{public:voidoperator()(){//dosomething}};intmain(){std::shared_ptrfoo_ptr(newFoo);//Iwanttolaunchafoo_ptr()inanewthread//Isthisthecorrectway?std::threadmyThread(

c++ - Visual Studio 2010 中 std::make_shared() 的友元函数(不是 Boost)

如何创建std::make_shared()的友元函数。我试过:classMyClass{public:friendstd::shared_ptrstd::make_shared();//or//friendstd::shared_ptrstd::make_shared();protected:MyClass();};但它不起作用(我使用的是VisualStudio2010SP1) 最佳答案 如何向您的类添加一个静态方法:classFoo{public:staticshared_ptrcreate(){returnstd::shar

c++ - 为什么我会从这个 unsigned int 中得到段错误?

我正在尝试初始化一个整数数组并将所有元素设置为1。我需要该数组的上限为4294967295,或者32位unsignedint可能的最大数.这对我来说似乎是一项微不足道的任务,但我遇到了segfault。我可以空运行for循环,它似乎工作正常(虽然速度很慢,但它正在处理近43亿个数字,所以我不会提示)。当我尝试在循环中执行任何类型的操作时,问题似乎就出现了。我在下面的指令-primeArray[i]=1;-导致segfault错误。据我所知,这不应该导致我超出阵列。如果我注释掉该行,则不会出现segfault。已经很晚了,我疲倦的眼睛可能只是遗漏了一些简单的东西,但我可以再用一双。这是我

c++ - 特化 std::make_shared

我有一个具有严格对齐要求(由于使用了AVX操作)的类型,它大于平台默认对齐方式。为了简化此类的使用,我想专门化std::make_shared以始终为该类型使用合适的分配器。像这样:namespacestd{templateinlineautomake_shared(Args&&...args){returnstd::allocate_shared(allocator_type,std::forward(args)...);}}我的问题是,标准允许这样做吗?它会按预期工作吗? 最佳答案 来自N4140[namespace.std]/

c++ - size_t 是否总是 unsigned int

这个问题在这里已经有了答案:Differencebetweensize_tandunsignedint?(7个答案)关闭7年前。是否有任何实现将size_t定义为unsignedint以外的东西?在我工作的每个系统下,它都被定义为unsignedint,所以我很好奇。

c++ - uint32、int16、uint8 .. 为什么这些常用数据类型没有纳入标准

关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭5年前。Improvethisquestion多年来,在涉及C/C++的多个组织和各种项目中,我发现通过定义本地版本的types.h解决了对固定宽度整数的需求,它看起来像这个:-typedefsignedcharint8;typedefunsignedcharuint8;typedefsignedshortint16;typedefunsignedshortuint16;typedefsignedlongint32;typedefun

c++ - 将 int 转换为 size_t

我想知道当我传递integer时clang编译器的以下警告到std::initializer_list:non-constant-expressioncannotbenarrowedfromtype'int'to'unsignedlong'ininitializerlist为什么可以int被转换为size_t但是一个int不会传递给std::initializer_list,即intmain(){size_ts_t=0;inti=0;std::initializer_listi_l={i};//warnings_t=i;//nowarningreturn0;}

c++ - 使 shared_ptr 失去内存所有权

我有一个shared_ptr我绕过它。最终,在某些情况下,我想将原始指针传递给一个函数,然后该函数成为内存所有者。在这些情况下shared_ptr不再负责释放内存,因为我调用的函数获得了所有权。我如何获得shared_ptr失去所有权?我想要shared_ptr的原因失去所有权是因为我想使用ProtocolBuffer的AddAllocated功能,它接受一个已经分配的指针并获得它的所有权。例子:shared_ptrmyProtoSharedPtr=//bythispointthisisthelastreferencetotheheapallocatedMyProto//Iwantto