基本问题:指向 unordered_maps 中对象的指针 (C++)

发布于 2024-08-09 17:22:56 字数 971 浏览 3 评论 0原文

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

感谢这里的建议,我创建了一个无序映射:

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

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

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;

类应变的实例永远不会已删除,指向它们的指针也没有从应变表中删除。指针偶尔会在向量<向量之间移动。应变*>活菌株和载体<应变*> deadStrains,但一旦进入应变表,它们就会保留在应变表上。

这是犹太洁食吗?只要底层实例永远不会被破坏,添加到它们的指针就会保持不变吗?

我应该始终能够通过使用例如第一个条目从应变表中的指针获取成员属性,这是否也是正确的,

 hmap::const_iterator itr1 = strainTable.begin();
 int id = (itr1->second)->getStrainID();

我发现一段时间后,我的应变表中的指针指向垃圾。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自由范儿 2024-08-16 17:22:56

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

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

所以你描述的情况是没问题的。现在,至于为什么你的应变表中的指针最终指向垃圾,如果没有更多细节,我无法说。您确定没有删除任何地方的任何对象吗?您确定将指针从一个向量复制到另一个向量时是否正确?

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?

懵少女 2024-08-16 17:22:56

如果您从未删除它们,那么指针仍然可以。另一方面,您可能想让事情变得更整洁,并为整个 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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文