Getting to understand C ++ Phase II Basics (6) - C ++ array

Outline

C ++ supports an array of data structures that can store a sequence of fixed size elements of the same type of collection. An array is used to store a series of data, but it is often considered to be a series of variables of the same type.

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

All arrays are composed of contiguous memory locations. The lowest address corresponds to the first element, the highest address corresponding to the last element.

Array definition

To declare an array in C ++, and the number of elements need to specify the type of element, such as: type arrayName [arraySize];

This is called one-dimensional array. arraySize must be an integer constant greater than zero, type may be any valid C ++ data types. For example, to declare a type comprising a double array of 10 elements of balance , such as the declaration: double balance [10]; now balance is available array 10 can accommodate double the number of type.

Features 1: the same for all data types

Feature 2: continuous memory address

Three kinds of an array definition mode

Four kinds of two-dimensional array defined way

Array initialization

In C ++, you can initialize an array individually, it may also be used to initialize a statement, such as: double balance [5] = {1000.0, 2.0, 3.4, 7.0, 50.0};

The number of values ​​between the braces {} is not larger than the number of elements in an array we specify a statement in square brackets []. If omitted the number of size of the array, the array size, compared with the initialization element. Thus, if: double balance [] = {1000.0, 2.0, 3.4, 7.0, 50.0};

Create an array, the array it created in the previous example is exactly the same. The following is an example of an element array assignment is: balance [4] = 50.0;

The above statement is the value of the array elements in the fifth Fu 50.0. All are based on an array index 0 as the first element of which, also called based index, a last index of the array is the total size of the array minus one. Graphical representation:

Access array elements

Array elements can be accessed through an array name indexed. Index of the element is placed in square brackets, followed behind an array of names. For example: double salary = balance [9]; statement sets the values ​​in the array above the first element 10 is assigned to the variable salary. The following statement demonstrates array, the array assignments, access the array:


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

                        One public interest is scanned No. java. More importantly, the little ape is willing to be a friend you programmed the road!

Articles starting address: www.javayihao.top

No public debut: java One

Guess you like

Origin www.cnblogs.com/javayihao/p/11906374.html