[Solved] error in using fill() and fill_n() in 2D double type array [closed]


Either

std::fill(dp[0], dp[0] + 165 * 2, -1.0);

or

std::fill_n(dp[0], 165 * 2, -1.0);

should work in standard C++. The error about fill macro with two arguments suggests that something #defines a macro named fill that interferes with the C++ standard library, though. This is likely to break more things than just this, so you should look into it and see if there’s something like the NOMINMAX macro that prevents windows.h from doing the same with min and max (which breaks <limits>).

The problem you run into is that the value type ofdp is not double but double[2], to which -1.0 cannot be assigned. The above code uses the fact that a 2D array of doubles looks in memory just like a flat array of doubles, which means that by casting dp to double*, it can be filled just like a 1D array. dp[0] is just the easiest way to get a pointer to the first element of dp of type double*.

Addendum: Technically, dp[0] is of type double[2], but this decays to double* when it is used. dp can only decay to double (*)[2] (pointer to array of two doubles).

7

solved error in using fill() and fill_n() in 2D double type array [closed]