C ++ learning tour-structure struct

Source: Internet
Author: User

C ++ learning tour-structure struct
C ++ learning tour-structure struct

In our programming, the structure isVery importantFor example, suppose we want to program the name, salary, height, weight, average score, and three points of Stephen Curry (my favorite basketball star), hit rate, number of assists, and so on. We hope that a data format can store the information in a unit. Students who have never used structs (like me a long time ago) may first think of arrays. But I will tell you that the array cannot complete this task, because although the array can store multiple elements. However, all elements must be of the same type. That is to say. An array can store 20 int values. Another array can store 15 float values, but the same Array cannot store int values in some elements, and other elements can store float values. Therefore, in the face of such problems, arrays are obviously inefficient and inconvenient.

The structure in C ++ can well meet this requirement (store the information of Basketball Players ). A structure is a more flexible data format than an array, because the same structure can store multiple types of data, which enables information about the library (basketball player) to be stored in a structure, to merge the data representation. If you want to store the information of the entire (Warrior) team .. Then we can use a structure array. Each array element is a structure that can store one member.

The structure is a user-defined type, and the structure Declaration defines the data attributes of this type. After the type is defined, you can create a variable of this type. Therefore, the definition structure is divided into two steps. First, define the structure description, and then create the structure variable (Structure Data Object) according to the description as follows:

Struct inflatable // The structure name is inflatable {char name [20]; // The structure member name int age ;//.. float volume ;//.. double price ;//..}; // note that semicolons cannot be lost.

The keyword struct indicates that the Code defines a structure layout. Inflatable is the name of this data format. In this way, you can create inflatable variables like int and char variables. The following braces contain a list of data types stored in structured storage. Each list item is a declaration statement. The preceding structure has four structure members with different types.

Create a structure variable (equivalent to taking the template to the player)
Example:

Inflatable kuli; inflatable dawei; // if you have used the C language, you will find that the structure variable created in the C language is struct inflatable kuli; // In fact, the keyword struct can be omitted when the structure variable is declared. // in C ++, the usage of the structure mark is the same as that of the basic type name. This change emphasizes that, structure Declaration defines a new type

Because the kuli type is inflatable, you can use the member operator (.) to access each member. For example, kuli. name indicates the name Member of the structure. Kuli. price refers to the price member. The same dawei. price is the price Member of the dawei variable. In short, the members who can access the structure through the member name can access the array just like through the index.
Next, let's take a look at how to use the structure in the program.


The program running result is as follows:

We can see from this program that the structure is very convenient for the collection of multiple types of data. In addition, you can use the value assignment operator (=) to assign the structure member to another structure of the same type, so that each member in the structure will be set to the value of the corresponding member in another structure, even if the member is an array. This assignment is called a member assignment. As in the code, assign the pal value to test. The printed values are the same.

This section also showed us a problem, that is, the scope of the structure.
The position of the structure Declaration is very important. If it is a locally declared structure, this structure can only be used in this function and cannot be used in other functions. However, external declarations can be used by any function after them. C ++ does not advocate the use of external variables, but advocates the use of external structure declarations. For example, inmain is the internal declaration structure in the main program, so there is no problem in the internal use of the program, but an error occurs in the external function. As follows:

Structure Initialization Method

1. You can define and create structural variables at the same time.
struct perks{    int key_number;    char car[12];}mr_smith, ms_jones;
2. You can even initialize variables created in this way.
Struct perks {int key_number; char car [12];} mr_gitx = {7, "Pasdf"}; // However, separating structure definitions and declarations can increase program readability
3. There is also a special statement
// Its name is omitted. struct {int x; int y;} position;/* creates a structure variable named position. You can use the member operator to access its members, but this type does not have a name. Therefore, you cannot create a variable of this type in the future.

Except that C ++ programs can use the structure tag as the type name, the structure of C language has all the features of the C ++ structure currently discussed, but the structure of C ++ has more features. For example, unlike the structure of C, C ++ can have member methods in addition to member variables. These advanced features are often used in classes.

Structure Array
Integer array -- as the name implies, it is an array that stores integer data. Each element is an integer.
For exampleint a[10];
Character array-as the name implies, it is an array for storing characters. Each element is a character.
For examplechar ptr[10];

Analogy
We know that a structure array is an array that stores the structure. Each element of the structure is a structure, for example:

Struct inflatable {char name [20]; int id; long phone :}; inflatable geust [2]; // This is a structure array // We can initialize the structure as follows: inflatable geust [2] {"basdf", 12,123 879234 }, // geust [0] Structure {"xcfaw", 32,231 810423} // geust [1] structure };

To initialize the structure array, you can combine the rules that use the initialization array (Use commas to separate the values of each element and enclose these values in curly brackets) and the initialization structure rules (Use commas to separate the values of each member and enclose these values in curly brackets ). Since each element in the array is a structure, you can use the structure initialization method to provide its value. Therefore, the final result is a list of values enclosed in curly braces separated by commas (,).

Structure usage (mainly efficiency ):
The structure can simplify the number of input parameters. You only need to pass a struct to the function. Of course, the structure can also increase the number of parameters passed out of the structure and return a structure that will take all the members out of the structure. Of course, this simplification can increase the readability of the program and improve the efficiency of the function. And greatly improved the efficiency in future modifications (for example, your function originally had five parameters. When the structure was developed to a large scale, it suddenly found that my function needed six parameters, haha, all the places where the function is called are changed ??? I'm afraid I can't stand it, but it won't be so annoying to use the structure for parameters. Just add a member and modify it as needed. Is it very efficient ??) Therefore, the structure is very important in the development of program development projects !!!!!

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.