[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Properly Initializing a Dynamically Defined Triple String Array?

sherman

11/18/2008 11:00:00 PM

string ***dat_array;
int t;

dat_array = new string **[t];
for (i=0; i<t; i++) {
dat_array[i] = new string *[3];
for (j=0; j<5; j++)
dat_array[i][j] = new string [5];
}
// dat_array = new string **[t];
for (i=0; i<t; i++)
for (j=0; j<3; j++)
for (k=0; k<5; k++)
dat_array[i][j][k] = string("Hello Everybody!");

@@@@@@@@@@@@@@@@@@@@@

Is this the proper way to initialize a dynamically defined triple
string array "dat_array[t][3][5]"? I know a priori the last two
dimensions. I get the fist one, t , later in the program.
Thank you.

sherman
3 Answers

Paavo Helde

11/18/2008 11:29:00 PM

0

sherman kirjutas:

> string ***dat_array;
> int t;
>
> dat_array = new string **[t];
> for (i=0; i<t; i++) {
> dat_array[i] = new string *[3];
> for (j=0; j<5; j++)
> dat_array[i][j] = new string [5];
> }
> // dat_array = new string **[t];
> for (i=0; i<t; i++)
> for (j=0; j<3; j++)
> for (k=0; k<5; k++)
> dat_array[i][j][k] = string("Hello Everybody!");
>
> @@@@@@@@@@@@@@@@@@@@@
>
> Is this the proper way to initialize a dynamically defined triple
> string array "dat_array[t][3][5]"? I know a priori the last two
> dimensions. I get the fist one, t , later in the program.

Seems to be technically correct, but tedious and error-prone. I would
probably use something like the following:

int idx(int i, int j, int k, int t) {
return i + j*t + k*t*3;
}

std::vector<std::string> dat_array;
dat_array.resize( t*3*5);
....
dat_array[idx(i, j, k, t)] = "Hello everybody!";

YMMV
Paavo

red floyd

11/19/2008 6:29:00 AM

0

Paavo Helde wrote:
> sherman kirjutas:
>
>> string ***dat_array;
>> int t;
>>
>> dat_array = new string **[t];
>> for (i=0; i<t; i++) {
>> dat_array[i] = new string *[3];
>> for (j=0; j<5; j++)
>> dat_array[i][j] = new string [5];
>> }
>> // dat_array = new string **[t];
>> for (i=0; i<t; i++)
>> for (j=0; j<3; j++)
>> for (k=0; k<5; k++)
>> dat_array[i][j][k] = string("Hello Everybody!");
>>
>> @@@@@@@@@@@@@@@@@@@@@
>>
>> Is this the proper way to initialize a dynamically defined triple
>> string array "dat_array[t][3][5]"? I know a priori the last two
>> dimensions. I get the fist one, t , later in the program.
>
> Seems to be technically correct, but tedious and error-prone. I would
> probably use something like the following:
>
> int idx(int i, int j, int k, int t) {
> return i + j*t + k*t*3;
> }
>
> std::vector<std::string> dat_array;
> dat_array.resize( t*3*5);
> ...
> dat_array[idx(i, j, k, t)] = "Hello everybody!";
>

If you're going to do it that way, I'd wrap dat_array in a class, and
provide operator()(int, int, int)

Paavo Helde

11/19/2008 8:07:00 AM

0

red floyd <no.spam.here@example.com> kirjutas:

> Paavo Helde wrote:
>> sherman kirjutas:
>>
>>> string ***dat_array;
>>> int t;
>>>
>>> dat_array = new string **[t];
>>> for (i=0; i<t; i++) {
>>> dat_array[i] = new string *[3];
>>> for (j=0; j<5; j++)
>>> dat_array[i][j] = new string [5];
>>> }
>>> // dat_array = new string **[t];
>>> for (i=0; i<t; i++)
>>> for (j=0; j<3; j++)
>>> for (k=0; k<5; k++)
>>> dat_array[i][j][k] = string("Hello Everybody!");
>>>
>>> @@@@@@@@@@@@@@@@@@@@@
>>>
>>> Is this the proper way to initialize a dynamically defined triple
>>> string array "dat_array[t][3][5]"? I know a priori the last two
>>> dimensions. I get the fist one, t , later in the program.
>>
>> Seems to be technically correct, but tedious and error-prone. I would
>> probably use something like the following:
>>
>> int idx(int i, int j, int k, int t) {
>> return i + j*t + k*t*3;
>> }
>>
>> std::vector<std::string> dat_array;
>> dat_array.resize( t*3*5);
>> ...
>> dat_array[idx(i, j, k, t)] = "Hello everybody!";
>>
>
> If you're going to do it that way, I'd wrap dat_array in a class, and
> provide operator()(int, int, int)

Yes, by encapsulating and abstracting one can build tools exactly
matching the needs. Another question is whether it requires a lot of
boilerplate code, and whether it is worth it. std::vector has a rich
interface, and I would not like to lose it. For example, the deeply
nested loop for initialization with the same value in the OP post could
be replaced by a single line:

std::fill( dat_array.begin(), dat_array.end(), "Hello Everybody!");

So either one should derive publicly from std::vector (which creates its
own problems), or write some forwarding boilerplate code. Maybe the right
approach would be to derive privately from std::vector and add using
declarations for all needed names.

Paavo