首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

hash_map用法不懂啊各位大神~

2012-04-15 
hash_map用法不懂啊,各位大神救命啊~~~// test.cpp : Defines the entry point for the console applicati

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 *>,


1> _Traits=stlp_std::_Nonconst_traits<stlp_std::pair<LinkId *const ,TwoWayMessage *>>,
1> _Val=stlp_std::pair<LinkId *const ,TwoWayMessage *>,
1> _Key=LinkId *,
1> _HF=HashLinkId,
1> _ExK=stlp_std::priv::_Select1st<stlp_std::pair<LinkId *const ,TwoWayMessage *>>,
1> _EqK=EqualLinkId,
1> _All=stlp_std::allocator<stlp_std::pair<LinkId *const ,TwoWayMessage *>>,
1> _KT=const LinkId *
1> ]
1> f:\learntool\vs2010\stlport-5.2.1\stlport\stl\_hash_map.h(195): 参见对正在编译的函数 模板 实例化“stlp_std::priv::_Ht_iterator<_BaseIte,_Traits> stlp_std::hashtable<_Val,_Key,_HF,stlp_std::priv::_HashMapTraitsT<_Tp>,_ExK,_EqK,_All>::find<_KT>(const _KT &)”的引用
1> with
1> [
1> _BaseIte=stlp_std::priv::_Slist_iterator<stlp_std::pair<LinkId *const ,TwoWayMessage *>,stlp_std::_Nonconst_traits<stlp_std::pair<LinkId *const ,TwoWayMessage *>>>,
1> _Traits=stlp_std::priv::_NonLocalHashMapTraitsT<stlp_std::pair<LinkId *const ,TwoWayMessage *>>,
1> _Val=stlp_std::pair<LinkId *const ,TwoWayMessage *>,
1> _Key=LinkId *,
1> _HF=HashLinkId,
1> _Tp=stlp_std::pair<LinkId *const ,TwoWayMessage *>,
1> _ExK=stlp_std::priv::_Select1st<stlp_std::pair<LinkId *const ,TwoWayMessage *>>,
1> _EqK=EqualLinkId,
1> _All=stlp_std::allocator<stlp_std::pair<LinkId *const ,TwoWayMessage *>>,
1> _KT=const LinkId *
1> ]
1> e:\学习盘\学习\程序\mln\test01\test.cpp(100): 参见对正在编译的函数 模板 实例化“stlp_std::priv::_Ht_iterator<_BaseIte,_Traits> stlp_std::hash_map<_Key,_Tp,_HashFcn,_EqualKey>::find<const LinkId*>(const _KT &)”的引用
1> with
1> [
1> _BaseIte=stlp_std::priv::_Slist_iterator<stlp_std::pair<LinkId *const ,TwoWayMessage *>,stlp_std::_Nonconst_traits<stlp_std::pair<LinkId *const ,TwoWayMessage *>>>,
1> _Traits=stlp_std::priv::_NonLocalHashMapTraitsT<stlp_std::pair<LinkId *const ,TwoWayMessage *>>,
1> _Key=LinkId *,
1> _Tp=TwoWayMessage *,
1> _HashFcn=HashLinkId,
1> _EqualKey=EqualLinkId,
1> _KT=const LinkId *
1> ]
1>
1>生成失败。
1>
1>已用时间 00:00:06.10
========== 全部重新生成: 成功 0 个,失败 1 个,跳过 0 个 ==========
救命啊~~~~

[解决办法]
一帖未结 不尊重回答者 ~ ~ 


[解决办法]
http://www.cplusplus.com
这里找例子吧。
[解决办法]
如果不是使用的vs系列的话,那么编译通过的话只需要将LinkId const*id;的const去掉就没什么问题。

热点排行