将对象插入哈希表 (C++)
这是我第一次制作哈希表。我试图将字符串(键)与指向 Strain 类对象(数据)的指针相关联。
// Simulation.h
#include <ext/hash_map>
using namespace __gnu_cxx;
struct eqstr
{
bool operator()(const char * s1, const char * s2) const
{
return strcmp(s1, s2) == 0;
}
};
...
hash_map< const char *, Strain *, hash< const char * >, struct eqstr > liveStrainTable;
在 Simulation.cpp 文件中,我尝试初始化表:
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 );
liveStrainTable[ MRCA ]= firstStrainPtr;
收到一条错误消息,内容为“'((Simulation*)this)->Simulation::liveStrainTable[MRCA] 中的 'operator[]' 不匹配” ”。我还尝试以不同的方式使用“liveStrainTable.insert(...)”,但无济于事。
真的很想在这方面得到一些帮助。我很难理解适合 SGI hash_map 的语法,以及 SGI 参考< /a> 几乎没有为我澄清任何事情。谢谢。
This is my first time making a hash table. I'm trying to associate strings (the keys) with pointers to objects (the data) of class Strain.
// Simulation.h
#include <ext/hash_map>
using namespace __gnu_cxx;
struct eqstr
{
bool operator()(const char * s1, const char * s2) const
{
return strcmp(s1, s2) == 0;
}
};
...
hash_map< const char *, Strain *, hash< const char * >, struct eqstr > liveStrainTable;
In the Simulation.cpp file, I attempt to initialize the table:
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 );
liveStrainTable[ MRCA ]= firstStrainPtr;
I get an error message that reads "no match for ‘operator[]’ in ‘((Simulation*)this)->Simulation::liveStrainTable[MRCA]’." I've also tried using "liveStrainTable.insert(...)" in different ways, to no avail.
Would really love some help on this. I'm having a difficult time understanding the syntax appropriate for SGI hash_map, and the SGI reference barely clarifies anything for me. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试
liveStrainTable[ MRCA.c_str() ]=firstStrainPtr;
。它期望const char *
作为键值类型,但MRCA
具有string
类型。另一种方法是将 liveStrainTable 更改为:
Try
liveStrainTable[ MRCA.c_str() ]= firstStrainPtr;
. It expectsconst char *
as type of key value, butMRCA
has typestring
.Another way is to change
liveStrainTable
to:其他人回答了您的直接问题,但我可以建议使用 unordered_map 相反 - 它随下一版本的 STL 一起提供,并受到所有主要编译器的支持。
Others answered your direct question, but may I suggest using unordered_map instead - it's coming with the next version of the STL and is supported by all major compilers.
hash_map 不是 STL 的一部分。没有提供 hash 的实现,或者换句话说,hash_map 默认情况下无法对字符串进行哈希处理。您需要自己的哈希函数。尝试
:
hash_map is not part of STL. There's no implementation provided for hash, or in other words, the hash_map can't hash strings by default. You need your own hash function. T
Try:
hash_map 使用 const char * 作为键类型定义,并且在访问时使用 std::string 作为键。这是两种不同的类型,模板没有为第二种类型构建运算符,因此这是一个错误。使用 std::string 作为哈希图定义或使用 MRCA.c_str()
The hash_map is defined with const char * as the key type and you are using an std::string as the key when accessing. These are 2 different types, the template did not build an operator for the second type, so this is an error. Use std::string for the hashmap definition or use MRCA.c_str()
现在,您的类型不匹配。您正在传递 MRCA(字符串),其中需要
char const *
。您可以使用 c_str() 从字符串中获取 char const * ,或者(更好)更改哈希表的定义以将字符串作为其键类型。Right now, you have a type mis-match. You're passing MRCA (a string) where a
char const *
is expected. You can either usec_str()
to get achar const *
from the string, or (far better) change the definition of your hash table to take a string as its key type.