基本问题:指向 unordered_maps (C++) 中对象的指针 [英] Basic questions: Pointers to objects in unordered_maps (C++)

查看:51
本文介绍了基本问题:指向 unordered_maps (C++) 中对象的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 C++ 编程的新手,非常感谢那些不需要太多先验知识的回复.

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;

该映射中的数据是指向 Strain 类实例的指针.一旦创建了这些实例,我就会创建指向它们的指针,然后将这些指针添加到我的哈希表 (hmap trainTable) 和另一个向量 (vector liveStrains),例如,

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;

Strain 类的实例永远不会被删除,也不会从strainTable 中删除指向它们的指针.指针偶尔会在向量之间移动.Strain * > liveStrains 和 vector deadStrains,但是一旦在strainTable上,它们就会留在strainTable上.

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?

我应该总是能够从strainTable中的指针中获取成员属性是否也正确,例如,对于第一个条目,

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.

推荐答案

指向任何使用 new 分配的对象的指针将保持有效,直到您对该对象调用 delete.你可以随意复制指针,只要底层对象没有被删除,它就会有效.

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.

其次,是的,您可以通过容器迭代器从存储的指针访问对象属性是正确的.但要始终检查以确保 hmap::find() 的返回值不等于 hmap::end().

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?

这篇关于基本问题:指向 unordered_maps (C++) 中对象的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆