I'm new to C++ programming and would greatly appreciate replies that don't assume much prior knowledge.
Thanks to suggestions here, I've created an unordered map:
typedef std::tr1::unordered_map<std::string, Strain*> hmap;
The data in this map are pointers to instances of class Strain. As soon as these instances are created, I create pointers to them, and I then add these pointers to my hash table (hmap strainTable) and to another vector (vector< Strain *> liveStrains), e.g.,
string MRCA;
for ( int b = 0; b < SEQ_LENGTH; b++ ) {
int randBase = rgen.uniform(0,NUM_BASES);
MRCA.push_back( BASES[ randBase ] );
}
Strain * firstStrainPtr;
firstStrainPtr = new Strain( idCtr, MRCA, NUM_STEPS );
liveStrains.push_back( firstStrainPtr );
strainTable[ MRCA ]= firstStrainPtr;
Instances of class Strain are never deleted, nor are pointers to them removed from strainTable. Pointers do occasionally move between vector< Strain * > liveStrains and vector< Strain * > deadStrains, but once on strainTable, they stay on strainTable.
Is this kosher? As long as the underlying instances are never destroyed, will the pointers added to them remain intact?
Is it also correct that I should always be able to get member attributes from the pointers in strainTable by using, e.g., for the first entry,
hmap::const_iterator itr1 = strainTable.begin();
int id = (itr1->second)->getStrainID();
I'm finding that after a while, pointers in my strainTable point to garbage.