As carrotcake1029 and tabstop have stated, this is more or less a solved problem, especially if you are willing to look outside of the standard library. If you have access to Boost, you could use...