J
jdm
I'm trying to rewrite some code that used arrays and pointers like
this:
void calculate_table(size_t rows, size_t columns, int *raw_data, int
**table)
{
//initialise the elements of table to zero.
//then save the results of calculations on the elements of raw_data
in it
}
so that it will use the standard library instead. Sometimes there have
been situations where I've had to leave the original function intact,
and so to avoid duplication have ended up writing something like:
void calculate_table(size_t rows, size_t columns, vector <int>&
raw_data, vector< vector <int> >& table)
{
int **array_table = new int*[rows];
for (size_t s = 0; s <rows; s++)
{
array_table = &table[0];
}
calculate_table(rows, columns, &raw_data[0], array_table);
delete[] array_table;
}
(exploiting the fact that the elements of a vector have to be stored
contiguously, as per the C++ standard.)
Is there a more efficient way of doing it than that for loop? Some of
these functions will have to be called a very large number of times
during some simulated annealing, and I may soon have to do something
like the above with some code involving three-dimensional arrays.
Thanks,
James McLaughlin.
this:
void calculate_table(size_t rows, size_t columns, int *raw_data, int
**table)
{
//initialise the elements of table to zero.
//then save the results of calculations on the elements of raw_data
in it
}
so that it will use the standard library instead. Sometimes there have
been situations where I've had to leave the original function intact,
and so to avoid duplication have ended up writing something like:
void calculate_table(size_t rows, size_t columns, vector <int>&
raw_data, vector< vector <int> >& table)
{
int **array_table = new int*[rows];
for (size_t s = 0; s <rows; s++)
{
array_table
}
calculate_table(rows, columns, &raw_data[0], array_table);
delete[] array_table;
}
(exploiting the fact that the elements of a vector have to be stored
contiguously, as per the C++ standard.)
Is there a more efficient way of doing it than that for loop? Some of
these functions will have to be called a very large number of times
during some simulated annealing, and I may soon have to do something
like the above with some code involving three-dimensional arrays.
Thanks,
James McLaughlin.