hash_map用法不懂啊各位大神~
hash_map用法不懂啊,各位大神救命啊~~~
// test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <hash_map>
using namespace std;
class TwoWayMessage
{
public:
TwoWayMessage(double nodeToFactorMsgs[], double factorToNodeMsgs[])
{
for(int i = 0; i < 2; i++)
{
nodeToFactorMsgs_[i] = nodeToFactorMsgs[i];
factorToNodeMsgs_[i] = factorToNodeMsgs[i];
}
}
~TwoWayMessage() {}
double * getNodeToFactorMessage() { return nodeToFactorMsgs_;}
double * getFactorToNodeMessage() { return factorToNodeMsgs_;}
private:
double nodeToFactorMsgs_[2];
double factorToNodeMsgs_[2];
};
class LinkId
{
public:
LinkId(int predId, int superPredId, int superClauseId, int predIndex)
{
predId_ = predId;
superPredId_ = superPredId;
superClauseId_ = superClauseId;
predIndex_ = predIndex;
dirty_ = true;
}
~LinkId(){}
int getPredId() { return predId_;}
int getSuperPredId() { return superPredId_; }
int getSuperClauseId() { return superClauseId_; }
int getPredIndex() { return predIndex_; }
size_t getHashCode()
{
if (!dirty_) return hashCode_;
hashCode_ = predId_;
hashCode_ = hashCode_*31 + superPredId_;
hashCode_ = hashCode_*31 + superClauseId_;
hashCode_ = hashCode_*31 + predIndex_;
dirty_ = false;
return hashCode_;
}
private:
int predId_;
int superPredId_;
int superClauseId_;
int predIndex_;
size_t hashCode_;
bool dirty_;
};
class HashLinkId
{
public:
size_t operator()(LinkId * const & lid) const
{
return lid->getHashCode();
}
};
class EqualLinkId
{
public:
bool operator()(LinkId * const & lid1, LinkId * const & lid2)
{
return (lid1->getPredId() == lid2->getPredId() &&
lid1->getSuperPredId() == lid2->getSuperPredId() &&
lid1->getSuperClauseId() == lid2->getSuperClauseId() &&
lid1->getPredIndex() == lid2->getPredIndex());
}
};
typedef hash_map<LinkId*, TwoWayMessage*, HashLinkId, EqualLinkId> LinkIdToTwoWayMessageMap;
LinkIdToTwoWayMessageMap mymap;
int main(int argc, char* argv[])
{
LinkId const*id;
mymap.find(id);
return 0;
}
编译报错:
1>------ 已启动全部重新生成: 项目: test, 配置: Debug Win32 ------
1>生成启动时间为 2012/3/6 15:31:14。
1>_PrepareForClean:
1> 正在删除文件“.\Debug\test.lastbuildstate”。
1>InitializeBuildStatus:
1> 正在对“.\Debug\test.unsuccessfulbuild”执行 Touch 任务。
1>ClCompile:
1> StdAfx.cpp
1> test.cpp
1>f:\learntool\vs2010\stlport-5.2.1\stlport\stl\_hashtable.h(502): error C3848: 具有类型“const EqualLinkId”的表达式会丢失一些 const-volatile 限定符以调用“bool EqualLinkId::operator ()(LinkId *const &,LinkId *const &)”
1> f:\learntool\vs2010\stlport-5.2.1\stlport\stl\_hashtable.h(511): 参见对正在编译的函数 模板 实例化“stlp_std::priv::_Slist_iterator<_Tp,_Traits> stlp_std::hashtable<_Val,_Key,_HF,stlp_std::priv::_HashMapTraitsT<_Tp>,_ExK,_EqK,_All>::_M_find<_KT>(const _KT &) const”的引用
1> with
1> [
1> _Tp=stlp_std::pair<LinkId *const ,TwoWayMessage *>,
// test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <hash_map>
using namespace std;
class TwoWayMessage
{
public:
TwoWayMessage(double nodeToFactorMsgs[], double factorToNodeMsgs[])
{
for(int i = 0; i < 2; i++)
{
nodeToFactorMsgs_[i] = nodeToFactorMsgs[i];
factorToNodeMsgs_[i] = factorToNodeMsgs[i];
}
}
~TwoWayMessage() {}
double * getNodeToFactorMessage() { return nodeToFactorMsgs_;}
double * getFactorToNodeMessage() { return factorToNodeMsgs_;}
private:
double nodeToFactorMsgs_[2];
double factorToNodeMsgs_[2];
};
class LinkId
{
public:
LinkId(int predId, int superPredId, int superClauseId, int predIndex)
{
predId_ = predId;
superPredId_ = superPredId;
superClauseId_ = superClauseId;
predIndex_ = predIndex;
dirty_ = true;
}
~LinkId(){}
int getPredId() { return predId_;}
int getSuperPredId() { return superPredId_; }
int getSuperClauseId() { return superClauseId_; }
int getPredIndex() { return predIndex_; }
size_t getHashCode()
{
if (!dirty_) return hashCode_;
hashCode_ = predId_;
hashCode_ = hashCode_*31 + superPredId_;
hashCode_ = hashCode_*31 + superClauseId_;
hashCode_ = hashCode_*31 + predIndex_;
dirty_ = false;
return hashCode_;
}
private:
int predId_;
int superPredId_;
int superClauseId_;
int predIndex_;
size_t hashCode_;
bool dirty_;
};
class HashLinkId
{
public:
size_t operator()(LinkId * const & lid) const
{
return lid->getHashCode();
}
};
class EqualLinkId
{
public:
bool operator()(LinkId * const & lid1, LinkId * const & lid2)
{
return (lid1->getPredId() == lid2->getPredId() &&
lid1->getSuperPredId() == lid2->getSuperPredId() &&
lid1->getSuperClauseId() == lid2->getSuperClauseId() &&
lid1->getPredIndex() == lid2->getPredIndex());
}
};
typedef hash_map<LinkId*, TwoWayMessage*, HashLinkId, EqualLinkId> LinkIdToTwoWayMessageMap;
LinkIdToTwoWayMessageMap mymap;
int main(int argc, char* argv[])
{
LinkId const*id;
mymap.find(id);
return 0;
}
编译报错:
1>------ 已启动全部重新生成: 项目: test, 配置: Debug Win32 ------
1>生成启动时间为 2012/3/6 15:31:14。
1>_PrepareForClean:
1> 正在删除文件“.\Debug\test.lastbuildstate”。
1>InitializeBuildStatus:
1> 正在对“.\Debug\test.unsuccessfulbuild”执行 Touch 任务。
1>ClCompile:
1> StdAfx.cpp
1> test.cpp
1>f:\learntool\vs2010\stlport-5.2.1\stlport\stl\_hashtable.h(502): error C3848: 具有类型“const EqualLinkId”的表达式会丢失一些 const-volatile 限定符以调用“bool EqualLinkId::operator ()(LinkId *const &,LinkId *const &)”
1> f:\learntool\vs2010\stlport-5.2.1\stlport\stl\_hashtable.h(511): 参见对正在编译的函数 模板 实例化“stlp_std::priv::_Slist_iterator<_Tp,_Traits> stlp_std::hashtable<_Val,_Key,_HF,stlp_std::priv::_HashMapTraitsT<_Tp>,_ExK,_EqK,_All>::_M_find<_KT>(const _KT &) const”的引用
1> with
1> [
1> _Tp=stlp_std::pair<LinkId *const ,TwoWayMessage *>,