C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

please help me...

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Pat
Guest





PostPosted: Sat Mar 06, 2004 4:30 pm    Post subject: please help me... Reply with 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
Pat
Guest





PostPosted: Sat Mar 06, 2004 4:30 pm    Post subject: Re: please help me... Reply with 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 中撰寫...
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





PostPosted: Sat Mar 06, 2004 6:56 pm    Post subject: Re: please help me... Reply with quote



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





PostPosted: Sat Mar 06, 2004 7:43 pm    Post subject: Re: please help me... Reply with quote

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





PostPosted: Sat Mar 06, 2004 7:58 pm    Post subject: Re: [FAQ] please help me... Reply with quote

"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?

http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.15

Always check the FAQ before posting a question.

-Mike



Back to top
TJB
Guest





PostPosted: Sun Mar 07, 2004 8:05 am    Post subject: Re: please help me... Reply with quote


"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





PostPosted: Sun Mar 07, 2004 8:23 am    Post subject: Re: please help me... Reply with quote

* "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





PostPosted: Sun Mar 07, 2004 9:13 am    Post subject: Re: please help me... Reply with quote


"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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.