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

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

      <legend id='krlNb'><style id='krlNb'><dir id='krlNb'><q id='krlNb'></q></dir></style></legend>
      <tfoot id='krlNb'></tfoot>
    1. 为什么标准迭代器范围是 [begin, end) 而不是 [begin, end]?

      时间:2024-05-12
        • <bdo id='LJ1EI'></bdo><ul id='LJ1EI'></ul>

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

                <tbody id='LJ1EI'></tbody>

                <legend id='LJ1EI'><style id='LJ1EI'><dir id='LJ1EI'><q id='LJ1EI'></q></dir></style></legend>

              1. <tfoot id='LJ1EI'></tfoot>
                <i id='LJ1EI'><tr id='LJ1EI'><dt id='LJ1EI'><q id='LJ1EI'><span id='LJ1EI'><b id='LJ1EI'><form id='LJ1EI'><ins id='LJ1EI'></ins><ul id='LJ1EI'></ul><sub id='LJ1EI'></sub></form><legend id='LJ1EI'></legend><bdo id='LJ1EI'><pre id='LJ1EI'><center id='LJ1EI'></center></pre></bdo></b><th id='LJ1EI'></th></span></q></dt></tr></i><div id='LJ1EI'><tfoot id='LJ1EI'></tfoot><dl id='LJ1EI'><fieldset id='LJ1EI'></fieldset></dl></div>
                本文介绍了为什么标准迭代器范围是 [begin, end) 而不是 [begin, end]?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                为什么标准将 end() 定义为末尾之后,而不是实际末尾?

                Why does the Standard define end() as one past the end, instead of at the actual end?

                推荐答案

                最容易的论据是 迪杰斯特拉本人:

                • 您希望范围的大小是一个简单的差异 end   begin;

                • You want the size of the range to be a simple difference end − begin;

                当序列退化为空序列时,包括下限更自然",而且因为替代方案(排除下限)需要存在前一-the-beginning"哨兵值.

                including the lower bound is more "natural" when sequences degenerate to empty ones, and also because the alternative (excluding the lower bound) would require the existence of a "one-before-the-beginning" sentinel value.

                您仍然需要证明为什么从零而不是从 1 开始计数,但这不是您问题的一部分.

                You still need to justify why you start counting at zero rather than one, but that wasn't part of your question.

                当您有任何类型的算法处理对基于范围的构造的多个嵌套或迭代调用时,[begin, end) 约定背后的智慧会一次又一次地得到回报,这些构造是自然链接的.相比之下,使用双闭范围会导致不合一的代码以及极其令人不快和嘈杂的代码.例如,考虑一个分区 [n0, n1)[n1, n2)[n2,n3).另一个例子是标准的迭代循环 for (it = begin; it != end; ++it),它运行 end - begin 次.如果两端都包含在内,相应的代码的可读性就会大大降低.并想象一下您将如何处理空范围.

                The wisdom behind the [begin, end) convention pays off time and again when you have any sort of algorithm that deals with multiple nested or iterated calls to range-based constructions, which chain naturally. By contrast, using a doubly-closed range would incur off-by-ones and extremely unpleasant and noisy code. For example, consider a partition [n0, n1)[n1, n2)[n2,n3). Another example is the standard iteration loop for (it = begin; it != end; ++it), which runs end - begin times. The corresponding code would be much less readable if both ends were inclusive – and imagine how you'd handle empty ranges.

                最后,我们还可以很好地论证为什么计数应该从零开始:根据我们刚刚建立的范围的半开放约定,如果给定一个 N 元素的范围(比如枚举数组的成员),那么 0 是自然的开始",这样您就可以将范围写为 [0, N),没有任何尴尬的偏移或更正.

                Finally, we can also make a nice argument why counting should start at zero: With the half-open convention for ranges that we just established, if you are given a range of N elements (say to enumerate the members of an array), then 0 is the natural "beginning" so that you can write the range as [0, N), without any awkward offsets or corrections.

                简而言之:事实上,我们在基于范围的算法中没有随处看到数字 1,这是 [begin, end) 约定的直接结果和动机.

                In a nutshell: the fact that we don't see the number 1 everywhere in range-based algorithms is a direct consequence of, and motivation for, the [begin, end) convention.

                这篇关于为什么标准迭代器范围是 [begin, end) 而不是 [begin, end]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:STL 中的双端队列到底是什么? 下一篇:如果在从头到尾迭代时在 map 元素上调用 erase() 会发生什么?

                相关文章

              2. <i id='5HlPo'><tr id='5HlPo'><dt id='5HlPo'><q id='5HlPo'><span id='5HlPo'><b id='5HlPo'><form id='5HlPo'><ins id='5HlPo'></ins><ul id='5HlPo'></ul><sub id='5HlPo'></sub></form><legend id='5HlPo'></legend><bdo id='5HlPo'><pre id='5HlPo'><center id='5HlPo'></center></pre></bdo></b><th id='5HlPo'></th></span></q></dt></tr></i><div id='5HlPo'><tfoot id='5HlPo'></tfoot><dl id='5HlPo'><fieldset id='5HlPo'></fieldset></dl></div>
              3. <legend id='5HlPo'><style id='5HlPo'><dir id='5HlPo'><q id='5HlPo'></q></dir></style></legend>

                <small id='5HlPo'></small><noframes id='5HlPo'>

              4. <tfoot id='5HlPo'></tfoot>
                  <bdo id='5HlPo'></bdo><ul id='5HlPo'></ul>