<tfoot id='8b94T'></tfoot>
    1. <small id='8b94T'></small><noframes id='8b94T'>

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

      1. 指针作为映射 C++ STL 中的键

        时间:2024-05-12
          <tbody id='Ffmv5'></tbody>

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

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

                • <small id='Ffmv5'></small><noframes id='Ffmv5'>

                  本文介绍了指针作为映射 C++ STL 中的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个问题,当在地图中用作键时,如何处理指向自定义对象的指针.更具体地说,如果我定义

                  I have a question on how pointers to a custom object are handled when used as Keys in an map. More specifically if I define

                  std::map< CustomClass*, int > foo;
                  

                  默认的 C++ 实现可以处理这些指针吗?还是我需要定义一个自定义比较器函数来处理它?一般来说,使用指向对象的指针作为键是一种好习惯吗?

                  Would the default C++ implementation work to handle these pointers? Or do I need to define a custom comparator function to handle it? In general, is it good practice to use pointers to objects as keys?

                  推荐答案

                  默认实现会比较指针存储的地址,因此不同的对象会被视为不同的键.但是,不会考虑对象的逻辑状态.例如,如果您使用 std::string * 作为键,两个不同的 std::string 对象具有相同的 "Hello" 文本> 将被视为不同的键!(当按他们的地址存储在地图中时)

                  The default implementation will compare the addresses stored by the pointers, so different objects will be considered as different keys. However, the logical state of the object will not be considered. For example, if you use std::string * as the key, two different std::string objects with the same text of "Hello" would be considered a different key! (When stored in the map by their addresses)

                  只要您理解上述重要区别,就可以使用指针作为键.

                  It's ok to use pointers as keys so long as you understand the important difference above.

                  这篇关于指针作为映射 C++ STL 中的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:迭代器的类型:输出 vs. 输入 vs. 前向 vs. 随机访问迭代器 下一篇:根据 C++ 规范,std::set 迭代顺序是否总是升序?

                  相关文章

                • <small id='foQ9a'></small><noframes id='foQ9a'>

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

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