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

Source: Internet
Author: User
Tags array definition

3.6 Arrays

After learning the previous basic data types, we can now define a single variable to represent the individual data. For example, we can use the int type to define a variable to represent the bus's 216 way; You can use the float type to define a variable to represent the tomato 3.5 yuan a pound. However, in addition to a single isolated data, there is a class of bulk data in the real world. For example, the payroll for all employees of a company, which has the same data type (all int types), is of the same nature (which represents the employee's salary), a large number (the wages of thousands of employees), and tends to form a meaningful collection 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 an array of such structured data types to express bulk data, which organizes these data into a data sequence that allows the data to be queued and eaten with fruit, greatly facilitating the processing of bulk data.

Declaration and initialization of 3.6.1 arrays

In C + +, the method of defining an array is very similar to defining a variable, but the variable name becomes the array name, and after the array name, we use the brackets "[]" to elicit a constant representing the number of data elements in the array. Its specific grammatical form is as follows:

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

Where the data type represents the type of this series of bulk data. For 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, then the data type of the whole array is the int type; 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. Where arr indicates that this is an array, and salary indicates that the data in the array is wages; the number of brackets in the array name indicates the number of batches of data in this series. For 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 can define an array to hold 100,000 employee payroll data:

An array of 100,000 employee wages is saved in int arrsalary[100000];  

You can also initialize an array with "{}" while you are defining arrays. For example:

defines and initializes an array of int narray[1,2,3,4,       

This line of code in the definition of a length of 5 integer array narray at the same time, with "{}" 1, 2, 3, 4, 5 are assigned to the array of 5 elements to complete the initialization of the array. Of course, if you do not need to assign an initial value to all the data in the array, you can only assign values to the previous elements of the array, and the remaining unspecified initial values will be assigned a value of 0 or the default initial value for this data. For example:

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

While it is possible to use "{}" to assign an initial value to an array of elements while it is being defined, it is often impractical to use "{}" to complete the assignment of all of the data in the array. More often than not, we use "{}" to assign all elements in the array to 0 to complete the clear 0 operation before the array is used. For example:

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

Know more: 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, which often represents the number of data classification. For example, we want to show the results of all students in a school, we tend to divide all student grades into three grades according to grade, then each grade can be divided into 10 classes according to the class, and each class has 30 students. In this way, after three classifications, we can use a three-dimensional array to preserve the scores of all students in a school:

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

Once we've defined the array, we have more than one variable, and we can refer to the data elements in the array to do the math. To access individual data in an array, we do this by giving an array subscript in brackets after the array name. The so-called array subscript, which represents the position of the data to be accessed in the array. Note that the subscript that represents the location of the data is counted starting at 0. For 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 //assignment of the array to 1, indicating that the boss's salary is 1arrsalary[1;// read the first data in the array, 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 by giving the subscript 0 in brackets after the array name. And so, to access the second data in the array, the array subscript should be 1, if you want to access the nth data, the subscript should be n-1. For example:

The second data in the array represents the boss's salary, giving a set of 1 visits to arrsalary[99999;  in turn is the salary arrsalary[of eachemployee;  ...         ..

Another thing to note when using array subscripts 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 a nutshell, an array of length n is a range of [0,n-1] values for the subscript. If the value of the subscript exceeds this range, it accesses the memory area outside the array, causing errors in the array's access out of bounds, causing the data to read and write errors, which can even cause the program to crash in severe cases. And, this kind of error is very covert, often difficult to find. So when we started using arrays, we had to be careful to prevent array access from going out of bounds. The array subscript corresponds to the data element in the array as shown in:

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

Similarly, for multidimensional arrays, such as two-dimensional arrays, three-dimensional arrays, the data elements in the array can also be accessed by given multiple subscripts. For example:

The first grade, the second class, the 26th student's achievement is 82arrscore[0][1][;   

As we can see here, reading and writing access to elements in an array is as simple as using a single variable. With the help of arrays, we do not have to define multiple variables of the same type individually when describing a large number of identical data, but simply define an array that accommodates the data that needs to be processed, and then use different subscripts to access different data, as easily as having multiple individual variables.

Original address: http://www.cnblogs.com/nihaoCPP/p/3998144.html

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

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.