简体   繁体   English

使用Curly Brackets(Braces)围绕变量C ++

[英]Use of Curly Brackets (Braces) Around a variable C++

I'm learning 2D arrays in my programming class. 我正在编程类中学习2D数组。 My teacher used something without explaining it and I was curious why we used it. 我的老师用了一些东西而没有解释它,我很好奇为什么我们用它。 Since it has to do with a symbol I'm not sure how to google or search for it, as these symbols are used in the search itself. 由于它与符号有关,我不确定如何谷歌或搜索它,因为这些符号用于搜索本身。 Anyways the code was this: 无论如何代码是这样的:

int small[26]= {0}, large[26]={0}, i;

Why are the curly braces needed around the 0's? 为什么0周围需要花括号?

The program this code is a part of examines a file and looks for each letter of the alphabet and counts them individually. 该代码的程序是检查文件的一部分,并查找字母表中的每个字母并单独计算它们。

It could be written even simpler 它写得更简单

int small[26]= {}, large[26]={}, i;

The curly brackets means an initializer list in this case of arrays. 花括号表示在这种情况下数组的初始化列表。

Let assume for example that you want to define an array with elements 1, 2, 3, 4, 5. 假设您要定义一个包含元素1,2,3,4,5的数组。

You could write 你可以写

int a[5];

a[0] = 1;
a[1] = 2;
a[2] = 3;
a[3] = 4;
a[4] = 5;

However C++ allows to assign elements of an array when it is defined. 但是,C ++允许在定义数组时分配它们。 The equivalent record will look 相同的记录将是

int a[5] = { 1, 2, 3, 4, 5 };

If there are initializers less than the size of the array then remaining elements will be initialized by zeroes. 如果初始化程序小于数组的大小,则剩余的元素将由零初始化。 For example 例如

int a[5] = { 1, 2 };

In this case a[0] will be equal tp 1 a[1] will be equal to 2 and all other elements will be equal to 0. 在这种情况下,a [0]将等于tp 1 a [1]将等于2并且所有其他元素将等于0。

You may omit the size of an array. 您可以省略数组的大小。 For example 例如

int a[] = { 1, 2, 3, 4, 5 };

In this case the compiler will allocate as many elements of the array as there are initializers in the initializer list. 在这种情况下,编译器将分配与初始化程序列表中的初始化程序一样多的数组元素。

Record (valid only in C++. In C it is not allowed) 记录(仅在C ++中有效。在C中不允许)

int a[5] = {};

is equivalent to 相当于

int a[5] = { 0 };

that is all elements of the array will be initialized by 0. In the last record the first element is initialized explicitly by zero and all other elements are also initialized by zero because their initializers in the initializer list were not specified. 也就是说,数组的所有元素都将被初始化为0.在最后一条记录中,第一个元素由零显式初始化,所有其他元素也由零初始化,因为初始化程序列表中的初始化程序未指定。

The same way you can initialize also scalar objects. 与初始化标量对象的方式相同。 For example 例如

int x = { 10 }; int x = {10};

The only difference that for scalar objects you can specify only one initializer. 标量对象的唯一区别是您只能指定一个初始化程序。 You even may write without the assignment operator 您甚至可以在没有赋值运算符的情

int x { 10 };

You also can write 你也可以写

int x {};

In this case x will be initialized by 0. 在这种情况下,x将被初始化为0。

int Array[26]= {0};

Initializes all elements in that array to 0. 将该数组中的所有元素初始化为0。

As BryanChen mentions below: 正如BryanChen在下面提到的那样:

Note: int Array[26]= {1}; will initialize first element to 1 and others to 0. 

By default, regular arrays of local scope (for example, those declared within a function) are left uninitialized. 默认情况下,本地范围的常规数组(例如,在函数内声明的数组)保持未初始化。 This means that none of its elements are set to any particular value; 这意味着它的所有元素都没有设置为任何特定值; their contents are undetermined at the point the array is declared. 它们的内容在声明数组时未确定。

But the elements in an array can be explicitly initialized to specific values when it is declared, by enclosing those initial values in braces {}. 但是,通过将这些初始值括在大括号{}中,数组中的元素可以在声明时显式初始化为特定值。

Note that 注意

int array [constant] = {};

Default initializes all values. 默认初始化所有值。 The default initializer for int is 0. int的默认初始值设定项为0。

int small[26]= {0};

Sets the first number to 0 and default initializes the rest. 将第一个数字设置为0,默认初始化其余数字。

In general: 一般来说:

int array [constant] = {value initialize all indices listed here, 
                        the rest are default initialized};

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

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