| View previous topic :: View next topic |
| Author |
Message |
Pat Guest
|
Posted: Sat Mar 06, 2004 4:30 pm Post subject: please help me... |
|
|
Hi,
I want to use "new" to build an array test[1000][1000]. Can you tell me how
to do?
Thank.
Pat
|
|
| Back to top |
|
 |
Pat Guest
|
Posted: Sat Mar 06, 2004 4:30 pm Post subject: Re: please help me... |
|
|
I don't want to use "int test[1000][1000]".
Thanks.
"Pat" <Pat (AT) Pat (DOT) com> 在郵件 news:4049fc90$1_2 (AT) rain (DOT) i-cable.com 中撰寫...
| Quote: | Hi,
I want to use "new" to build an array test[1000][1000]. Can you tell me
how
to do?
Thank.
Pat
|
|
|
| Back to top |
|
 |
AirPete Guest
|
Posted: Sat Mar 06, 2004 6:56 pm Post subject: Re: please help me... |
|
|
Pat wrote:
| Quote: | I don't want to use "int test[1000][1000]".
Thanks.
"Pat" <Pat (AT) Pat (DOT) com> 在郵件 news:4049fc90$1_2 (AT) rain (DOT) i-cable.com
中撰寫...
Hi,
I want to use "new" to build an array test[1000][1000]. Can you tell
me how to do?
Thank.
Pat
|
Try a jagged vector of vectors:
(not compiled)
vector<vector arr;
arr.resize(1000);
for(int i =0;i<1000;++i)
arr[i].resize(1000);
Then simply:
arr[x][y] = 1234;
- Pete
|
|
| Back to top |
|
 |
osmium Guest
|
Posted: Sat Mar 06, 2004 7:43 pm Post subject: Re: please help me... |
|
|
Pat writes:
| Quote: | I want to use "new" to build an array test[1000][1000]. Can you tell me
how
to do?
|
This works.
<add boilerplate>
void foo(int b[][4])
{
cout << b[2][3] << endl;
}
void test()
{
int(*a)[4] = new int[3][4];
a[2][3] = 1024;
foo(a);
}
|
|
| Back to top |
|
 |
Mike Wahler Guest
|
|
| Back to top |
|
 |
TJB Guest
|
Posted: Sun Mar 07, 2004 8:05 am Post subject: Re: please help me... |
|
|
"Pat" <Pat (AT) Pat (DOT) com> wrote
| Quote: | Hi,
I want to use "new" to build an array test[1000][1000]. Can you tell me
how
to do?
Thank.
Pat
Hi, |
You have to create an array of arrays like this:
int i,**test;
test = new int*[1000];
for (i = 0; i < 1000; i++)
test[i] = new int[1000];
You could also start with an array of pointers, int* test[1000], and
initialize the elements in it.
|
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Sun Mar 07, 2004 8:23 am Post subject: Re: please help me... |
|
|
* "TJB" <centaurus (AT) telia (DOT) com> schriebt:
| Quote: |
"Pat" <Pat (AT) Pat (DOT) com> wrote
I want to use "new" to build an array test[1000][1000]. Can you tell me
how to do?
You have to create an array of arrays like this:
int i,**test;
test = new int*[1000];
for (i = 0; i < 1000; i++)
test[i] = new int[1000];
You could also start with an array of pointers, int* test[1000], and
initialize the elements in it.
|
Well no, e doesn't "have to".
Except if e _really_ doesn't want a two-dimensional array.
See the answer from osmium earlier in the thread about how to allocate
a two-dimensional array using new,
int (*p)[1000] = new int[1000][1000];
The general question is also a FAQ, see
http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.15>,
"How do I allocate a multidimensional array using new?"
Unfortunately, in spite of the heading the FAQ does not show how to allocate
a multidimensional array using new (!), only jagged arrays like yours. But
it does go on to show a few generally much better ways.
--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
|
|
| Back to top |
|
 |
LNK2005 Guest
|
Posted: Sun Mar 07, 2004 9:13 am Post subject: Re: please help me... |
|
|
"osmium" <r124c4u102 (AT) comcast (DOT) net> skrev i meddelandet
news:c2d9mi$1q469c$1 (AT) ID-179017 (DOT) news.uni-berlin.de...
| Quote: | Pat writes:
int(*a)[4] = new int[3][4];
|
Maybe you should point out that this only works if the number of items in
the second dimension (=4) is known at compile time, which limits the
usefullness of this method.
|
|
| Back to top |
|
 |
|