Chapter 4: Arrays in C++

Chapter 4: Arrays in C++

Arrays in C++

An array is a collection of elements of the same type in C++. Through arrays, we can easily access and manipulate multiple data of the same type. This article will discuss in depth the concept of arrays in C++, array declaration, array initialization, array access and some common array operations, and help you better understand and use arrays through rich code examples and practical cases.

Array declaration and initialization

In C++, the declaration of an array needs to specify the element type and the array name. Here is the declaration of a simple integer array:

int numbers[5];

In the above code, we declared an numbersinteger array called , which contains 5 elements. Array indices start at 0, so numbers[0]to numbers[4]represent the first to fifth elements of the array, respectively.

We can also initialize an array using an initializer list:

int numbers[] = {
    
    2, 4, 6, 8, 10};

In the above code, we declare and initialize an integer array numberswith 5 elements. We don't need to specify the array size, the compiler will automatically determine the array size according to the initialization value.

Array access and modification

Accessing elements of an array by index is very simple. Use square brackets and index (i.e., array subscript) operators to access or modify specific elements of an array. Here is an example:

int numbers[] = {
    
    2, 4, 6, 8, 10};

cout << numbers[0] << endl; // 输出第一个元素:2
numbers[2] = 12; // 修改第三个元素的值为12
cout << numbers[2] << endl; // 输出修改后的值:12

In the above code, we created an array of integers numbersand initialized it. Then, use numbers[0]to read the value of the first element, and assign the new value numbers[2]to to modify the value of the third element. Finally, the output is "2" and "12".

Loop through the array

Loop constructs are often used to iterate over all elements in an array for processing or manipulation. C++ provides a variety of looping constructs such as forloops, whileloops, and do-whileloops.

Here's an forexample that uses a loop to iterate over an array and calculate the sum:

int numbers[] = {
    
    2, 4, 6, 8, 10};
int sum = 0;

for (int i = 0; i < 5; i++) {
    
    
    sum += numbers[i];
}

cout << "数组元素的总和是:" << sum << endl;

In the above code, we declared a sumvariable named to hold the sum of the array elements. Then, use fora loop to go through the entire array and add each element to sumit. Finally, the output is "The sum of the array elements is: 30".

actual case

Let's look at a more complex example, combining multidimensional arrays and array operations:

const int ROWS = 3;
const int COLS = 4;

int matrix[ROWS][COLS];

// 填充二维数组
for (int i = 0; i < ROWS; i++) {
    
    
    for (int j = 0; j < COLS; j++) {
    
    
        matrix[i][j] = i + j;
    }
}

// 输出二维数组的元素
cout << "二维数组的元素:" << endl;
for (int i = 0; i < ROWS; i++) {
    
    
    for (int j = 0; j < COLS; j++) {
    
    
        cout << matrix[i][j] << " ";
    }
    cout << endl;
}

In the above code, we declare a matrixtwo-dimensional array of integers named , and use two nested loops to initialize each element to the sum of the corresponding row and column indices. Then, use two nested loops to iterate through and output the entire 2D array.

The output is as follows:

二维数组的元素:
0 1 2 3 
1 2 3 4 
2 3 4 5

Guess you like

Origin blog.csdn.net/qq_51447496/article/details/132241155