简体   繁体   English

C++ 数组初始化

[英]C++ array initialization

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} 

Yes, this form of initialization is supported by all C++ compilers. 是的,所有C ++编译器都支持这种初始化形式。 It is a part of C++ language. 它是C ++语言的一部分。 In fact, it is an idiom that came to C++ from C language. 实际上,它是从C语言到C ++的习惯用语。 In C language = { 0 } is an idiomatic universal zero-initializer . 在C语言中, = { 0 }是惯用的通用零初始化器 This is also almost the case in C++. 在C ++中也几乎就是这种情况。

Since this initalizer is universal, for bool array you don't really need a different "syntax". 由于这个initalizer是通用的,对于bool数组,你并不需要一个不同的“语法”。 0 works as an initializer for bool type as well, so 0可以作为bool类型的初始化器

bool myBoolArray[ARRAY_SIZE] = { 0 };

is guaranteed to initialize the entire array with false . 保证用false初始化整个数组。 As well as 以及

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 exactly the 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 . 但是,在C ++中, = { 0 }可能不适用于所有类型,例如枚举类型,无法使用整数0进行初始化。 But C++ supports the shorter form 但是C ++支持更短的形式

T myArray[ARRAY_SIZE] = {};

ie 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 ++ 11通用初始化语法中是可选的,并且它通常被认为是更好的写入样式:

char myarray[ARRAY_SIZE] {0}

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. 如果没有为类定义默认构造函数,则初始化程序列表必须完整 - 也就是说,数组中的每个元素必须有一个初始化程序。

You can declare the array in C++ in these type of ways. 您可以用这些方式在C ++中声明数组。 If you know the array size then you should declare the array for: integer: int myArray[array_size]; 如果您知道数组大小,那么您应该声明数组:integer: int myArray[array_size]; Double: double myArray[array_size]; Double: double myArray[array_size]; Char and string : char myStringArray[array_size]; 字符串和字符串: char myStringArray[array_size]; The difference between char and string is as follows 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]; integer: int myArray[array_size];

Double: double myArray[array_size]; Double: double myArray[array_size];

Additionally, keep a note of how the array and vector are default initialized if don't initialize them explicitly.此外,如果没有显式初始化arrayvector ,请注意它们是如何默认初始化的。

int main()
{
    bool arr1[3];                   // true  true  true  [here true is SOME garbage value] 
    std::vector<bool> vec1(3);      // false false false

    bool arr2[3] = { true };        // true false false [here true/false are REAL true/false values] 
    vector<bool> vec2(3, true);     // true true  true

    bool arr3[3] = { false };         // false false false
    std::vector<bool> vec3(3, false); // false false false


    int arr4[3];                      // GV GV GV - garbage value
    std::vector<int> vec4(3);         // 0 0 0

    int arr5[3] = { 1 };              // 1 0 0
    std::vector<int> vec5(3, 1);      // 1 1 1

    int arr6[3] = { 0 };              // 0 0 0
    std::vector<int> vec6(3, 0);      // 0 0 0
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM