⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 transfer_char_to_key.cpp

📁 VC7.1中如果将char*作为key的话需要重载关于char*的hash_compare函数 可是我怎么都没法搞定 只好将char*转为int
💻 CPP
字号:
#include <iostream>
#include <hash_map>
#include <string>

using namespace std;
using namespace stdext;
template <>
bool hash_compare<int>::operator( ) ( 
      const int & s1,
      const int & s2
   ) const
{
   char *ss1 = (char*)(s1);
   char *ss2 = (char*)(s2);
   if (strcmp(ss1,ss2) == 0)
      return false;
   else 
      return true;
}

int main(int argc,char** argv )
{       
   hash_map <int, int, hash_compare <int > > hm;
   hash_map <int, int, hash_compare <int > >::iterator itp1;        
   char* s1 = strdup("111");
   char* s2 = strdup("222");
   hm[(int)(s1)] = 111; 
   hm[(int)(s2)] = 222;  
   char* key1 = strdup("111");  
   itp1 = hm.find((int)(key1));
   if (itp1 == hm.end()) {
      cout<<"not found "<<key1<<endl;
   }
   else  {
      cout<<"found :"<<key1<<" == "<<itp1->second<<endl;
   }   
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -