struct N
{
longdouble x;
longdouble y;
longdouble r;
friendoperator<(const N &T, const N &Z)
{
return T.r < Z.r;
}
};
///
map<N, longdouble> circles;
I was also given a list of 2000 circles that I attempted to store in the map. None of those are identical, although some may have the same radius lengths or the coordinates of their centers.
After reading the file my program with the declarations above outputted only 1355 as the circles' size. I have discovered that changing the operator declaration into:
1 2 3 4
friendoperator<(const N &T, const N &Z)
{
return T.r < Z.r || T.x < Z.x || T.y < Z.y;
}
makes the program work as intended. Why is it the case that the first declaration causes the container to be faulty?
Isn't the operator only used for assessing in which part of the container the data should be put? Of course, circles with the same r values would be thought of as equal (as in the order of the structs does not matter), but how come the program ignores the other parameters?
No. By default, the < operator is used to put the elements in order (which is how std::map can look up the elements so quickly) and also to know if two elements are the same (it only stores one value per key).