Kousik
Kousik

Reputation: 475

Is there any simple way to copy an uchar array to a double array?

I have an simple array [not vector] of data ranging between 0-255 in a uchar type array. But for some computation I need to copy the array to a double type array.

I can do that by a loop and copying it element by element. But looking if there is an easy process to do this by some function or methods.

Upvotes: 2

Views: 185

Answers (1)

Konrad Rudolph
Konrad Rudolph

Reputation: 545865

C++ algorithms work on any type of iterator, they are not restricted to specific container types.

As such, you can use std::copy to copy values from one iterator range to another, and perform implicit type conversion in the process:

uchar a[N];
double b[N];
// …
std::copy(std::begin(a), std::end(a), std::begin(b));
// or:
std::copy_n(std::begin(a), N, std::begin(b));

The above uses a C-style array but the same of course works with std::array.

Upvotes: 2

Related Questions