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

  3. <legend id='Uekbl'><style id='Uekbl'><dir id='Uekbl'><q id='Uekbl'></q></dir></style></legend>

    1. <tfoot id='Uekbl'></tfoot>

      C++ 数组初始化

      C++ array initialization(C++ 数组初始化)
    2. <tfoot id='7ia5B'></tfoot>
          • <legend id='7ia5B'><style id='7ia5B'><dir id='7ia5B'><q id='7ia5B'></q></dir></style></legend>

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

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

                <tbody id='7ia5B'></tbody>
                <bdo id='7ia5B'></bdo><ul id='7ia5B'></ul>
              • 本文介绍了C++ 数组初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                是将数组初始化为全 0 的这种形式

                is this form of intializing an array to all 0s

                char myarray[ARRAY_SIZE] = {0} 所有编译器都支持吗?,

                char myarray[ARRAY_SIZE] = {0} supported by all compilers? ,

                如果是,是否有与其他类型类似的语法?例如

                if so, is there similar syntax to other types? for example

                bool myBoolArray[ARRAY_SIZE] = {false} 
                

                推荐答案

                是的,所有 C++ 编译器都支持这种形式的初始化.它是 C++ 语言的一部分.实际上,它是从 C 语言来到 C++ 的一个习语.在 C 语言中 = { 0 } 是惯用的通用零初始化器.这也是几乎在 C++ 中的情况.

                Yes, this form of initialization is supported by all C++ compilers. It is a part of C++ language. In fact, it is an idiom that came to C++ from C language. In C language = { 0 } is an idiomatic universal zero-initializer. This is also almost the case in C++.

                因为这个初始化器是通用的,对于 bool 数组,你真的不需要不同的语法".0 也可以作为 bool 类型的初始化器,所以

                Since this initalizer is universal, for bool array you don't really need a different "syntax". 0 works as an initializer for bool type as well, so

                bool myBoolArray[ARRAY_SIZE] = { 0 };
                

                保证用 false 初始化整个数组.还有

                is guaranteed to initialize the entire array with false. As well as

                char* myPtrArray[ARRAY_SIZE] = { 0 };
                

                保证用 char * 类型的空指针初始化整个数组.

                in guaranteed to initialize the whole array with null-pointers of type char *.

                如果你认为它提高了可读性,你当然可以使用

                If you believe it improves readability, you can certainly use

                bool myBoolArray[ARRAY_SIZE] = { false };
                char* myPtrArray[ARRAY_SIZE] = { nullptr };
                

                但重点是 = { 0 } 变体给你完全相同的结果.

                but the point is that = { 0 } variant gives you exactly the same result.

                然而,在 C++ 中,= { 0 } 可能不适用于所有类型,例如枚举类型,它不能用整数 0 初始化.但是 C++ 支持更短的形式

                However, in C++ = { 0 } might not work for all types, like enum types, for example, which cannot be initialized with integral 0. But C++ supports the shorter form

                T myArray[ARRAY_SIZE] = {};
                

                即只是一对空的 {}.这将默认初始化任何类型的数组(假设元素允许默认初始化),这意味着对于基本(标量)类型,整个数组将被正确初始化为零.

                i.e. just an empty pair of {}. This will default-initialize an array of any type (assuming the elements allow default initialization), which means that for basic (scalar) types the entire array will be properly zero-initialized.

                这篇关于C++ 数组初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                Constructor initialization Vs assignment(构造函数初始化 Vs 赋值)
                Is a `=default` move constructor equivalent to a member-wise move constructor?(`=default` 移动构造函数是否等同于成员移动构造函数?)
                Has the new C++11 member initialization feature at declaration made initialization lists obsolete?(声明时新的 C++11 成员初始化功能是否使初始化列表过时了?)
                Order of constructor call in virtual inheritance(虚继承中构造函数调用的顺序)
                How to use sfinae for selecting constructors?(如何使用 sfinae 选择构造函数?)
                Initializing a union with a non-trivial constructor(使用非平凡的构造函数初始化联合)
                  <tbody id='drmby'></tbody>
                <i id='drmby'><tr id='drmby'><dt id='drmby'><q id='drmby'><span id='drmby'><b id='drmby'><form id='drmby'><ins id='drmby'></ins><ul id='drmby'></ul><sub id='drmby'></sub></form><legend id='drmby'></legend><bdo id='drmby'><pre id='drmby'><center id='drmby'></center></pre></bdo></b><th id='drmby'></th></span></q></dt></tr></i><div id='drmby'><tfoot id='drmby'></tfoot><dl id='drmby'><fieldset id='drmby'></fieldset></dl></div>
              • <tfoot id='drmby'></tfoot>

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

                  • <bdo id='drmby'></bdo><ul id='drmby'></ul>

                        • <legend id='drmby'><style id='drmby'><dir id='drmby'><q id='drmby'></q></dir></style></legend>