EDIT: The question changed, latitude/longitude are gone and some things have been clarified. I'm starting over:
Your question seems to imply that the distance calculation is not just a simple std::abs(X-Y), as it would make no sense at all to store the result to speed things up. I'll assume that you have an expensive function that calculates it, let's say:
int distance( int X, int Y ) { /* heavy stuff */ }
Now you need to decide whether or not to call it or if you have already done that and you can reuse the result. You need a container to store the results and a function to use it:
typedef std::pair< int, int > key;
std::map< key, int > values;
int quick_distance( int X, int Y )
{
const auto k = key(X,Y);
const auto it = values.find(k);
if( it != values.end() ) return it->second;
const auto d = distance(X,Y);
values[k] = d;
return d;
}