In programming, it is sometimes used in a collection of several finite data elements, such as the collection of seven data elements in Monday to Sunday within a week, a collection of three color red, yellow, and green, a job The collection of ten employees in the team, etc., the value of a variable in the program is limited to the elements in the collection. At this point, these data sets can be defined as enumeration types. Therefore, the enumeration type is a collection of certain types of data, such as the possibility of values in the week in the week:
{Sun, MON, TUE, WED, THU, FRI, SAT}
This collection can be defined as an enumeration type describing the week, which has a total of seven elements, so an enumerated variable defined by an enumeration type can only take a certain element value in the collection. Since the enumeration type is exported data type, you must first define the enumerated type, and then define an enumerated variable with the enumerated type.
Enum {}; where: Keywords enum Representation is defined by enumeration type, enumeration type name is composed of identifier, and enumeration element table is enumerated or enumerated Constant composition. For example: Enum weekdays {Sun, MON, TUE, WED, THU, Fri, Sat} defines an enumeration type named WeekDays, which contains seven elements: Sun, Mon, Tue, WED, THU, FRI, SAT . When compiling a compiler, specify a constant constant value (also known as a serial number value) to each element in the enumerated type. If there is no integer value of the specified element in the enumerated type definition, the integer value is incremented by 0, so the seven elements of the WeekDays enumeration type are SUN, MON, TUE, WED, THU, FRI, SAT correspondence The integer values are 0, 1, 2, 3, 4, 5, 6, respectively. Note: When defining an enumerated type, you can also specify an integer constant value corresponding to the element. For example, the enumeration type of the logic value set {true, false} can be defined as follows: enum boolean {true = 1, false = 0}; this definition specification: True value is 1, and the value of false is 0. Description Color Set {Red, Blue, Green, Black, White, Yellow} Type Colors can be defined as follows: Enum colors {red = 5, Blue = 1, Green, Black, White, Yellow}; this definition regulation RED For 5, Blue is 1, and the elements value will increase from 2 to 1. Green, Black, White, YELLOW is 2, 3, 4, 5. At this time, the integer 5 will be used to represent two color Red and Yellow. Usually the two different elements have the same integer value. The definition of enumeration type is only defined a new data type, and this data type can only be used to define enumeration variables with an enumeration type.
8.1.2 Definition of an enumerated type variable
There are three ways to define enumeration type variables, namely, first define variables, define variables, define variables, directly define variables, now as follows: 1. Define variable format after first define: [, , ..., ]; for example: enum colors {red = 5, blue = 1, Green, Black, White, Yellow}; Colors C1, C2; C1, C2 is a enumerated variable of the colors type.
2. Define variables while defining types
Format: Enum {} [, , ..., ]; for example: enum colors {red = 5, blue = 1, Green , Black, White, Yellow} C1, C2; 3. Direct definition enumeration variable
Format: Enum {} [, , ..., ];,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, C1 = RED, C2 = Blue; As can be seen from the above example, when the enumeration variable is defined, the variable can be initialized, and the initial value of C1 is RED, and the initial value of C2 is Blue.
8.1.3 References of enumerated type variables
Only 2 types of calculations can only be used for enumeration type variables: assignment operation and relationship operation. 1. Assignment C specifies that the element of the enumeration type can directly assign an enumeration variable, and the same type enumeration variable can be assigned each other. Such as: enum weekdays // Define Sunday to Saturday for enumeration Type Weekdays {Sun, Mon, Tue, WED, THU, Fri, Sat}; void main (void) {weekdays day1, day2; // Define two enumeration types Variable day1, day2 day1 = sun; // Assign the element Sun to the enumeration variable day1 day2 = day1; // enumerate Variable DAY1 assignment to day2 cout << day1 << endl; // Output DAY1 value, ie Sun Serial No. 0} This example defines two types of WeekDays that type variables for WeekDays DAY1 and Day2. These two enumerated variables can only take the set of {Sun, MON, TUE, WED, THU, FRI, SAT}. You can use an assignment operator to assign the element Sun to DAY1. The value of the enumerated variable DAY1 can be assigned to the same type enumeration variable DAY2. Note: (1) You cannot use the keyboard to enter the element value to the enumerated variable to enumerate, for example: CIN >> DAY1 is wrong. Therefore, the value of the enumeration variable can only be entered by initialization or assignment operator. (2) The enumeration variable can be output with "COUT <<", but the output is the sequence number value corresponding to the element, not an element value. For example: cout << day1; outputs the serial number 0 corresponding to the element Sun in DAY1 with COUT, not the element sun.
2. The relationship operation enumeration variable can be compared with element constants. There is also a relationship between the same enumeration variables, but the relationship between the enumeration variables is compared to its serial number value. For example: day1 = sun; // DAY1 The serial number value of the element Sun is 0 days2 = mon; // DAY2 The sequence number value of the element MON is 1 if (day2> day1) day2 = day1; // day2> DAY1 comparison is Serial number value relationship: 1> 0 comparison IF (DAY1> SAT) DA1 = SAT; // DAY1> SAT is the serial number value relationship: 0> 6 comparison Day2 and DAY1 comparison, it is actually its element MON Compared with the Sun serial number value 1 and 0, since 1> 0 is established, Day2> DAY1 condition is true, DAY2 = day1 = sun. Similarly, since the serial number value of the element Sun in DAY1 is less than the serial number 6 of SAT, the day1> SAT condition is false, and the value of DAY1 is unchanged.
[Example 8.1] First define the enumeration type colors describing six colors, and then define the enumeration array with this enumeration type, and enter 6 color numbers arbitrarily, convert to the enumeration array after converting into a corresponding color enumeration. , Finally output the color in the enumeration array. # include # include // Use the exit (0) function You must include the entry type colors // define the enumeration type colors {Red, Blue, Green, Black, White, Yellow}; Void Main (Void) {Colors Color [6]; // Define Enumeration Class Array Color [6] Int J, N; COUT << "0: Red, 1: Blue, 2 : Green, 3: Black, 4: White, 5: Yellow "<< Endl; cout <<" Please enter 6 color numbers: "; for (j = 0; j <6; j ) {cin >> n; // Enter color number if (n <0 || n> 5) {cout << "Enter color value error, please re-enter!"; Exit (0);} else switch (n) // convert color number to Color elements are stored in array {case 0: color [j] = red; break; case 1: color [j] = blue; break; case 2: color [j] = green; break; casse 3: color [j] = Black; Break; Case 4: Color [J] = White; Break; Case 5: Color [J] = YELLOW; BREAK;}} for (j = 0; j <6; j ) // 6 times, output arrays Element of color {Switch (color [j]) {copy red: cout << "red"; break; case blue: cout << "blue"; break; casser: cout << "green"; break; cas Black: cout << "Black"; Break; Case White: cout << "White"; break; casser: cout << "Yellow"; COUT << '/ t';} Cout << '/ n';} Operation, screen tips: 0: Red, 1: Blue, 2: Green, 3: Black, 4: White, 5: Yellow, please enter 6 color numbers: 0 1 2 3 4 5 Screen Out: Red Blue Green Black White Yellow WHILOW cannot be enumerated by the keyboard directly to the enumeration variable, so you can only enter the serial number value of the enumerated element first, then Use the switch statement to convert the serial number value into an element value, and assign the element value to the enumerated array element.
Similarly, since the element value in the enumerable array cannot be output with COUT, when the output is used, it can only be used to determine which element is output, and then the corresponding element value is output with the COUT << "Element" mode. [Example 8.2] Define an enumerated type {RED, Blue, Green} describing three colors, and output all of these three colors. Solution: This is a full-aligned problem of three colors, and all 27 ranks of three colors can be output in a row. # include Enum colors {red, blue, green}; void show (color) {switch (color) {copy red: cout << "red"; break; case blue: cout << "blue" Break; Case Green: COUT << "Green"; Break;} cout << '/ t';} void main (void) {Colos Col1, Col2, Col3; for (col1 = red; col1 <= Green; col1 = Colors (INT = Red; Col2 <= green; col2 = colors (int (color) 1)) for (col3 = red; col3 <= Green; col3 = colors (COL3) 1)) {Show (color); show (col2); show (col3); cout << '/ n';}} The program has all combinations of three colors through triple cycle. In the FOR cycle statement, use the enumeration variable COL1 to loop variables, and the value of the COL1 is started from the Red start to Green. The self-intementing operation of the cyclic variable is achieved by expressions colors (INT (COL1) 1). In the formula, the COL1 is first converted into an integer, then add 1, and then convert to the enumeration value of the colors type to the col1 variable.