C++_ series of self-study Courses _ _7_ Class _ array _ C + + Primer fourth Edition

Source: Internet
Author: User

When it comes to arrays, we should all be familiar with the concept of arrays in C, Pascal, Java and other languages. Support for arrays is also provided in C + +. An array is simply a bunch of the same

A collection of data type objects. Here are two points to take: the same data type, collection.

One, array

1. Definition

An array is a composite data type that consists of the type name, identifier, and array dimensions of an object that is stored by the arrays. The type name specifies the type of object that can be stored in the array, and identifiers are used to identify

Array, which is used to refer to the array elements in the program, which specifies how many array elements the array can hold.

The definition of the array is as follows:

Type-name    array identifier [n];  

Here are some examples of arrays:

int iarray[10]; Defines an array that stores an int object, which can store an array of 10 integral types.

2. Visit

Array elements can be accessed through subscript operators, just like vectors.

For example, define:

Char charray[3];

Charray[0] = ' A ';

Charray[1] = ' B ';

CHARRAY[2] = ' + ';

By using the subscript operator to access the elements of the array, the subscript is calculated from 0, so the largest subscript = = number of elements of the array-1; In use is to be careful not to cross the border.

Array out-of-bounds can cause some strange errors.

3. Initialization of array elements

The elements in the array can be initialized at the same time as the array is defined. As shown below:

int iarray[3] = {10,2,3}; This defines an array of integers with three elements and three elements initialized to 10, 2, 3. i.e. iarray[0] = 10;

IARRAY[1] = 2;  IARRAY[2] = 3; Here are the 3 numbers enclosed in {}, called the initial list.

If you provide an initial list, you can omit the length of the definition array. For example:

int iarray[] = {10, 2, 3}; This defines an integer array with an array length of 3.

Char charray[] = "Volcanol"; This defines a character array with an array length of 9, where there are only 8 characters and why there are 9 elements, because

String literals automatically add a null character to the system by default, which is ' + '; In order to add an element that stores ' \ A ', there will eventually be 9 array elements.

There is another situation:

int iarray[5] = {1, 2, 3}; This defines an array that can hold 5 elements, but the initialization list provides only 3 elements and 2 fewer elements, and there are

Iarray[0] = 1, iarray[1] = 2; IARRAY[2] = 3, iarray[3] = 0, iarray[4] = 0;

Important: If you do not provide an initialization value, then the value of the array element is the same as the normal variable

1. If the global array is a built-in type, the initialization session is 0.

2. If the local array is a built-in type, it will get a random value.

3. If the array is a class type, the default constructor is called to initialize. If there is no default constructor, a display initialization list must be provided for the array element.

4. Access to arrays

As mentioned earlier, arrays are somewhat similar to vectors and can be accessed by subscript.

Here's an example:

intMain () {intiarray[Ten];  for(intI=0; I! =sizeof(IArray)/sizeof(int); i++) Iarray[i]=i;  for(intI=0; I! =sizeof(IArray)/sizeof(int); i++) cout<<iArray[i]<<Endl; return 0;}

This allows the elements of the IArray array to be assigned a value of 0 to 9, and the execution result is as follows:

[Email protected] cpp_src]# g++ test.cpp [[email protected] cpp_src]#. /A. Out012345678 9

Important, when using the sizeof operator, be aware that sizeof asks for the number of bytes occupied, not the length of the array.

Exp:

int Main () {    cout<<sizeof(L'a') <<Endl;     return 0 ;}

The program execution results are as follows:

[Email protected] cpp_src]# g++ test.cpp [[email protected] cpp_src]#. /a. Out4

Therefore, the following program sections need to be noted:

int iarray[];  for (int0sizeof(iarray); ++i)  // wrong understanding of the sizeof operator, thus to the wrong result, this place needs attention  = i;

  

5, array subscript out of bounds

int Main () {    string strarray[2];    strarray[0"volcanol";    strarray[1"Hi,";    cout<<strarray[1]<<strarray[2]<<Endl;     return 0 ;}

Here we cross-border access, the output is as follows:

 [[email protected] cpp_src]# g++ Test.cpp [[email protected] cpp_src]# . /a.out   Hi,di? Li?h4?? h?\  4  i?~{l|?????? 3 ?? 5 ? _4????? [email protected]?? 3  <i?+ 4 ? y?? Y?? Y?? Y?? Y?? y?≥æ¿≥æ¿p≥æ¿?≥æ¿┌≥æ¿/≠æ¿_≠æ¿?≠æ¿ì≠æ¿æ≠濶£æ¿î£æ¿à£æ¿û£æ¿ Æ¿  Æ¿6 Æ¿g Æ¿z æ¿? æ¿? æ¿  Æ¿á Æ¿î Æ¿ú Æ¿<æ¿^æ¿┐æ¿? Æ¿?    濳æ¿îæ¿øæ¿ô !      Рÿó«?                                  4? ??  {J?i686[[em Ail protected] cpp_src]#  

This is read out, if it is written, it will lead to a program error, if the use of some mechanisms to bypass the system's restrictions from user space to jump to the kernel space, it can cause

The kernel is not stable.

6. Arrays cannot be directly assigned and copied

Unlike vectors, arrays cannot be assigned and copied directly to the whole, which requires attention.

There is one more issue to be aware of.

int Main () {    string strarray[2];        strarray[0"volcanol";    strarray[1"Hi,";    cout<<strarray[1]<<strarray[0]<<endl;     return 0 ;}

The execution results are as follows:

[[email protected] cpp_src]#./a . Out hi,volcanol

Here we see that strarray[0] can be assigned to a string literal as a whole, because the subscript operator of the array returns an Lvalue, and the value of the left

The data type is string so it can be assigned as a whole.

The array is talking about this, and there are some other things about the array to be continued ...

C++_ series of self-study Courses _ _7_ Class _ array _ C + + Primer fourth Edition

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.