The problem of using doubles as keys in maps / sets is floating point precision.
Some people suggested adding epsilon to your comparison function, but that means that your keys will no longer fulfill the necessary strict weak ordering criteria. This means that you will get a different set / map depending on the insertion order of your elements.
In case you want to combine / combine / combine data based on double values and are ready to allow a certain level of rounding / epsilon (obviously you will have to), the following solution is a good idea
Convert all doubles (where we assign as keys) to integers, multiplying them by an accuracy coefficient (e.g. 1e8) and rounding to the nearest integer (int)i+0.5 (if i> 0), then create a set / map, which is the keys to these integers. When retrieving the final key values, divide the int by a precision factor to return a double value (albeit rounded).
c ++ dictionary set floating-point key
zhaomin
source share