Reputation: 77
I have a pointer * double
array and basically, they are indexed in any order.
For example,
double[0][0] = 80.0
double[3][0] = 56.8
double[4][0] = 56.7
How do I for example check to see if double[1][2]
exists and then create one if it doesn't.
I do not intend on using vectors
or anything 'fancy'
, just plain arrays.
Upvotes: 0
Views: 3443
Reputation: 44023
Hm. Well, if you really want to do it with plain arrays (why?), there's not much you can do but rely on a magic value. I'd suggest std::numeric_limits<double>::quiet_NaN()
(NaN = not a number). That is to say:
#include <algorithm>
#include <limits>
double data[10][20]; // maximum dimensions mandatory with arrays. There's
// no way around that, and you can't grow it later.
std::fill(data[0], data[0] + 10 * 20, std::numeric_limits<double>::quiet_NaN());
Then you can later check if there's a real value in the array like so:
if(!std::isnan(data[1][2])) { // #include <cmath> for std::isnan
// oh, look, there's a value here!
} else {
// Nothing here yet. Better fill it
data[1][2] = 123.0;
}
Caveat: If your calculations may produce NaN values themselves, you're screwed this way. This happens, for example, if you attempt to calculcate 0.0 / 0.0
or std::sqrt(-1)
or std::log(-1)
or something else that has no defined value in the reals. If your calculations produce NaN and you write it into the array, this approach will act as though the value had never been written there.
Upvotes: 2