Converting from int** to vector< vector<int> >&

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.
 
V

Victor Bazarov

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.


Without knowing what kind of calculations you're doing, it's difficult
to predict the improvements that can be achieved by a certain method,
but you could try rewriting your calculation algorithm in terms of
iterators, which should work fine whether you store the data in arrays
(dynamic, automatic) or in standard containers.

I don't have an example for you. Do you have a copy of "The C++
Standard Library"? You should...

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top