What might be a good practice to express dynamic records in C++?
I am trying to write a code which handles dynamic records which the length of the record differs from it's type. The target that I'm expressing does not have much operations to apply to, and may be accessed directly from the programmer, so I think it should be a struct
rather than a class
.
The size of the record is fixed on construction, and the number of record may exist from hundreds to tens of millions per execution.
I think I have three options. First is to use a fixed size array for having the length of maximum possible items of the records. The maximum possible length is known and the length varies from 1 to maximum 10. edited: the records has some common entries
Second option is to make a dynamic array for each records. In this case, should it be better to use std::vector
rather than manipulating the allocation myself? Or is using STL always better than implementing it myself?
Third option is to use inheritance with polymorphism. I don't want to actually do this because there is not much "common" operations or even any operations to be applied with.
Another functionality that I want is, as it is going to be a record I want to be accessible with a string or at least a name-like method. This is not mandatory but I think this might be good for future code readability. I could use a std::map
instead of a dynamic array or std::vector
I thought in this case I would like some get/setter like C# properties(which C++ does not have).
So I have few things to consider. One thing is the readability of future code. If I sacrifice the readability, then I could just access records by indexes, not by names. Another thing is performance and the memory usage. I want to avoid highly the "string" comparison operation that might occur during std::map and also want to use the least amount of memory since there are lots of records to handle with.
Until now, while doing some personal project I do have think a while when it comes to choosing which data structure to use, but actually the reasoning was not good. Please share your insights or it would be best if you recommend me a literature to read (Not only which has topics with generic algorithm and data-structure stuff but also with real coding practice and may explain in-depth STL).
Aucun commentaire:
Enregistrer un commentaire