Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将对象插入哈希表(C+;+;)_C++_Hashmap_Sgi - Fatal编程技术网

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

C++ 将对象插入哈希表(C+;+;),c++,hashmap,sgi,C++,Hashmap,Sgi,这是我第一次制作哈希表。我试图将字符串(键)与指向类的对象(数据)的指针相关联 // 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< cons

这是我第一次制作哈希表。我试图将字符串(键)与指向类的对象(数据)的指针相关联

// 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.h
#包括
使用名称空间_gnu_cxx;
结构方程
{
布尔运算符()(常量字符*s1,常量字符*s2)常量
{
返回strcmp(s1,s2)==0;
}
};
...
hash_map,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;
string-MRCA;
对于(int b=0;b
我收到一条错误消息,上面写着“((Simulation*)this)->Simulation::liveStrainTable[MRCA]”中的“operator[]”不匹配。”我还尝试以不同的方式使用“liveStrainTable.insert(…)”,但没有成功


我真的很想在这方面得到一些帮助。我很难理解适用于SGI哈希_映射的语法,这个语法对我来说几乎没有任何澄清。谢谢。

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

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

hash_map< string, Strain *, hash<string>, eqstr > liveStrainTable;
hash\u mapliveStrainTable;

散列映射是以const char*作为键类型定义的,访问时使用std::string作为键。这是两种不同的类型,模板没有为第二种类型生成运算符,因此这是一个错误。使用std::string作为hashmap定义或使用MRCA.c_str()

现在,您的类型不匹配。您正在传递MRCA(一个字符串),其中需要一个
char常量*
。您可以使用
c_str()
从字符串中获取
char const*
,或者(更好地)更改哈希表的定义,将字符串作为其键类型。

其他人回答了您的直接问题,但是我可以建议改用它吗?它是STL的下一个版本,所有主要编译器都支持它。

哈希映射不是STL的一部分。没有为散列提供实现,或者换句话说,默认情况下,散列映射不能散列字符串。您需要自己的哈希函数。T

尝试:

typedef结构{
size\u t运算符()(常量字符串和str)常量{
返回_gnu_cxx::_stl_hash_string(str.c_str());
}
}斯特拉什;
hash_mapliveStrainTable;

这有什么好处?不使用带有两个前导下划线的名称空间:)太棒了!非常感谢。“liveStrain[MRCA]=firststraintptr;”似乎起作用了——我现在就开始编写所有其他操作。我迫不及待地想知道散列映射/无序映射是STL的一部分,并且可以处理字符串。“我迫不及待地想知道散列映射/无序映射是STL的一部分”,实际上它将成为标准库的一部分。90年代末,STL的大部分(但不是全部)成为std库的一部分。std库还有很多其他部分(字符串、流…),所以“STL”不是“标准库”的同义词。而且,实际上很可能您的std库已经在命名空间
std::tr1
中提供了
unordered\u map
。TR1(技术报告1)列出了许多本应成为下一个标准一部分的库,并鼓励供应商提供它们。它被广泛采用。如果您的std库没有它,那么在http::www.boost.org的boost库中有一个实现。
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;