I'm using a C++ std::map to hold a large collection of entities:
using std::map;
map {structureEntityID, classEntityRecord} tableEntityRecords; //(replace {}s with arrows)
I will be frequently modifying the entities in my table (many times a second). Is it better to modify those records via a pointer or is it better to make a local copy, modify it, and then update the table?
For example...
Via a pointer:
classEntityRecord* getEntityRecord(structureEntityID entityID)
{
map {structureEntityID, classEntityRecord}::iterator iteratorEntityRecord;
iteratorEntityRecord = tableEntityRecords.find(entityID);
return &iteratorEntityRecord->second;
}
classEntityRecord *entityRecord;
entityRecord = getEntityRecord(entityID);
entityRecord->x = 15;
Via a copy/modify/update:
classEntityRecord getEntityRecord(structureEntityID entityID)
{
map {structureEntityID, classEntityRecord}::iterator iteratorEntityRecord;
iteratorEntityRecord = tableEntityRecords.find(entityID);
return iteratorEntityRecord->second;
}
classEntityRecord entityRecord;
entityRecord = getEntityRecord(entityID);
entityRecord.x = 15;
tableEntityRecords[entityID] = entityRecord;
I would think it is better to use a pointer, but this is my first time with C++ maps so I don't fully understand how they work.
My big worry is if I take a pointer to one of the values in my table, is it possible for the C++ map to be reordered and have that pointer no longer be valid? The program is multithreaded so entities can be added to the table while others are being modified.
I appreciate the help!