Can someone suggest an elegant way to solve the following problem please?
I have a multi-map keyed by time, and i wish to return the item that occured closest to a specified time T. In addition, the times searched within the map can only be an hour either side of T.
Tried multiple techniques, however the most efficient would seem to be to firstly reject all times that are not within an hour of T, and then iterate over the remaining items to find the one closest to T.
Just use
map.lower_bound()to find the first time that isn’t lower than the one you are looking for, then just check the adjacent (smaller) if it’s closer than the one returned bylower_bound()and you are done.