Hello, C + + (12) How to manage multiple types of data of the same nature? 3.6 Arrays

Source: Internet
Author: User
Tags array definition

3.6 Arrays

Having learned the previous basic data types, we are now able to define individual variables to represent individual data. For example, we can use the int type to define the variable to represent the bus 216 way, can use the float type to define the variable to represent the tomato 3.5 yuan a catty.

However, in addition to a single isolated data, the real world is another type of bulk data. For example, the salary of all employees of a company, the data type is the same (all int type), the same nature (all of the employee's salary), the number is very large (tens of thousands of employees wages). And often form a meaningful set of data (employee pay). For this type of bulk data, it is clearly not feasible to define a single variable to represent it.

For this reason, C + + provides structured data types such as arrays to express bulk data, which organizes the data into a sequence of data. Let the data sit in rows. Eating fruit, greatly facilitates the processing of batch data.

Declaration and initialization of 3.6.1 arrays

In C + +, the method that defines an array is similar to the way that a variable is defined, but the variable name becomes the array name. After the array name, we use the brackets "[]" to elicit a constant representing the number of data elements in the array.

Its detailed syntax forms such as the following:

Data type array name [number of constants] [number constants] ...;

, the data type represents the type of this series of bulk data.

Example. We want to define an array that can hold the wages of multiple employees, and each employee's payroll data can be represented by the int type data. The data type of the entire array is the int type, and the array name is usually an identifier that indicates the meaning of the data in the array. Here, the data in the array is the employee's salary, so we can use Arrsalary as the array name.

, arr indicates that this is an array, whereas salary indicates that the data in the array is wages; The number constants in parentheses after the array name represent the number of batches of data in this series.

Example. There are 100,000 employees in this company, and we need to keep 100,000 payroll data in the array, so this number is naturally 100000. It is also important to note that this number constant must be greater than 0 and must be constant.

Based on the above analysis, we are able to define an array to hold 100,000 employee payroll data:

// save an array of 100,000 employee salaries int arrsalary[100000];

At the same time as the array is defined. You can also initialize an array with "{}".

Like what:

// to define and initialize an array int narray[51,2,3,4,5 };  

This line of code defines an integer array of length 5 at the same time, using "{}" to assign 1, 2, 3, 4, 5 respectively to the array of 5 elements, to complete the initialization of the array. Of course. Assume that you do not need to assign an initial value to all the data in the array. You can also assign values only to the preceding elements of an array. The remaining data that does not specify the initial value. will be assigned a value of 0 or the default initial value for such data.

Like what:

// given the initial value of the first 6 elements in the array, the remaining 94 data is assigned a value of 0 int nbigarray[] = {-542543 };

Although we are able to use "{}" to assign an initial value to an array element at the same time that it is defined. But arrays often have more data elements. It is often impractical to use the assignment of all the data in the "{}" complete. In many other cases, we use "{}" to assign all elements in the array to 0, which completes the 0 operation before the array is used. Like what:

// assigns a value of 0 to all elements in the Nbigarray array int nbigarray[0 };

Know a lot of other: multidimensional arrays

The brackets "[]" in the array definition are used to determine the number of dimensions of the array.

After the array name there are several "[]" means that this is a several-dimensional array, and the number of dimensions of an array, often represents the number of data in the classification. Example. We want to show the results of all students in a school, we often first put all the students in grades according to grade three grade, and then each grade can be divided into 10 classes according to the class. And each class has 30 students.

Thus, after three classifications, we were able to use a three-dimensional array to preserve the results of all students in a school:

// record a three-dimensional array of student scores int arrscore[3[] [+];
Use of 3.6.2 arrays

Once the array is defined, we have more than one variable and can reference the data elements in the array to perform the operation.

To access the individual data in the array. We do this by giving an array subscript in the brackets after the array name.

The so-called array subscript, which represents the location of the data to be interviewed in the array.

Note that the subscript that represents the location of the data is the beginning of the 0 count. Example. In the arrsalary array that we defined earlier to record employee wages. The first data is the boss's salary, and we can read and write access to the first data in the following ways:

// The first data in the array represents the boss's salary, with subscript 0 representing the first data of the array // The assignment is 1, which means the boss's salary is 1arrsalary[01;//  read the first data in the array and output the boss's salary cout< <" the Boss's salary is:"<<arrsalary[0]<<endl;

As we can see here, we can read and write the first data in the array as well as read and write ordinary variables, given the subscript 0 in the brackets after the array name. And so on, to access the second data in the array. Array subscript should be 1, if you want to access the nth data, the subscript should be n-1. Like what:

// The second data in the array represents the boss's salary, giving the 1 interview arrsalary[199999; // in turn, each employee's salary arrsalary[2; // ... ..

When using the array subscript. Another thing to note is that the subscript must be greater than or equal to 0, which is less than the number constant when the array is defined. In simple terms. An array of length n, with a range of values for the subscript [0. N-1]. Assume that the subscript value exceeds this range. The memory area outside of the array will be interviewed. Causes the array to access the cross-border error, the light causes the data read and write error, serious even causes the program to crash. Moreover, such errors are very covert and often very difficult to find. So when we started working with arrays. Be sure to always keep an eye out for array access.

The corresponding relationship between the array subscript and the data elements in the array, for example, as seen:

Figure 3-3 The corresponding relationship between the data element and subscript in the array

Same truth. For multidimensional arrays such as two-dimensional arrays, three-dimensional arrays, etc. The same ability to access data elements in an array by given multiple subscripts. Like what:

// first grade. The second class, the 26th student's achievement is thearrscore[0[1][three];

Here we can see. Read and write access to elements in an array by subscript, as simple as using a single variable. With an array of help. We are describing a large number of the same data. Instead of having to define multiple variables of the same type individually, you simply define an array that accommodates the data you need to process, and then you can access different data through different subscripts. It is as convenient as having multiple individual variables.

Hello, C + + (12) How to manage multiple types of data of the same nature? 3.6 Arrays

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.