草庐IT

array_new

全部标签

c++ - 没有 new 的动态数组 (C++)

我是C++的新手,这是一个非常基本的问题。在C++中,只有两种创建动态数组的方法(读过一本书,如果我错了请纠正我)使用new运算符或malloc()函数,取自C。当声明一个数组intarray[size]时,方括号[]必须有一个const.但是在下面的代码中,size是一个unsignedint变量。#includeintmain(){usingnamespacestd;unsignedintsize;cout>size;intarray[size];//DynamicallyAllocatingMemorycout>array[i];}//DisplayingElementscout

c++ - 当您调用 new[] 为 N 个整数分配一个数组时,是否保证该数组将按顺序分配到物理内存中?

据我了解,每个计算机程序总是使用虚拟内存,而处理物理内存的方式取决于操作系统。我正在参加一个算法工程类(class),在某个时候有人提到,如果缓存内存是无限的并且一个缓存行的大小为B那么预计会发生的缓存未命中数如果您只想扫描N元素的数组,则为N/B我可以看出这在理论上是如何工作的,因为我们假设N元素在物理内存中一个接一个地放置。但是,这实际上是真的吗?如果虚拟内存是顺序分配的,那是否也意味着物理内存也将是顺序分配的?在我看来,在实践中,假设N不大于缓存大小,如果N元素未在物理内存(RAM)中按顺序分配。也许我误解了虚拟内存和物理内存之间的区别,我不确定。 最

c++ - 是否有像 C# 中那样的 C++ new 声明

我想知道是否有像C#中那样的new声明用于C++C#允许您这样做,它只是稍微整理了代码:FuncCall(newFoo(){Bar="sausage",Boo=4});只是我觉得这在C++中有点草率:unique_ptrfoo(newFoo());foo.Bar="sausage";foo.Boo=4;FuncCall(move(foo));Foo可能看起来像这样:classFoo{public:Foo();stringBar;intBoo;}为什么我不将所有内容都放入构造参数中?因为当你必须初始化这么多的时候,这很愚蠢:Foo(intwidth,intheight,stringtit

c++ - map 、集合等的 array_view 替代方案

假设我有一些类层次结构,其中有几个virtual返回容器引用的函数:#include#include#include#include#includeclassInterface{public:virtualconststd::vector&getArray()const=0;virtualconststd::set&getSet()const=0;virtualconststd::map&getMap()const=0;};classSubclassA:publicInterface{public:conststd::vector&getArray()constoverride{ret

c++ - c++11 严格别名规则是否允许通过 char *、char(&)[N]、甚至 std::array<char, N>& 使用 -fstrict-aliasing -Wstrict-aliasing=2 访问 uint64_t?

根据this关于C++11/14严格别名规则的stackoverflow回答:Ifaprogramattemptstoaccessthestoredvalueofanobjectthroughaglvalueofotherthanoneofthefollowingtypesthebehaviorisundefined:thedynamictypeoftheobject,acv-qualifiedversionofthedynamictypeoftheobject,atypesimilar(asdefinedin4.4)tothedynamictypeoftheobject,atypet

c++ - 当类没有 constexpr 构造函数时简化冗余 std::array 初始化

我有以下代码的更复杂版本:#include#includeusingnamespacestd;classDummy{public:Dummy(constdoublea,constdoublef){//Somecomplexcalculations}};constexprdoublevalues[]{0.1,0.2,0.3,0.4};constexprautoN=sizeof(values)/sizeof(values[0]);staticconstarraydummies{Dummy(10*values[0],M_PI*0),Dummy(10*values[1],M_PI*1),Dum

c++ - 类模板中 std::array 的大小取决于模板参数

我有一个下面的类模板templateconstexprintarraySize(){returnarraySize()+N;}templateconstexprintarraySize(){return0;}templateclassMyClass{public:std::array()>arr;};intmain(){MyClasscls;std::cout一切正常,但我想要calculateArraySize()作为成员函数。我尝试了以下方法:templateclassMyClass{public:staticconstexprintarraySize();std::array::

c++ - 为什么 new 和 delete 在 MSVC 2010 下的循环中这么慢

当我尝试在循环中创建和删除类的实例时遇到问题。迭代的执行时间是完全不同的。据我了解,这与从内存中删除对象有关。但是,这个操作的行为我不明白。为什么时间不同?我如何解决它?当我在单独的线程中删除对象时,时间是稳定的。classNODE{public:NODE(){}NODE*add(NODE*node){children.push_back(node);returnnode;}virtual~NODE(){for(vector::iteratorit=children.begin();it!=children.end();++it){delete*it;}}vectorchildren;

.net - 如何将 cli::array 从 native 代码转换为 native 数组?

我正在围绕用C++\CLI编写的托管组件编写native包装器。我在托管代码中有以下功能:array^Class::Function();我想从具有以下签名的nativeC++类公开此函数:shared_arrayClass::Function();我已经知道如何从native代码调用托管函数,但我不确定如何安全地将托管数组复制到非托管数组中。gcroot^>managedArray=_managedObject->Function(); 最佳答案 有两种常用的方法:使用native代码执行编码(marshal)处理,这需要使用pi

c++ - R 数值 vector 列表 -> C++ 2d array with Rcpp

我主要使用R,但最终想使用Rcpp与一些接收和返回二维数值数组的C++函数交互。因此,为了开始使用C++和Rcpp,我想我只需要编写一个小函数,将我的可变长度数字vector的R列表转换为C++等效项,然后再返回。require(inline)require(Rcpp)test1=cxxfunction(signature(x='List'),body='usingnamespacestd;Listxlist(x);intxlen=xlist.size();vector>xx;for(inti=0;itest=as>(xlist[i]);xx.push_back(test);}retu