Latest web development tutorials

C ++ Array

C ++ support forarray data structure that can store a sequence of fixed-size collection of elements of the same type.An array is used to store a range of data, but it is often considered to be a series of variables of the same type.

Declaration is not a declaration of an array of individual variables, such as number0, number1, ..., number99, but declare an array of variables, such as numbers, and then use the numbers [0], numbers [1], ..., numbers [99] to represent a separate variable. Specific array elements can be accessed through an index.

All arrays consists of contiguous memory locations. Lowest address corresponds to the first element, the highest address corresponding to the last element.

Declare an array

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

type arrayName [ arraySize ];

This is called one-dimensional array.arraySize must be an integer greater than zero constant,type can be any valid C ++ data types. For example, to declare an array of type doublebalance contains 10 elements declaration statement is as follows:

double balance[10];

Balanceis now available array can accommodate 10 type double figures.

Array initialization

In C ++, you can initialize the array one by one, you can also use an initial statement, as follows:

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

The number of values ​​between braces {} can not be greater than the number of elements in an array we specify statement in square brackets [].

If you omit the size of the array, the array size when compared to the number of elements to initialize. Thus, if:

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

You will create an array, the array before it with one example being created is exactly the same. Below is an element of an array assignment examples:

balance[4] = 50.0;

The statement said the value of array elements forming the fifth to 50.0. All arrays are 0 as the index of their first element, also known based index, the last index of the array is the total size of the array minus 1. The following are discussed above array of graphical representation:

Array representation

Access array elements

Array elements can be accessed via the array name indexed. Index element is placed within square brackets, followed behind an array of names. E.g:

double salary = balance[9];

The above statement sets the value of the first array of 10 elements of the salary assigned to the variable. The following example uses the above three concepts, namely, declare an array, the array assignments, the 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 usessetw () 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

Detailed arrays in C ++

In C ++, the array is very important, we need to know more details about the array. Listed below are some important concepts associated with an array of C ++ programmers must be clear:

多维数组 C++ 支持多维数组。多维数组最简单的形式是二维数组。
指向数组的指针 您可以通过指定不带索引的数组名称来生成一个指向数组中第一个元素的指针。
传递数组给函数 您可以通过指定不带索引的数组名称来给函数传递一个指向数组的指针。
从函数返回数组 C++ 允许从函数返回数组。