As others have pointed out, using std :: set or std :: multiset just doesn't shorten it.
You probably didn’t notice, because you use pointers, but the assumption is that the objects are immutable (although in this case it means that the pointers are const, but not pointed).
Therefore, you cannot use (directly) a standard container that will automatically perform your accounting.
At this point you have several solutions:
- you can use the library, in this case Boost.MultiIndex comes to mind, although you will have to learn how to use it.
- or you can wrap a standard container in a dedicated class (for example, your set)
I think both of them are equally important. Since the operation is very simple, you may not want to use the Boost library for this yet (learning curve, integration, ...).
Alternatively, you can use 'invasive' containers. I mean, you can use the "Observer" template here → your object can notify its container every time its value changes, so that the container moves it to its "new" correct position (using the internal std :: multiset).
If efficiency is a problem, I would not consider sorting a vector. Sorting a full container every time one change of an object is waste, the erase / insert method is much more efficient.
Matthieu monrocq
source share