Definition of enumeration type:Enumeration type (enumeration) is a derived data type in C++, which is a collection of several enumeration constants defined by the user.
Definition format: The definition format of the enumeration type is:
enum <类型名> {<枚举常量表>};
Format description:
- The keyword enum — indicates that the identifier following it is the name of an enumeration type.
- Enumeration constant table — composed of enumeration constants. "Enumeration constants" or "enumeration members" are integer quantities expressed in the form of identifiers, representing the values of the enumeration type. The enumeration constant table lists all the values of the enumeration type. The enumeration constants are separated by commas and must be different from each other. The value type is the same as that of a conditional expression.
Application examples:
enum color_set1 {RED, BLUE, WHITE, BLACK}; // 定义枚举类型color_set1
enum week {Sun, Mon, Tue, Wed, Thu, Fri, Sat}; // 定义枚举类型week
Important notes:
Enumeration constants represent the possible values that variables of this enumeration type can take. The compilation system assigns an integer value to each enumeration constant. By default, this integer is the sequence number of the listed element, starting from 0. Integer values can be specified for some or all enumeration constants when defining the enumeration type. Enumeration constants before the specified value still take values in the default manner, while enumeration constants after the specified value take values according to the principle of incrementing by 1 in sequence. The values of enumeration constants can be repeated. For example:
enum fruit_set {apple, orange, banana=1, peach, grape}
//枚举常量apple=0,orange=1, banana=1,peach=2,grape=3。
enum week {Sun=7, Mon=1, Tue, Wed, Thu, Fri, Sat};
//枚举常量Sun,Mon,Tue,Wed,Thu,Fri,Sat的值分别为7、1、2、3、4、5、6。
Enumeration constants can only be represented as identifiers, not as literal constants such as integer or character types. For example, the following definition is illegal:
enum letter_set {'a','d','F','s','T'}; //枚举常量不能是字符常量
enum year_set{2000,2001,2002,2003,2004,2005}; //枚举常量不能是整型常量
It can be changed to the following form and the definition becomes legal:
enum letter_set {a, d, F, s, T};
enum year_set{y2000, y2001, y2002, y2003, y2004, y2005};
Usage of Enumeration Variables
The main purpose of defining enumeration types is to increase program readability. One of the most common and meaningful uses of enumeration types is to describe state quantities, which will be seen in Chapter 9, Input/Output Stream Classes.
Definition format: After defining an enumeration type, variables of that enumeration type can be defined, such as:
color_set1 color1, color2;
The type and variables can also be defined simultaneously (even the type name can be omitted), with the format as follows:
enum {Sun,Mon,Tue,Wed,Thu,Fri,Sat} weekday1, weekday2;
Related Operations
The values of enumeration variables can only be the values listed in the enumeration constant table, which is a subset of integers.
Enumeration variables occupy the same memory size as integers.
Enumeration variables can only participate in assignment, relational operations, and output operations. When participating in operations, their own integer values are used. For example, given the definition:
enum color_set1 {RED, BLUE, WHITE, BLACK} color1, color2;
enum color_set2 { GREEN, RED, YELLOW, WHITE} color3, color4;
Then the permitted assignment operations are as follows:
color3=RED; //将枚举常量值赋给枚举变量 color4=color3; //相同类型的枚举变量赋值,color4的值为RED int i=color3; //将枚举变量赋给整型变量,i的值为1 int j=GREEN; //将枚举常量赋给整型变量,j的值为0
The permitted relational operations are: ==, <, >, <=, >=, !=, etc., for example:
//比较同类型枚举变量color3,color4是否相等 if (color3==color4) cout<<"相等"; //输出的是变量color3与WHITE的比较结果,结果为1 cout<< color3<WHITE;
Enumeration variables can be output directly, and what is output is the integer value of the variable. For example:
cout<< color3; //输出的是color3的整数值,即RED的整数值1
Example
A pocket contains several balls of five colors: red, yellow, blue, white, and black. Each time, three balls of different colors are taken from the pocket. Count and output all the possible ways.
Analysis:Since a ball can only be one of the five colors, an enumeration type can be used to represent the color of the ball. Suppose the balls taken out are i, j, and k. According to the problem statement, i, j, and k can each have five possible values, and i≠j≠k. The exhaustive method can be used to check every possible combination one by one, find the combinations that meet the requirements, and output them.
Example
Important Notes
- Enumeration variables can be output directly, but cannot be input directly. For example: cout >> color3; //illegal
- Cannot directly assign a constant to an enumeration variable. For example: color1=1; //illegal
- Enumeration variables of different types cannot be assigned to each other. For example: color1=color3; //illegal
- The input and output of enumeration variables generally use switch statements to convert them into characters or strings; other processing of enumeration type data also often applies switch statements to ensure the legality and readability of the program.