<tfoot id='wNW8I'></tfoot>

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

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

      <legend id='wNW8I'><style id='wNW8I'><dir id='wNW8I'><q id='wNW8I'></q></dir></style></legend>
      • <bdo id='wNW8I'></bdo><ul id='wNW8I'></ul>

      1. C++ stl unordered_map 实现,参考有效性

        C++ stl unordered_map implementation, reference validity(C++ stl unordered_map 实现,参考有效性)

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

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

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

                <legend id='twLrx'><style id='twLrx'><dir id='twLrx'><q id='twLrx'></q></dir></style></legend>
                1. 本文介绍了C++ stl unordered_map 实现,参考有效性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  对于 std::mapstd::tr1::unordered_map,我从标准中看到:

                  For both std::map and std::tr1::unordered_map, I see from the standard that:

                  对 unordered_map 容器中元素的引用在所有情况,即使在重新哈希后也是如此.

                  References to elements in the unordered_map container remain valid in all cases, even after a rehash.

                  他们是如何做到这一点的(实施方面)?他们是否将所有条目维护为一种链表,然后哈希表只存储指向元素的指针?

                  How are they doing that (implementation-wise)? Are they maintaining all the entries as a kind of linked list and then the hash-table just stores pointers to the elements?

                  推荐答案

                  是的,涉及到链表,虽然不像你建议的那样.

                  Yes, linked lists are involved, although not quite in the way you suggest.

                  2011 标准说(23.2.5 第 8 段),无序关联容器的元素被组织到桶中.具有相同哈希码的键出现在同一个桶中."

                  The 2011 standard says (23.2.5 para 8), "The elements of an unordered associative container are organized into buckets. Keys with the same hash code appear in the same bucket."

                  在每个桶内,元素都在一个链表中(每个桶都有一个单独的列表,而不是整个容器的一个大列表).当容器被重新散列时,元素将被分配到新的存储桶,但指向每个元素的指针仍然有效.每个新存储桶中的链表由指向该存储桶中结束的现有元素的指针组装而成.

                  Within each bucket, the elements are in a linked list (a separate list for each bucket, not one big list for the whole container). When the container is rehashed, the elements will be assigned to new buckets, but the pointers to each element remain valid. The linked list in each new bucket is assembled from pointers to the existing elements that end up in that bucket.

                  迭代器因重新散列而失效,因为迭代器需要保存指向元素及其存储桶的指针(因此它可以从一个存储桶的最后一个元素步进到下一个存储桶的第一个元素).元素指针仍然有效,因此现有的指针和对元素的引用仍然有效,但桶指针因重新散列而无效,因此迭代器不可用.

                  Iterators are invalidated by a rehash, since an iterator needs to hold pointers to both the element and its bucket (so it can be stepped from the last element of one bucket to the first element of the next). The element pointer remains valid, so existing pointers and references to an element are still valid, but the bucket pointer is invalidated by a rehash, so the iterator isn't usable.

                  (这也是为什么无序容器只支持前向迭代器,而不是有序关联容器支持的双向迭代器.每个桶的元素都在一个单向链表中,所以你不能通过它们向后退步.)

                  (This is also why unordered containers only support forward iterators, instead of the bidirectional iterators supported by the ordered associative containers. The elements of each bucket are in a singly linked list, so you can't step backwards through them.)

                  这篇关于C++ stl unordered_map 实现,参考有效性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  C++: Is it possible to use a reference as the value in a map?(C++:是否可以使用引用作为映射中的值?)
                  Where ampersand quot;amp;quot; can be put when passing argument by reference?(其中符号“amp;通过引用传递参数时可以放置吗?)
                  Why can a non-const reference parameter be bound to a temporary object?(为什么可以将非常量引用参数绑定到临时对象?)
                  What is a dangling reference?(什么是悬空引用?)
                  C++ reference changes when push_back new element to std::vector(当 push_back 新元素到 std::vector 时,C++ 引用发生变化)
                  Why does std::forward have two overloads?(为什么 std::forward 有两个重载?)
                  <i id='xO4gh'><tr id='xO4gh'><dt id='xO4gh'><q id='xO4gh'><span id='xO4gh'><b id='xO4gh'><form id='xO4gh'><ins id='xO4gh'></ins><ul id='xO4gh'></ul><sub id='xO4gh'></sub></form><legend id='xO4gh'></legend><bdo id='xO4gh'><pre id='xO4gh'><center id='xO4gh'></center></pre></bdo></b><th id='xO4gh'></th></span></q></dt></tr></i><div id='xO4gh'><tfoot id='xO4gh'></tfoot><dl id='xO4gh'><fieldset id='xO4gh'></fieldset></dl></div>

                  <legend id='xO4gh'><style id='xO4gh'><dir id='xO4gh'><q id='xO4gh'></q></dir></style></legend>
                  1. <small id='xO4gh'></small><noframes id='xO4gh'>

                      <bdo id='xO4gh'></bdo><ul id='xO4gh'></ul>
                        <tbody id='xO4gh'></tbody>

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