I need to store discrete ranges in a set, joining adjacent ranges upon insertion. Is there a structure in STL which already has such functionality?
I have tried boost::intervals, but it's quite heavy and a bit of an overkill for what I'm trying to do.
For example, assume the set is empty and the the following elements are inserted:
[64, 96]
[0, 4]
[11, 15]
[5, 10]
The expected contents of the interval set should be as follows:
[0, 15]
[64, 96]
This is a well known question. There is a wikipedia page on possible solutions to your question. Of course in the C++ STL you could implement a solution based on the Naive approach, explained in wikipedia, using a std::map
because a map is a Red-Black Tree which is a type of Binary Search Tree.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With