Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

An array of C+


May 11, 2021 C++


Table of contents


An array of C+

C++ supports the Array data structure, which can store a sequential collection of elements of the same type at a fixed size. An array is used to store a series of data, but it is often considered a series of variables of the same type.

The declaration of an array is not to declare a single variable, such as number0, number1、...、number99, but rather to declare an array variable, such as numbers, and then to represent a separate variable using numbers 、...、 . Specific elements in an array can be accessed through an index.

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

Declare the array

To declare an array in C+, you need to specify the type of element and the number of elements, as follows:

type arrayName [ arraySize ];

This is called a one-dimensional array. must be an integer constant greater than zero, and type can be any valid type of C+ data. For example, to declare an array of 10 elements of double type double, declare the following statement:

double balance[10];

Balance is now an available array that can hold 10 numbers of double type.

Initialize the array

You can initialize the array one by one in C+, or you can use an initialization statement, as follows:

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

The number of values between braces cannot be greater than the number of elements we specify in square brackets when declaring an array.

If you omit the size of the array, the size of the array is the number of elements at the time of initialization. Therefore, 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. Here's an example of assigning a value to an element in an array:

balance[4] = 50.0;

The statement above assigns the value of the fifth element in the array to 50.0. A ll 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 view of the array discussed above:

An array of C+

Access array elements

Array elements can be accessed by indexing the array name. T he index of the element is placed in square brackets, behind the array name. For example:

double salary = balance[9];

The statement above assigns the value of the 10th element in the array to the salary variable. The following example uses the three concepts above, namely, declaring arrays, array assignments, accessing arrays:

#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 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

The array in C+ is detailed

Arrays are very important in C++ and we need to know more about arrays. Here are some important concepts related to arrays that programmers must be aware of:

concept describe
Multidimensional Arrays C ++ supports multi-dimensional arrays.The simplest form of multidimensional arrays is a two-dimensional array.
Pointer to array You can generate a pointer to the first element in an array by specifying an array name without an index.
Pass array to function You can deliver a pointer to the array by specifying an array name without an index.
Returns an array from the function C ++ allows the array to be returned from the function.