如何使用stdext :: hash_map? [英] How to use stdext::hash_map?

查看:139
本文介绍了如何使用stdext :: hash_map?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想看一个简单的例子,说明如何正确地重写stdext :: hash_compare,以便为我自己的用户定义类型定义一个新的散列函数和比较运算符。我使用Visual C ++(2008)。

I would like to see a simple example of how to override stdext::hash_compare properly, in order to define a new hash function and comparison operator for my own user-defined type. I'm using Visual C++ (2008).

推荐答案

这就是你可以做到的方式。

This is how you can do it

class MyClass_Hasher {
     const size_t bucket_size = 10; // mean bucket size that the container should try not to exceed
     const size_t min_buckets = (1 << 10); // minimum number of buckets, power of 2, >0
     MyClass_Hasher() {
          // should be default-constructible
     }
     size_t operator()(const MyClass &key) {
             size_t hash_value;
             // do fancy stuff here with hash_value
             // to create the hash value. There's no specific
             // requirement on the value.
             return hash_value;
     }

     bool operator()(const MyClass &left, const MyClass &right) {
            // this should implement a total ordering on MyClass, that is
            // it should return true if "left" precedes "right" in the ordering
     }
 };

然后,您可以使用

Then, you can just use

stdext::hash_map my_map<MyClass, MyValue, MyClass_Hasher>

这篇关于如何使用stdext :: hash_map?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆