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

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

        <bdo id='ZOPVF'></bdo><ul id='ZOPVF'></ul>
      1. <legend id='ZOPVF'><style id='ZOPVF'><dir id='ZOPVF'><q id='ZOPVF'></q></dir></style></legend>

        哨兵节点如何提供优于 NULL 的好处?

        How does a sentinel node offer benefits over NULL?(哨兵节点如何提供优于 NULL 的好处?)
        1. <i id='682T3'><tr id='682T3'><dt id='682T3'><q id='682T3'><span id='682T3'><b id='682T3'><form id='682T3'><ins id='682T3'></ins><ul id='682T3'></ul><sub id='682T3'></sub></form><legend id='682T3'></legend><bdo id='682T3'><pre id='682T3'><center id='682T3'></center></pre></bdo></b><th id='682T3'></th></span></q></dt></tr></i><div id='682T3'><tfoot id='682T3'></tfoot><dl id='682T3'><fieldset id='682T3'></fieldset></dl></div>
            <bdo id='682T3'></bdo><ul id='682T3'></ul>
            • <small id='682T3'></small><noframes id='682T3'>

                <tbody id='682T3'></tbody>

                  <legend id='682T3'><style id='682T3'><dir id='682T3'><q id='682T3'></q></dir></style></legend>
                  <tfoot id='682T3'></tfoot>
                  本文介绍了哨兵节点如何提供优于 NULL 的好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在 哨兵节点维基百科页面上,它说哨兵节点相对于 NULL 的好处是:

                  On the Sentinel Node wikipedia page it says that the benefits of a sentinel node over NULL are :

                  • 提高操作速度
                  • 减少算法代码大小
                  • 增强了数据结构的稳健性(可以说是).

                  我真的不明白对哨兵节点的检查如何更快(或者如何在链表或树中正确实现它们),所以我想这更多是一个两部分的问题:

                  I don't really understand how the checks against a sentinel node would be faster (or how to properly implement them in a linked list or tree), so I suppose this is more of a two part question:

                  1. 是什么让哨兵节点成为比 NULL 更好的设计?
                  2. 您将如何在(例如)列表中实现哨兵节点?

                  推荐答案

                  如果您只是进行简单的迭代而不查看元素中的数据,那么使用哨兵就没有任何优势.

                  There's no advantage with sentinels if you're just doing simple iteration and not looking at the data in the elements.

                  但是,将其用于查找"类型算法时会获得一些实际收益.例如,想象一个链表列表 std::list,您想在其中查找特定值 x.

                  However, there's some real gain when using it for "find" type algorithms. For example, imagine a linked list list std::list where you want to find a specific value x.

                  没有哨兵你会做什么:

                  for (iterator i=list.begin(); i!=list.end(); ++i) // first branch here
                  {
                    if (*i == x) // second branch here
                      return i;
                  }
                  return list.end();
                  

                  但是有了哨兵(当然,end 实际上必须是一个真正的节点……):

                  But with sentinels (of course, end actually has to be a real node for this...):

                  iterator i=list.begin();
                  *list.end() = x;
                  
                  while (*i != x) // just this branch!
                    ++i;
                  
                  return i;
                  

                  您看到不需要额外的分支来测试列表的末尾 - 该值始终保证在那里,因此如果 x,您将自动返回 end() 在您的有效"元素中找不到.

                  You see there's no need for the additional branch to test for the end of the list - the value is always guaranteed to be there, so you will automatically return end() if x cannot be found in your "valid" elements.

                  对于另一个很酷且实际有用的哨兵应用程序,请参阅intro-sort",这是大多数 std::sort 实现中使用的排序算法.它有一个很酷的分区算法变体,它使用哨兵来删除一些分支.

                  For another cool and actually useful application of sentinels, see "intro-sort", which is the sorting algorithm that's used in most std::sort implementations. It has a cool variant of the partition algorithm that uses sentinels to remove a few branches.

                  这篇关于哨兵节点如何提供优于 NULL 的好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Consistent pseudo-random numbers across platforms(跨平台一致的伪随机数)
                  Vary range of uniform_int_distribution(改变uniform_int_distribution的范围)
                  What is a seed in terms of generating a random number?(就生成随机数而言,种子是什么?)
                  Is 1.0 a valid output from std::generate_canonical?(1.0 是 std::generate_canonical 的有效输出吗?)
                  Getting big random numbers in C/C++(在 C/C++ 中获取大随机数)
                  What is the best way to generate random numbers in C++?(在 C++ 中生成随机数的最佳方法是什么?)
                1. <i id='J29yh'><tr id='J29yh'><dt id='J29yh'><q id='J29yh'><span id='J29yh'><b id='J29yh'><form id='J29yh'><ins id='J29yh'></ins><ul id='J29yh'></ul><sub id='J29yh'></sub></form><legend id='J29yh'></legend><bdo id='J29yh'><pre id='J29yh'><center id='J29yh'></center></pre></bdo></b><th id='J29yh'></th></span></q></dt></tr></i><div id='J29yh'><tfoot id='J29yh'></tfoot><dl id='J29yh'><fieldset id='J29yh'></fieldset></dl></div>

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

                          <bdo id='J29yh'></bdo><ul id='J29yh'></ul>

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

                            <tfoot id='J29yh'></tfoot>

                              <tbody id='J29yh'></tbody>