草庐IT

array_new

全部标签

c++ - Numpy 的 __array_interface__ 不返回字典

我正在使用一个外部程序来计算一个用C++编写并通过boost::python与python接口(interface)的矩阵。我想将此C数组传递给numpy,根据作者的说法,此功能已通过numpy的obj.__array_interface__实现。如果我在python脚本中调用它并将C++对象分配给X我将获得以下内容:printX#printX.__array_interface__#>printX.__array_interface__()#{'shape':(5,5),'data':(4416696960,True),'typestr':'",line96,in#ValueErro

c++ - `constexpr` `std::array` 的二元运算

我想写一个constexpr函数,减少给定的std::array用二元运算。IE。实现的函数templatereduce(std::array,binary_function);为了简单起见,我想从加法开始。例如sum(std::array{{1,2,3,4,5}});//returns15.到目前为止我得到了什么。我使用常用​​的索引技巧来索引数组元素。IE。生成int序列,可用于通过参数列表扩展进行索引。templatestructseq{};templatestructgen_seq:gen_seq{};templatestructgen_seq:seq{};//gen_seq-

c++ - 使用 std::array::size 实例化 std::array 时出错

示例代码test.cpp#include#includeintmain(){//OKconststd::arrayarray_int={42,1337};std::arrayarray_float_ok;//Errorconststd::arrayarray_string={"foo","bar"};std::arrayarray_float_error;return0;}使用g++4.8.4(Ubuntu14.04)编译g++-Wall-std=c++0xtest.cpp-otest给出如下错误信息test.cpp:Infunction‘intmain()’:test.cpp:14:

c++ - 在 C++ 中是否允许从括号值列表中赋值 std::array?

在c++primer(第5版)中,提到不允许从花括号值列表赋值std::array。Becausethesizeoftheright-handoperandmightdifferfromthesizeoftheleft-handoperand,thearraytypedoesnotsupportassignanditdoesnotallowassignmentfromabracedlistofvalues.下面的代码作为例子给出。std::arraya1={0,1,2,3,4,5,6,7,8,9};std::arraya2={0};//elementsallhavevalue0a1=a

c++ - 在派生类中重写运算符 new/delete

我有一个无状态的抽象基类,各种具体类都从中继承。其中一些派生类也是无状态的。因为它们中的许多是在运行期间创建的,所以我想通过覆盖运算符new()/delete()让所有无状态派生类模拟单例来节省内存和开销。一个简化的例子看起来像这样:#includestructBase{virtual~Base(){}protected:Base(){}//preventconcreteBaseobjects};structD1:publicBase{//statefulobject--defaultbehaviorintdummy;};structD2:publicBase{//statelesso

c++ - 取一个数组引用 `T(&)[n]` 到一个 `std::array<T, n>` 的内容

假设我有一个std::array并希望获取对其内容的数组引用(即未公开的elems数组成员)。我很惊讶地发现std::array::data()返回T*而不是T(&)[n],所以似乎有必要进行某种类型转换。我可以写:std::arrayarr;int(&ref)[5]=*reinterpret_cast(arr.data());但是,这看起来很丑陋并且可能不安全。它是合法的(定义明确的)代码吗?是否有更好的方法来做到这一点? 最佳答案 该标准没有提供array的底层实现,但是如果它使用int[5]作为底层表示,那么对于该实现,只有您

c++ - Constexpr 替代 placement new 能够使内存中的对象保持未初始化状态?

我正在尝试创建一个静态容器,它具有基于堆栈的内存并且可以容纳T的N个实例。非常类似于std::vector我希望当前未使用的内存不包含T的初始化项。这通常可以通过placementnew来解决,但不可能在constexpr中使用。使用union我发现了一个技巧,您可以为此使用union,如下所示:templateunioncontainer_storage_type{structempty{};constexprcontainer_storage_type():uninitialized{}{}constexprcontainer_storage_type(value_typev):v

c++ new运算符通过libstdc++占用大量内存(67MB)

我对libstdc++中的new运算符有一些疑问。我用C++编写了一个程序,但在内存管理方面遇到了一些问题。在用gdb调试以确定是什么在消耗我的ram之后,我得到了以下infoprocmappingsMappedaddressspaces:StartAddrEndAddrSizeOffsetobjfile0x4000000x4040000x40000/home/sebastian/Developement/powerserverplus-svn/psp-job-distributor/Release/psp-job-distributor0x6040000x6050000x10000x

c++ - 使用 std::shared_ptr<std::vector> 而不是 boost::shared_array 有意义吗?

现在我正在重写部分代码以使用C++11标准。在某些地方我发现了以下代码:boost::shared_arrayarray;是否可以将其替换为:std::shared_ptr>array;我正在尝试在我的代码中替换C++11中已经存在的所有boost功能。我需要澄清一点。实际上我需要一个原始数组(但有引用计数,所以它可以自动删除),不需要所有那些vector特征。所以boost::shared_array解决了我想要的问题,没有任何额外的成本。但我试图让我的代码尽可能多地使用新标准(尽管新标准仍未涵盖来自boost的许多库)。谢谢。 最佳答案

c++ - 在 'this' 指针上使用 placement new 是否安全

当前实现我有一个包含unique_ptr字段的类,这些字段相互依赖:classResourceManager{ResourceManager(){}ResourceManager(A*a_ptr):b_ptr(newB(a)),c_ptr(newC(b_ptr.get())){}ResourceManager&operator=(ResourceManager&&that){//Calldestructor,thenconstructanewinstanceontop~ResourceManager();ResourceManager*new_this=new(this)Resourc