For those of you who cannot afford the overhead of std::map (or cannot use it because you do not have a default constructor in your class), but whose data is not as simple as int , I wrote a guide to a solution using std::vector , which is optimal when you know the total number of elements in advance.
The manual contains a fully functional sample code that you can download and test yourself.
The solution mentioned here assumes that you control the class code so that in particular you can add some attributes. If this is still not possible, you can always add a wrapper around it:
class Wrapper { UntouchableClass const& mInstance; size_t dsID; size_t dsRank; size_t dsParent; }
In addition, if you know that the number of elements is small, there is no need for size_t , in which case you can add a template for the UnsignedInt type and solve it at run time using uint8_t , uint16_t , uint32_t or uint64_t , which you can get with using <cstdint> in C ++ 11 or using boost::cstdint otherwise.
template <typename UnsignedInt> class Wrapper { UntouchableClass const& mInstance; UnsignedInt dsID; UnsignedInt dsRank; UnsignedInt dsParent; }
Here's the link again if you missed it: http://janoma.cl/post/using-disjoint-sets-with-a-vector/
Janoma Apr 02 '14 at 2:14 a.m. 2014-04-02 02:14
source share