hash_map string key 忽略大小写

来源:互联网 发布:ubuntu开机自启动程序 编辑:程序博客网 时间:2024/06/05 16:14
  1. hash_map string key 忽略大小写。
  2. 转自网络,没有测试是否可用

  3. #include <hash_map>
  4. #include <string>
  5. #include <algorithm>
  6. #include <cctype>
  7.  
  8. inline size_t __stl_hash_string(constchar* __s)
  9. {
  10.     unsigned long __h = 0;
  11.     for ( ; *__s; ++__s)
  12.         __h = 5*__h +tolower(*__s);
  13.  
  14.     return size_t(__h);
  15. }
  16.  
  17. template<>
  18. struct stlport::hash<stlport::string>
  19. {
  20.     size_t operator()(const stlport::string& __s) const
  21.     {
  22.         return __stl_hash_string(__s.c_str());
  23.     }
  24. };
  25.  
  26. template<>
  27. struct stlport::equal_to<stlport::string>
  28.     : public stlport::binary_function<stlport::string ,stlport::string ,bool>
  29. {
  30.     bool operator()(const stlport::string& __x, const stlport::string& __y) const
  31.     {
  32.         return !_stricmp(__x.c_str() ,__y.c_str());
  33.     }
  34. };
  35.  
  36. int _tmain(int argc, _TCHAR* argv[])
  37. {
  38.     stlport::hash_map<stlport::string ,int> map;
  39.  
  40.     map.insert(stlport::make_pair("Test" , 123));
  41.  
  42.     stlport::hash_map<stlport::string ,int>::iteratoriter = map.find("teSt");
  43.     if(iter !=map.end())
  44.         printf("Found!\n");
  45.  
  46.     return 0;
  47. }

原创粉丝点击