Properly Initializing a Dynamically Defined Triple String Array?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sherman

    Properly Initializing a Dynamically Defined Triple String Array?

    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
  • Paavo Helde

    #2
    Re: Properly Initializing a Dynamically Defined Triple String Array?

    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 ::stringdat_arr ay;
    dat_array.resiz e( t*3*5);
    ....
    dat_array[idx(i, j, k, t)] = "Hello everybody!";

    YMMV
    Paavo

    Comment

    • red floyd

      #3
      Re: Properly Initializing a Dynamically Defined Triple String Array?

      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 ::stringdat_arr ay;
      dat_array.resiz e( 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)

      Comment

      • Paavo Helde

        #4
        Re: Properly Initializing a Dynamically Defined Triple String Array?

        red floyd <no.spam.here@e xample.comkirju tas:
        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<st d::stringdat_ar ray;
        >dat_array.resi ze( 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

        Comment

        Working...