Hi

I'm looking for an efficient way of converting an array of integers to an array of doubles.

Currently I'm doing this:

int *a = new int[1000]; //And then assign values to the indexes
double *b = new double[1000];
for(int i = 0; i < 1000; i++)
{
   b[i] = (double) a[i];
}

But this seems extremely inefficient to me, especially if you do this with large arrays over and over again. Isn't there some other way to do the conversion, such as cast or so?

Int and double have completely different memory representations -> No other way of converting the arrays.

Is there any reason why you can't use double instead of int in the first place? That way the conversion would be unnecessary.

Yes, there is an external function that takes a double array (and I can't change that function), and I'm using this function.

What I mean is, why not use an array of double from the start instead of an array of int, then pass that into the external function? Are you also using an external function that can't be changed which gives you an array of int, or something?

Can you modify the function so that it needs ints or make it template?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.