将对象插入哈希表 (C++)

发布于 2024-08-09 13:29:38 字数 1021 浏览 3 评论 0原文

这是我第一次制作哈希表。我试图将字符串(键)与指向 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 技术交流群。

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

发布评论

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

评论(5

找个人就嫁了吧 2024-08-16 13:29:38

尝试liveStrainTable[ MRCA.c_str() ]=firstStrainPtr;。它期望 const char * 作为键值类型,但 MRCA 具有 string 类型。

另一种方法是将 liveStrainTable 更改为:

hash_map< string, Strain *, hash<string>, eqstr > liveStrainTable;

Try liveStrainTable[ MRCA.c_str() ]= firstStrainPtr;. It expects const char * as type of key value, but MRCA has type string.

Another way is to change liveStrainTable to:

hash_map< string, Strain *, hash<string>, eqstr > liveStrainTable;
辞别 2024-08-16 13:29:38

其他人回答了您的直接问题,但我可以建议使用 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.

奈何桥上唱咆哮 2024-08-16 13:29:38

hash_map 不是 STL 的一部分。没有提供 hash 的实现,或者换句话说,hash_map 默认情况下无法对字符串进行哈希处理。您需要自己的哈希函数。尝试

typedef struct {
  size_t operator()( const string& str ) const {
     return __gnu_cxx::__stl_hash_string( str.c_str() );
  }
} strhash;

hash_map< string, Strain *, strhash, eqstr > liveStrainTable;

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:

typedef struct {
  size_t operator()( const string& str ) const {
     return __gnu_cxx::__stl_hash_string( str.c_str() );
  }
} strhash;

hash_map< string, Strain *, strhash, eqstr > liveStrainTable;
泅人 2024-08-16 13:29:38

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()

各自安好 2024-08-16 13:29:38

现在,您的类型不匹配。您正在传递 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 use c_str() to get a char const * from the string, or (far better) change the definition of your hash table to take a string as its key type.

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