如何使用New Operator定义2D数组? [英] How do you define 2D arrays using New Operator?

查看:52
本文介绍了如何使用New Operator定义2D数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在sub中动态分配一个二维数组


float * myarray = new float [n] [n];


当然我收到了错误。如何使用New

运算符分配2D数组?


我目前正在使用


static float gxy [10000] [10000];


但是我不希望我的数组在sub中耗尽我的堆栈空间。


如果

,当子结束时,数组指针永远不会被删除,New运算符也会自动使数组中的值成为静态吗?那就是我只有在第一次进入sub时才分配数组
。所有后续子

调用都跳过数组分配部分,并使用在第一个子调用中分配的数组指针

。当子结束时,数组中的指针和值不是




感谢您的帮助。


Dennis

I would like to dynamically allocate in a sub a 2 dimensional Array

float *myarray = new float [n][n];

of course I get an error. How do you allocate a 2D array using the New
operator?

I currently use

static float gxy[10000][10000];

but I don''t want my array in the sub to use up my stack space.

Also does the New operator automatically make the values in the array static if
the array pointer is never deleted when the sub is ended? That is I only
allocate the array once the first time I enter the sub. All subsequent sub
calls skip the array allocation section and use the array pointer that was
allocated in the first sub call. The pointer and values in the array are not
touched when the sub ends?

Thanks for any help.

Dennis

推荐答案



< De **** @ NoSpam.com>在消息中写道

news:47 ******************************** @ 4ax.com ...

<De****@NoSpam.com> wrote in message
news:47********************************@4ax.com...
我想在sub中动态分配一个二维数组

float * myarray = new float [n] [n];
<当然我收到了一个错误。如何使用New
运算符分配2D数组?

http://www.parashift.com/c++-faq-lit...html#faq-16.15

我目前正在使用

静态浮点gxy [ 10000] [10000];

但我不希望我的数组在sub中耗尽我的堆栈空间。

New运算符也自动生成值在数组
static如果子结束时从不删除数组指针?


否。对象可以拥有三个存储持续时间之一:

自动,静态或已分配。 ''new''分配一个对象,

和对象''生活''直到专门取消分配

''删除'',或者程序终止。

这是我只在第一次输入sub时分配数组。所有后续的
子调用都会跳过数组分配部分,并使用在第一个子调用中分配的数组指针。当子结束时,数组中的指针和值是
I would like to dynamically allocate in a sub a 2 dimensional Array

float *myarray = new float [n][n];

of course I get an error. How do you allocate a 2D array using the New
operator?
http://www.parashift.com/c++-faq-lit...html#faq-16.15
I currently use

static float gxy[10000][10000];

but I don''t want my array in the sub to use up my stack space.

Also does the New operator automatically make the values in the array static if the array pointer is never deleted when the sub is ended?
No. An object can have one of three ''storage durations'':
automatic, static, or allocated. ''new'' allocates an object,
and the object ''lives'' until specifically deallocated with
''delete'', or the program terminates.
That is I only
allocate the array once the first time I enter the sub. All subsequent sub calls skip the array allocation section and use the array pointer that was
allocated in the first sub call. The pointer and values in the array are not touched when the sub ends?




见上文。


-Mike



See above.

-Mike


文章< 47 **************************** ****@4ax.com>,
De****@NoSpam.com 写道:
In article <47********************************@4ax.com>,
De****@NoSpam.com wrote:
我想在sub中动态分配一个二维数组

float * myarray = new float [n] [n];

当然我收到了一个错误。如何使用New
运算符分配2D数组?
I would like to dynamically allocate in a sub a 2 dimensional Array

float *myarray = new float [n][n];

of course I get an error. How do you allocate a 2D array using the New
operator?




使用类:

====== ==== ========== ========== ========== ========== ====== ====


模板< typename T,typename U = std :: deque< T> >

class Matrix {

public:

typedef typename U :: size_type size_type;

typedef typename U ::参考参考;

typedef typename U :: const_reference const_reference;

typedef typename U :: iterator iterator;

typedef typename U :: const_iterator const_iterator;


Matrix():h_limit(0),v_limit(0){}

矩阵(size_type h,size_type v):buffer(h * v),h_limit(h),

v_limit(v){}


引用运算符()(size_type h,size_type v){

check_limits(h,v);

返回缓冲区[h_limit * h + v];

}


const_reference operator()(size_type h,size_type v)const {

check_limits(h,v);

返回缓冲区[h_limit * h + v];

}


iterator begin(){return buffer.begin(); } $ / $
iterator end(){return buffer.end(); } $ / $
const_iterator begin()const {return buffer.begin(); } $ / $
const_iterator end()const {return buffer.end(); }

私人:

void check_limits(size_type h,size_type v)const {

if(h> = h_limit || v> = v_limit)throw std :: out_of_range(

" Matrix");

}

U缓冲区;

size_type h_limit,v_limit;

};

========== ========== ====== ==== ========== ========== ==========


使用以上简单地说:


Matrix< float> gxy(10000,10000);

浮点值= gxy(234,543);

断言(值== 0);



Use a class:
========== ========== ========== ========== ========== ==========

template < typename T, typename U = std::deque< T > >
class Matrix {
public:
typedef typename U::size_type size_type;
typedef typename U::reference reference;
typedef typename U::const_reference const_reference;
typedef typename U::iterator iterator;
typedef typename U::const_iterator const_iterator;

Matrix(): h_limit( 0 ), v_limit( 0 ) { }
Matrix( size_type h, size_type v ): buffer( h * v ), h_limit( h ),
v_limit( v ) { }

reference operator()( size_type h, size_type v ) {
check_limits( h, v );
return buffer[h_limit * h + v];
}

const_reference operator()( size_type h, size_type v ) const {
check_limits( h, v );
return buffer[h_limit * h + v];
}

iterator begin() { return buffer.begin(); }
iterator end() { return buffer.end(); }
const_iterator begin() const { return buffer.begin(); }
const_iterator end() const { return buffer.end(); }
private:
void check_limits( size_type h, size_type v ) const {
if ( h >= h_limit || v >= v_limit ) throw std::out_of_range(
"Matrix" );
}
U buffer;
size_type h_limit, v_limit;
};
========== ========== ========== ========== ========== ==========

To use the above simply:

Matrix<float> gxy( 10000, 10000 );
float value = gxy( 234, 543 );
assert( value == 0 );

< br>

De****@NoSpam.com 写道:
我想要动态分配一个二维数组
I would like to dynamically allocate in a sub a 2 dimensional Array




一种方法,虽然不像使用类那样安全或有效,

是分配一个数组数组:


模板< typename T>

T ** new2D(int numRows,int numCols){

T ** a =新T * [numRows];

for(int i = 0; i< numRows; i ++){

a [i] =新T [numCols];

}

}


然后将其索引如下:

a [row] [col]


-

Derrick Coetzee

我将此新闻组发布到公共领域。我不承担所有

明示或暗示保证并承担所有责任。我不是专业人士。



One way to do this, although not as safe or efficient as using a class,
is to allocate an array of arrays:

template <typename T>
T** new2D(int numRows, int numCols) {
T** a = new T*[numRows];
for(int i=0; i<numRows; i++) {
a[i] = new T[numCols];
}
}

You then index it like this:
a[row][col]

--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.


这篇关于如何使用New Operator定义2D数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆