• <tfoot id='SiNPd'></tfoot>

    1. <legend id='SiNPd'><style id='SiNPd'><dir id='SiNPd'><q id='SiNPd'></q></dir></style></legend>

      <small id='SiNPd'></small><noframes id='SiNPd'>

      • <bdo id='SiNPd'></bdo><ul id='SiNPd'></ul>

      1. <i id='SiNPd'><tr id='SiNPd'><dt id='SiNPd'><q id='SiNPd'><span id='SiNPd'><b id='SiNPd'><form id='SiNPd'><ins id='SiNPd'></ins><ul id='SiNPd'></ul><sub id='SiNPd'></sub></form><legend id='SiNPd'></legend><bdo id='SiNPd'><pre id='SiNPd'><center id='SiNPd'></center></pre></bdo></b><th id='SiNPd'></th></span></q></dt></tr></i><div id='SiNPd'><tfoot id='SiNPd'></tfoot><dl id='SiNPd'><fieldset id='SiNPd'></fieldset></dl></div>

      2. C ++中的简单哈希图实现

        时间:2024-08-13
        <i id='50kgo'><tr id='50kgo'><dt id='50kgo'><q id='50kgo'><span id='50kgo'><b id='50kgo'><form id='50kgo'><ins id='50kgo'></ins><ul id='50kgo'></ul><sub id='50kgo'></sub></form><legend id='50kgo'></legend><bdo id='50kgo'><pre id='50kgo'><center id='50kgo'></center></pre></bdo></b><th id='50kgo'></th></span></q></dt></tr></i><div id='50kgo'><tfoot id='50kgo'></tfoot><dl id='50kgo'><fieldset id='50kgo'></fieldset></dl></div>

            <bdo id='50kgo'></bdo><ul id='50kgo'></ul>
            • <tfoot id='50kgo'></tfoot>

              <small id='50kgo'></small><noframes id='50kgo'>

              • <legend id='50kgo'><style id='50kgo'><dir id='50kgo'><q id='50kgo'></q></dir></style></legend>
                    <tbody id='50kgo'></tbody>

                1. 本文介绍了C ++中的简单哈希图实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我对 C++ 比较陌生.在 Java 中,我很容易实例化和使用 hashmap.我想知道如何在 C++ 中以简单的方式实现它,因为我看到了许多不同的实现,但对我来说它们都不简单.

                  I'm relatively new to C++. In Java, it's easy for me to instantiate and use a hashmap. I'd like to know how to do it in a simple way in C++, since I saw many different implementations and none of them looked simple to me.

                  推荐答案

                  大多数编译器应该为你定义std::hash_map;在即将到来的 C++0x 标准中,它将成为标准库的一部分,如 std::unordered_map.STL Page 是相当标准的.如果您使用 Visual Studio,Microsoft 有一个页面就可以了.

                  Most compilers should define std::hash_map for you; in the coming C++0x standard, it will be part of the standard library as std::unordered_map. The STL Page on it is fairly standard. If you use Visual Studio, Microsoft has a page on it.

                  如果你想使用你的类作为值,而不是作为键,那么你不需要做任何特别的事情.所有原始类型(像 intcharbool 甚至 char * 之类的东西)都应该正常工作"hash_map 中的键.但是,对于其他任何事情,您都必须定义自己的散列和相等函数,然后编写将它们包装在一个类中的函子".

                  If you want to use your class as the value, not as the key, then you don't need to do anything special. All primitive types (things like int, char, bool and even char *) should "just work" as keys in a hash_map. However, for anything else you will have to define your own hashing and equality functions and then write "functors" that wrap them in a class.

                  假设你的类被称为 MyClass 并且你已经定义了:

                  Assuming your class is called MyClass and you have already defined:

                  size_t MyClass::HashValue() const { /* something */ }
                  bool MyClass::Equals(const MyClass& other) const { /* something */ }
                  

                  您需要定义两个仿函数来将这些方法包装在对象中.

                  You will need to define two functors to wrap those methods in objects.

                  struct MyClassHash {
                    size_t operator()(const MyClass& p) const {
                      return p.HashValue();
                    }
                  };
                  
                  struct MyClassEqual {
                    bool operator()(const MyClass& c1, const MyClass& c2) const {
                      return c1.Equals(c2);
                    }
                  };
                  

                  并将您的 hash_map/hash_set 实例化为:

                  And instantiate your hash_map/hash_set as:

                  hash_map<MyClass, DataType, MyClassHash, MyClassEqual> my_hash_map;
                  hash_set<MyClass, MyClassHash, MyClassEqual> my_hash_set;
                  

                  之后一切都应该按预期工作.

                  Everything should work as expected after that.

                  这篇关于C ++中的简单哈希图实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:C++ 中的 map 与 hash_map 下一篇:c++设置容器的问题

                  相关文章

                  <i id='R9Luw'><tr id='R9Luw'><dt id='R9Luw'><q id='R9Luw'><span id='R9Luw'><b id='R9Luw'><form id='R9Luw'><ins id='R9Luw'></ins><ul id='R9Luw'></ul><sub id='R9Luw'></sub></form><legend id='R9Luw'></legend><bdo id='R9Luw'><pre id='R9Luw'><center id='R9Luw'></center></pre></bdo></b><th id='R9Luw'></th></span></q></dt></tr></i><div id='R9Luw'><tfoot id='R9Luw'></tfoot><dl id='R9Luw'><fieldset id='R9Luw'></fieldset></dl></div>

                2. <legend id='R9Luw'><style id='R9Luw'><dir id='R9Luw'><q id='R9Luw'></q></dir></style></legend>
                3. <tfoot id='R9Luw'></tfoot>

                  <small id='R9Luw'></small><noframes id='R9Luw'>

                      • <bdo id='R9Luw'></bdo><ul id='R9Luw'></ul>