If you use vectors instead of arrays, you can use an iterator in the vector constructor to copy.
std::vector<int> vi; vi.push_back(1); vi.push_back(2); vi.push_back(3); std::vector<float> vf(vi.begin(), vi.end()); assert(vf.size() == 3);
If you have an input array, but you can have a vector as output, you can also do this:
int ai[] = {1,2,3}; std::vector<float> vf(ai, ai+3); assert(vf.size() == 3);
If you need to input and output an array, you can use std::copy , but just make sure your output array is large enough:
int ai[] = {1,2,3}; float af[] = {0, 0, 0}; std::copy(ai, ai+3, af);
Note: std::copy , and the vector constructor will not blindly copy memory, it will implicitly display between the two types for each element . It performs tasks * result = * first, * (result + 1) = * (first + 1) and so on ...
Brian R. bondy
source share