migrate code using C99 dynamically allocated multidimensional arrays into C++ -


i'm in process of trying learn how things in c++, , 1 of aspects i'm grappling how efficiently implement dynamically allocated multidimensional arrays.

for example, have existing function:

void myfunc(int *lambda, int *d, int *tau, int r[*tau][*d]) {   int i, j, k, newj, leftovers;    r[0][0] = *lambda;   j = 0; // j indexes columns; start 0   for(i = 1; < *tau; i++){ // indexes rows     leftovers = *lambda;     for(k = 0; k < j; k++){       r[i][k] = r[i - 1][k]; // copy prior j       leftovers = leftovers - r[i][k];     }     r[i][j] = r[i - 1][j] - 1; // decrement     r[i][j+1] = leftovers - r[i][j]; // initialize right of j      if(j == *d - 2){ // second last column       for(k = 0; k <= j; k++){ if(r[i][k] != 0){ newj = k; } }       j = newj; // can't think of better way     }else{       j++; // increment j     }   } // next row please  } 

from i've read, seems common recommendation use std::vector purpose. care offer advice or code snippet on how implement r matrix above using std::vector equivalent?

i have thought common situation, interestingly, google turned fewer 50 hits "c99 c++".

thank you!
ben

i think straightforward conversion:

void myfunc(int *lambda, std::vector<std::vector<int> > &r) {   int i, j, k, newj, leftovers;   int tau = r.size();    r[0][0] = *lambda;   j = 0; // j indexes columns; start 0   for(i = 1; < tau; i++){ // indexes rows     int d = r[i].size();     leftovers = *lambda;     for(k = 0; k < j; k++){       r[i][k] = r[i - 1][k]; // copy prior j       leftovers = leftovers - r[i][k];     }     r[i][j] = r[i - 1][j] - 1; // decrement     r[i][j+1] = leftovers - r[i][j]; // initialize right of j      if(j == d - 2){ // second last column       for(k = 0; k <= j; k++){ if(r[i][k] != 0){ newj = k; } }       j = newj; // can't think of better way     }else{       j++; // increment j     }   } // next row please } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -