C++ array

C++ Supportarray, ..., numbers

Declaring an array does not declare individual variables such as number0, number1, ..., number99; rather, it declares an array variable such as numbers, and then uses numbers

All arrays consist of contiguous memory locations. The lowest address corresponds to the first element, and the highest address corresponds to the last element.

Declaring arrays

In C++, to declare an array, you need to specify the type of the elements and the number of elements, as shown below:

type arrayName [ arraySize ];

This is called a one-dimensional array.arraySizeMust be an integer constant greater than zero,typeIt can be any valid C++ data type. For example, to declare an array of 10 elements of type double,balance, the declaration statement is as follows:

double balance[10];

Nowbalanceis a usable array that can hold 10 numbers of type double.

Initializing arrays

In C++, you can initialize an array element by element, or with a single initialization statement, as follows:

double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};

The number of values between braces { } 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 array size will be the number of elements in the initializer. Therefore, if:

double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0};

You will create an array that is exactly the same as the array created in the previous example. The following is an example of assigning a value to an element in the array:

balance[4] = 50.0;

The above statement assigns the value 50.0 to the fifth element of the array. All arrays use 0 as the index of their first element, also called the base index; the last index of an array is the total size minus 1. The following is a graphical representation of the array discussed above:

数组表示

Access array elements

Array elements can be accessed by adding an index to the array name. The index of an element is placed inside 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 three concepts mentioned above, namely, declaring an array, array assignment, and accessing an array:

Example

#include <iostream> using namespace std; #include <iomanip> using std::setw; int main () { int n[ 10 ]; // n is an array of 10 integers // Initialize array elements for ( int i = 0; i < 10; i++ ) { n[ i ] = i + 100; // Set element i to i + 100 } cout << "Element" << setw( 13 ) << "Value" << endl; // output the value of each element in the array for ( int j = 0; j < 10; j++ ) { cout << setw( 7 )<< j << setw( 13 ) << n[ j ] << endl; } return 0; }

The above program usessetw() functionto format the output. When the above code is compiled and executed, it produces 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

C++ Arrays in Detail

In C++, arrays are very important. We need to learn more details about arrays. The following lists some important concepts related to arrays that C++ programmers must be clear about:

ConceptsDescription
Multidimensional arraysC++ supports multidimensional arrays. The simplest form of a multidimensional array is the two-dimensional array.
Pointer to an ArrayYou can generate a pointer to the first element of an array by specifying the array name without an index.
Passing Arrays to FunctionsYou can pass a pointer to an array to a function by specifying the array name without an index.
Returning Arrays from FunctionsC++ allows returning an array from a function.
other extensions