Reputation: 3322
I am not sure, if this is the right place to post this question. Please let me know if it's not.
I have got a number of 'entities'. Each entity has a number of attributes(all entities have same number of attributes) which are themselves stored as arrays (of same length). I would like store the 'entities' in such a manner, so that I can fetch each entity by name.
To do this I should probably use a map<string, entity>
to store each entity by name.
Now the trouble is, before this requirement (to store multiple entities) came up, I used to store an entity as a map<string, vector>
where each vector is an attribute and the string denotes the attribute name.
Taking this into account map<string, entity>
now becomes:
map<string, map<string, vector>
What was previously a simple and elegant solution has become difficult to read and awkward to use.
Encapsulating the map<string, vector>
as an entity class seems like going too far for the sake of beautiful code (as I don't have any procedures to put in the class).
Would I be better off using entity as a struct to "beautify" the code?
Or is there a much more simple or more elegant solution that I'm missing?
Or is this implementation the best I can hope for?
Note: I am dealing with a fairly large data set. There will be 1500-2000 entities in the map. Each entity will have 4-10 attributes. Each attribute will have close to 10000 values. Performance is a constraint.
Upvotes: 0
Views: 106
Reputation: 363547
I'm going to suggest an Entity
class or struct anyway. The reason is that your implementation may change over time (e.g. from map<string, vector<T>>
to multimap<string, T>
, or maybe an unordered_map
), and giving an entity an actual API allows you to encapsulate such changes and put constraints on the way entities are handled by other code. It will also make both the code and possible compiler error messages easier to read.
Performance should not suffer if you place the class/struct implementation entirely in a header and don't use virtual
methods. In Stroustrup's words, C++ is a "lightweight abstraction language", to be parsed as a "language for lightweight abstractions", and this is a place where a lightweight abstraction seems appropriate.
Upvotes: 2