transfer_char_to_key.cpp

来自「VC7.1中如果将char*作为key的话需要重载关于char*的hash_co」· C++ 代码 · 共 38 行

CPP
38
字号
#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 + =
减小字号Ctrl + -
显示快捷键?