samedi 31 octobre 2020

How to design a class holding a collection and a map which associates further data to some elements of the collection?

This is mostly a design question, so maybe it's best I state it explicitly here: in the following I describe a class I'm designing and some concerns I have about my design choices; I'd like to no if there's already some design pattern or, in general, a "way of doing it" which I am not aware of.

I need to write a class MyClass { /* ... */ };

  • representing an ordered collection of "things", which can also be repeated,
  • some of which have another "piece" of information associated to them.

For simplicity, let's say that the "things" are ints and the "piece" too is an int.

As regards the first point, I'm pretty sure a std::vector is fine, as I don't need to reorder the entries, to insert at the front, or things like these. Therefore a good start seems to be

class MyClass {
    std::vector<int> v;
    // ...
};

As regards the second point, however, I'm a bit puzzled about how to do that.

Since I need to associate something else to some elements of v, it seems natural that a use a map for that, for instance a std::unordered_map. However, since the ints in v can be repeated (v could be {1,2,2,2,3,4,0,3,2}), I cannot use them as the keys. Therefore maybe I could use their addresses, and so the class could be like this:

class MyClass {
    std::vector<int> v;
    std::unordered_map<int*, int> m;
};

However I feel this opens to a lot of issues or bugs down the road, due to this class explicily dealing with pointers. Well, not even that down the road, actually. The simple fact of delegating the construction of v and m to other function(s) could trigger UB if the the std::vector from which the addresses are taken is copied into v rather than moved.

Aucun commentaire:

Enregistrer un commentaire