Basic questions: Pointers to objects in unordered_maps (C++)

我是研究僧i 提交于 2019-12-05 02:03:30

问题


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.


回答1:


A pointer to any object allocated with new will remain valid until you call delete on the object. You can copy the pointer as much as you like, and it will be valid as long as the underlying object has not been deleted.

Secondly, yes, you are correct that you can access object attributes from the stored pointers via container iterators. But always check to make sure that the return value of hmap::find() is not equal to hmap::end().

So what you describe is fine. Now, as to why the pointers in your strainTable are ending up pointing to garbage, I couldn't say without more details. Are you sure you're not deleting any objects anywhere? Are you sure that when you copy pointers from one vector to the other, you are doing it correctly?




回答2:


if you never delete them, then the pointers are still ok. On the other hand, you might want to keep things a bit tidier and use standard containers for the whole shebang.

typedef std::tr1::unordered_map<std::string, Strain> hmap;
typedef std::tr1::unordered_map<std::string, hmap::iterator> weakhmap;

hmap strainTable;
weakhmap liveStrains;
Strain firstStrain( idCtr, MRCA, NUM_STEPS );
strainTable[MRCA] = firstStrain;
liveStrains[MRCA] = strainTable.find(MRCA);


来源:https://stackoverflow.com/questions/1652901/basic-questions-pointers-to-objects-in-unordered-maps-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!