6

I have a code in which at the end of a function I need to cast from int to double all the elements of an array in order to being able to do a final push_back before exiting the function. The code I have right now is:

template <class T, size_t dims> class A {
    typedef typename std::array<int, dims> ArrayInt;
    typedef typename std::array<double, dims> ArrayDouble;
    typedef typename std::vector <ArrayDouble> VectorDouble;

/* ...*/

foo() {
   /*  ...*/

   ArrayInt myArrayInt;
   ArrayDouble myArrayDouble;
   VectorDouble myVectorDouble;

    /* Initialize myArrayInt 
    Do some other stuff */

    for (int i = 0; i < dims; ++i) 
        myArrayDouble[i] = static_cast<double>(myArrayInt[i]);

    myVectorDouble.push_back(myArrayDouble);
    }
}

It works properly, but I do not feel comfortable about the lines:

for (int i = 0; i < dims; ++i) 
    myArrayDouble[i] = static_cast<double>(myArrayInt[i]);

Is there any better way of doing this?

Thank you.

Javi
  • 3,440
  • 5
  • 29
  • 43
  • 3
    From your title I assume that it's `static_cast<>` that's making you uncomfortable. [Here](http://stackoverflow.com/questions/103512/in-c-why-use-static-castintx-instead-of-intx)'s a good explanation on `static_cast<>`. In your case the cast is a valid conversion supported by the language. There is no other way to tell C++ to cast a vector (or an array) of `double`s to a vector (or an array) of `int`s than by looping and casting value by value. This is fine. – ierceg Feb 28 '14 at 13:50
  • 2
    which part of these lines do you feel uncomfortable with exactly? The loop, the cast, both? For the loop: look at std::transform. For the cast: this one shouldn't hurt, but something like boost::numeric_cast is a good choice as it takes care of rounding/out of range/... – stijn Feb 28 '14 at 13:50
  • 3
    Your `for`-loop could probably be simplified to `std::copy(myArrayInt.begin(), myArrayInt.end(), myArrayDouble.begin());`, but otherwise it looks fine. – johnsyweb Feb 28 '14 at 13:56
  • Did you size your destination? – Yakk - Adam Nevraumont Feb 28 '14 at 14:13
  • I am not happy with the loop. Since both arrays have the same value I was sure there were a way to directly remap memory of something like that. I am looking for an efficient solution. About the cast, I am using static because I thought it was the most efficient. Since I'm sure that I'm casting always from valid ints to valid doubles, reintrepret_cast or const_cast would be more efficient? – Javi Feb 28 '14 at 14:32

2 Answers2

5

You can use a function from algorithm.

With copy_n :

std::copy_n( myArrayInt.begin(), dims, myArrayDouble.begin() );

or with copy :

std::copy( myArrayInt.begin(), myArrayInt.end(), myArrayDouble.begin() );
BЈовић
  • 62,405
  • 41
  • 173
  • 273
3

This can be written with less code, but it is explicit.

ArrayInt myArrayInt;
ArrayDouble myArrayDouble;
VectorDouble myVectorDouble;

/* Initialize myArrayInt 
Do some other stuff */

using std::transform;
using std::copy;
using std::begin;
using std::end;

// with explicit conversion
auto to_double = [](const int i) { return double{i}; };
transform(begin(myArrayInt), end(myArrayInt), begin(myArrayDouble),
    to_double);

// with implicit conversion
copy(begin(myArrayInt), end(myArrayInt), begin(myArrayDouble));
utnapistim
  • 26,809
  • 3
  • 46
  • 82