C++ Array Definition & Assignment
In C++, defining and initializing an array can be achieved through the following steps:
- To define an array: To start, you need to define an array variable, specifying the type and size of the array. For example, to define an integer array, you can use the following syntax:
int arr[5]; // 定义一个包含5个整型元素的数组
- Assigning array elements: Next, you can assign values to each element of the array using indexing. For example, you can assign values to the array defined above using the following syntax:
arr[0] = 1; // 为数组第一个元素赋值为1
arr[1] = 2; // 为数组第二个元素赋值为2
arr[2] = 3; // 为数组第三个元素赋值为3
arr[3] = 4; // 为数组第四个元素赋值为4
arr[4] = 5; // 为数组第五个元素赋值为5
You can also initialize the elements of an array when defining it, for example:
int arr[5] = {1, 2, 3, 4, 5}; // 定义并初始化一个包含5个整型元素的数组
By using the above method, you can define and assign values to an array.