随笔 - 6  文章 - 8  trackbacks - 0
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿(1)

随笔档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜

刚刚装上VC8,发现VC8现在也支持hash_map了,终于可以不用STLPort了(不是不想用,而是在VC里面用太TNND麻烦了)。不过VC8上的hash-map还是有不爽的地方 1。它定义在stdext namespace 中 2。它需要至少specialize两个template来添加自定义类。。。不过终究还是算不小的的进步啦;D =====================================


============ VC2005 ================
// test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include
#include
#include

struct Key{
  int i;
};


namespace std {
  template <> struct less<::Key>
   : public binary_function<::Key, ::Key, bool>
  {
    bool operator()(const ::Key& _Left, const ::Key& _Right) const
    { // apply operator< to operands
      return (_Left.i < _Right.i);
    }
  };
}

namespace stdext {
  template <> size_t hash_value(const Key &key){
    return hash_value(key.i);
  }
}

int _tmain(int argc, _TCHAR* argv[])
{
  stdext::hash_map testmap; 
  Key k;
  k.i = 1;
  testmap[k] = std::string("hello, world");
  testmap[k] = std::string("hello, again");

  std::cout << "size of map: " << testmap.size() << std::endl;
  return 0;
}

============GCC + STLPort ===========
struct Key {
  ...
};


namespace std {
  template <> struct hash {
    size_t operator()(const Key &key) const {
        ...
    };
  }
}


int main()
{
  ...
}

posted on 2007-04-03 14:01 Neal 阅读(1977) 评论(1)  编辑 收藏 引用

FeedBack:
# re: VC8 的 hash_map 2007-04-05 08:59 
VC7里面一样有啊  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理