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

    1. <small id='aaVSs'></small><noframes id='aaVSs'>

      <tfoot id='aaVSs'></tfoot>

          <bdo id='aaVSs'></bdo><ul id='aaVSs'></ul>
      1. 何时使用 std::size_t?

        时间:2023-08-28
        <legend id='jkG9g'><style id='jkG9g'><dir id='jkG9g'><q id='jkG9g'></q></dir></style></legend>
        <i id='jkG9g'><tr id='jkG9g'><dt id='jkG9g'><q id='jkG9g'><span id='jkG9g'><b id='jkG9g'><form id='jkG9g'><ins id='jkG9g'></ins><ul id='jkG9g'></ul><sub id='jkG9g'></sub></form><legend id='jkG9g'></legend><bdo id='jkG9g'><pre id='jkG9g'><center id='jkG9g'></center></pre></bdo></b><th id='jkG9g'></th></span></q></dt></tr></i><div id='jkG9g'><tfoot id='jkG9g'></tfoot><dl id='jkG9g'><fieldset id='jkG9g'></fieldset></dl></div>
        • <bdo id='jkG9g'></bdo><ul id='jkG9g'></ul>

              <tfoot id='jkG9g'></tfoot>

                <tbody id='jkG9g'></tbody>
              • <small id='jkG9g'></small><noframes id='jkG9g'>

                • 本文介绍了何时使用 std::size_t?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我只是想知道我应该使用 std::size_t 代替 int 来代替循环和其他东西吗?例如:

                  I'm just wondering should I use std::size_t for loops and stuff instead of int? For instance:

                  #include <cstdint>
                  
                  int main()
                  {
                      for (std::size_t i = 0; i < 10; ++i) {
                          // std::size_t OK here? Or should I use, say, unsigned int instead?
                      }
                  }

                  一般来说,关于何时使用 std::size_t 的最佳实践是什么?

                  In general, what is the best practice regarding when to use std::size_t?

                  推荐答案

                  一个好的经验法则是,对于任何你需要在循环条件中与自然是 std::size_t 本身.

                  A good rule of thumb is for anything that you need to compare in the loop condition against something that is naturally a std::size_t itself.

                  std::size_t 是任何 sizeof 表达式的类型,并且保证能够在 C++ 中表达任何对象(包括任何数组)的最大大小.通过扩展,它还保证对于任何数组索引都足够大,因此它是数组上按索引循环的自然类型.

                  std::size_t is the type of any sizeof expression and as is guaranteed to be able to express the maximum size of any object (including any array) in C++. By extension it is also guaranteed to be big enough for any array index so it is a natural type for a loop by index over an array.

                  如果您只是计算一个数字,那么使用保存该数字的变量类型或 intunsigned int 可能更自然(如果足够大)因为这些应该是机器的自然尺寸.

                  If you are just counting up to a number then it may be more natural to use either the type of the variable that holds that number or an int or unsigned int (if large enough) as these should be a natural size for the machine.

                  这篇关于何时使用 std::size_t?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:什么时候 uint8_t ≠ unsigned char? 下一篇:C++ 中的 size_t 和 int 有什么区别?

                  相关文章

                • <legend id='8X1Zy'><style id='8X1Zy'><dir id='8X1Zy'><q id='8X1Zy'></q></dir></style></legend>

                  1. <small id='8X1Zy'></small><noframes id='8X1Zy'>

                      <bdo id='8X1Zy'></bdo><ul id='8X1Zy'></ul>
                    <tfoot id='8X1Zy'></tfoot>

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