I have to fill an array of 1000 objects by reading data from disk. However, not every objects exits.
Once I declare an array, memory will be reserved for 1000 objects. As I read them one by one, I set the memory to corresponding values. However, there might not be an object for member #276 and it's memory will remain set to whatever was there when the array was declared.
How do I keep the information that certain member of the array is invalid/doesn't exist?
I could somehow set all the bytes of the member to zero, but that might be a valid object.
The obvious solution is adding another array of bytes that will be set to 1 or 0 depending whether the object at that index exists, but it doesn't seem very elegant.
Could this be done with a vector instead? Can it somehow store an empty value?
No.
Except of course if you use some extra space to store that information (exists or not), or a sentinel value for the non-existed objects. An
std::vector
has the powerful ability to resize itself according to the number of elements it stores; so if it could satisfy your request, it would lose that ability.I would use an
std::unordered_map
, where every key would be the object's index (e.g. #276) and the value would be the actual object. If an object doesn't exist, don't insert that key in the map.Or an
std::map
, if you need to iterate over your data efficiently. Choosing between std::map and std::unordered_map.Either think really hard to find a sentinel value that will mark a cell of your array as empty. For example, if you already have the data somewhere in memory (which I think is not your case though), then you could use an array of pointers, instead of an array that stores whole objects. Then it would be obvious that a
NULL
pointer would be used for cells that are emptyAnother option would be to use an array of pairs, like this:
std::pair<myClass, bool>
, where the second operand indicates whether the corresponding cell is empty or not.Moreover, you could use a
std::vector<bool>
instead, which is very memory efficient (if you decide to follow an approach of an extra data structure), as mentioned in Why does std::vector<bool> has no .data()?. It will however lack in index performance.