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

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

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

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

      <tfoot id='C1BES'></tfoot>
      1. 什么是半开范围和离终值

        时间:2023-08-03

        <small id='7wtxZ'></small><noframes id='7wtxZ'>

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

              <tbody id='7wtxZ'></tbody>
              <bdo id='7wtxZ'></bdo><ul id='7wtxZ'></ul>

              • <legend id='7wtxZ'><style id='7wtxZ'><dir id='7wtxZ'><q id='7wtxZ'></q></dir></style></legend>
                  本文介绍了什么是半开范围和离终值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  这些术语在 C++ 中是什么意思?

                  What do these terminologies mean in C++?

                  1. 关闭 end

                  2. 半开范围 - [begin, off_the_end)

                  我在阅读 for 循环时遇到了它们.

                  I came across them while reading about for loops.

                  推荐答案

                  半开范围是包含第一个元素但不包括最后一个元素的范围.

                  A half-open range is one which includes the first element, but excludes the last one.

                  范围 [1,5) 是半开的,由值 1、2、3 和 4 组成.

                  The range [1,5) is half-open, and consists of the values 1, 2, 3 and 4.

                  结束"或结束"指的是序列末尾之后的元素,它的特殊之处在于允许迭代器指向它(但您可能不会查看实际值,因为它不存在)

                  "off the end" or "past the end" refers to the element just after the end of a sequence, and is special in that iterators are allowed to point to it (but you may not look at the actual value, because it doesn't exist)

                  例如,在以下代码中:

                  char arr[] = {'a', 'b', 'c', 'd'};
                  
                  char* first = arr
                  char* last = arr + 4;
                  

                  first 现在指向数组的第一个元素,而 last 指向数组末尾的一个元素.我们可以指向数组末尾的一个(但不能两个过去),但我们不能尝试访问该位置的元素:

                  first now points to the first element of the array, while last points one past the end of the array. We are allowed to point one past the end of the array (but not two past), but we're not allowed to try to access the element at that position:

                  // legal, because first points to a member of the array
                  char firstChar = *first;
                  // illegal because last points *past* the end of the array
                  char lastChar = *last;
                  

                  我们的两个指针 firstlast 共同定义了一个范围,即它们之间的所有元素.

                  Our two pointers, first and last together define a range, of all the elements between them.

                  如果是半开区间,则包含first指向的元素,以及中间的所有元素,但不包含last指向的元素(这很好,因为它实际上并不指向有效元素)

                  If it is a half open range, then it contains the element pointed to by first, and all the elements in between, but not the element pointed to by last (which is good, because it doesn't actually point to a valid element)

                  在 C++ 中,所有标准库算法都在这种半开范围内运行.例如,如果我想将整个数组复制到其他位置 dest,我会这样做:

                  In C++, all the standard library algorithms operate on such half open ranges. For example, if I want to copy the entire array to some other location dest, I do this:

                  std::copy(first, last, dest)
                  

                  一个简单的 for 循环通常遵循类似的模式:

                  A simple for-loop typically follows a similar pattern:

                  for (int i = 0; i < 4; ++i) {
                      // do something with arr[i]
                  }
                  

                  这个循环从 0 到 4,但不包括结束值,所以覆盖的索引范围是半开,特别是 [0, 4)

                  This loop goes from 0 to 4, but it excludes the end value, so the range of indices covered is half-open, specifically [0, 4)

                  这篇关于什么是半开范围和离终值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:特殊的简单随机数发生器 下一篇:即使浮点数不精确,Excel 如何成功舍入?

                  相关文章

                  最新文章

                    <bdo id='KDeUh'></bdo><ul id='KDeUh'></ul>
                  <tfoot id='KDeUh'></tfoot>

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

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