C++ array
C++ supports the array data structure, which can store a fixed-size sequential collection of elements of the same type. An array is used to store a series of data, but it is often thought of as a series of variables of the same type.
The declaration of an array is not to declare individual variables, such as number0, number1,..., number99, but to declare an array variable, such as numbers, and then use numbers[0], numbers[1] ,..., numbers[99] to represent individual variables. Specific elements in an array can be accessed via index.
All arrays are composed of consecutive memory locations. The lowest address corresponds to the first element, and the highest address corresponds to the last element.
Declaring an array
To declare an array in C++, you need to specify the type of elements and the number of elements, as shown below:
type arrayName [ arraySize ];
This is called a one-dimensional array. arraySize must be an integer constant greater than zero, type can be any valid C++ data type. For example, to declare an array balance of type double containing 10 elements, the declaration statement is as follows:
double balance[10];
Now balance is an available array that can hold 10 numbers of type double.
Initializing an array
In C++, you can initialize an array one by one, or you can use an initialization statement, as shown below:
double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
Values between curly brackets { } The number of cannot be greater than the number of elements we specified in square brackets [ ] when declaring the array.
If you omit the size of the array, the size of the array is the number of elements during initialization. So if:
double balance[] = {1000.0, 2.0, 3.4, 17.0, 50.0};
you will create an array that is exactly the same as the one created in the previous instance. The following is an example of assigning a value to an element in an array:
balance[4] = 50.0;
The above statement assigns the value of the fifth element in the array to 50.0. All arrays are indexed with 0 as their first element, also known as the base index, and the last index of the array is the total size of the array minus 1. The following is a graphical representation of the array discussed above:
#Accessing Array Elements
Array elements can be accessed by indexing the array name. The index of the element is enclosed in square brackets, following the array name. For example:
double salary = balance[9];
The above statement will assign the value of the 10th element in the array to the salary variable. The following example uses the above three concepts, namely, array declaration, array assignment, and array access:
#include <iostream> using namespace std; #include <iomanip> using std::setw; int main () { int n[ 10 ]; // n 是一个包含 10 个整数的数组 // 初始化数组元素 for ( int i = 0; i < 10; i++ ) { n[ i ] = i + 100; // 设置元素 i 为 i + 100 } cout << "Element" << setw( 13 ) << "Value" << endl; // 输出数组中每个元素的值 for ( int j = 0; j < 10; j++ ) { cout << setw( 7 )<< j << setw( 13 ) << n[ j ] << endl; } return 0; }
The above program uses the setw() function to format the output. When the above code is compiled and executed, it will produce the following results:
Element Value 0 100 1 101 2 102 3 103 4 104 5 105 6 106 7 107 8 108 9 109
Detailed explanation of arrays in C++
In C++, arrays are very important and we need to know more about arrays details. Listed below are some important concepts related to arrays that C++ programmers must know:
Concept | Description |
---|---|
Multidimensional array | C++ supports multidimensional arrays. The simplest form of a multidimensional array is a two-dimensional array. |
Pointer to array | You can generate a pointer to the first element in an array by specifying the array name without an index. |
Passing an array to a function | You can pass a pointer to an array to a function by specifying the array name without an index. |
Returning arrays from functions | C++ allows returning arrays from functions. |