C++ 数组初始化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1920430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ array initialization
提问by Eli
is this form of intializing an array to all 0s
这是将数组初始化为全 0 的这种形式吗
char myarray[ARRAY_SIZE] = {0}
supported by all compilers? ,
char myarray[ARRAY_SIZE] = {0}
所有编译器都支持?,
if so, is there similar syntax to other types? for example
如果是这样,是否有与其他类型类似的语法?例如
bool myBoolArray[ARRAY_SIZE] = {false}
回答by AnT
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 almostthe case in C++.
是的,所有 C++ 编译器都支持这种形式的初始化。它是 C++ 语言的一部分。实际上,它是从 C 语言来到 C++ 的一个习语。在 C 语言中= { 0 }
是一个惯用的通用零初始值设定项。这在 C++ 中也几乎是这样。
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
数组,你真的不需要不同的“语法”。0
也可以作为bool
类型的初始值设定项,所以
bool myBoolArray[ARRAY_SIZE] = { 0 };
is guaranteed to initialize the entire array with false
. As well as
保证用 初始化整个数组false
。也
char* myPtrArray[ARRAY_SIZE] = { 0 };
in guaranteed to initialize the whole array with null-pointers of type char *
.
保证用类型为 的空指针初始化整个数组char *
。
If you believe it improves readability, you can certainly use
如果您认为它提高了可读性,您当然可以使用
bool myBoolArray[ARRAY_SIZE] = { false };
char* myPtrArray[ARRAY_SIZE] = { nullptr };
but the point is that = { 0 }
variant gives you exactlythe same result.
但关键是该= { 0 }
变体为您提供了完全相同的结果。
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
但是,在 C++ 中= { 0 }
可能不适用于所有类型,例如枚举类型,它不能用 integer 初始化0
。但是 C++ 支持更短的形式
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.
即只是一对空的{}
. 这将默认初始化任何类型的数组(假设元素允许默认初始化),这意味着对于基本(标量)类型,整个数组将被正确初始化为零。
回答by incises
Note that the '=' is optional in C++11 universal initialization syntax, and it is generally considered better style to write :
请注意, '=' 在 C++11 通用初始化语法中是可选的,通常认为编写以下样式更好:
char myarray[ARRAY_SIZE] {0}
回答by Tabish
You can declare the array in C++ in these type of ways.
If you know the array size then you should declare the array for:
integer: int myArray[array_size];
Double: double myArray[array_size];
Char and string : char myStringArray[array_size];
The difference between char and string is as follows
您可以通过这些类型的方式在 C++ 中声明数组。如果您知道数组大小,那么您应该声明数组为: integer: int myArray[array_size];
Double: double myArray[array_size];
Char and string : char myStringArray[array_size];
char 和 string 之间的区别如下
char myCharArray[6]={'a','b','c','d','e','f'};
char myStringArray[6]="abcdef";
If you don't know the size of array then you should leave the array blank like following.
如果您不知道数组的大小,那么您应该将数组留空,如下所示。
integer: int myArray[array_size];
整数: int myArray[array_size];
Double: double myArray[array_size];
双倍的: double myArray[array_size];
回答by jasonline
Yes, I believe it should work and it can also be applied to other data types.
是的,我相信它应该可以工作,也可以应用于其他数据类型。
For class arrays though, if there are fewer items in the initializer list than elements in the array, the default constructor is used for the remaining elements. If no default constructor is defined for the class, the initializer list must be complete — that is, there must be one initializer for each element in the array.
但是对于类数组,如果初始化列表中的项少于数组中的元素,则默认构造函数用于其余元素。如果没有为类定义默认构造函数,则初始化器列表必须是完整的——也就是说,数组中的每个元素都必须有一个初始化器。