About C ++ enumerated type enum

About C ++ enumerated type enum

First, what is an enumeration type?
Enumerated type (Enumeration) is a derived data type in C ++, which is a collection of several enum constant defined by the user.

Declaration form:

enum 枚举名 {变量列表}; 

E.g:

 enum Weekday{SUN,MON.TUE,WED,THU,FRI,SAT};

Enumerated data types defined variables, for example:

Weekday myweekday;

(Note that the data type is Weekday instead of enum Weekday)

Second, the enumeration type Application Notes

1, the element has a default value enumerated, are as follows: 0,1,2,3, .... For example, a value of embodiment SUN 0, MON value of 1, TUE is 2, ..., SAT 6.

2, may be a separate element of the enumeration value is defined in the statement, such as:

enum Weekday{SUN=7,MON=1,TUE,WED,THU,FRI,SAT};

Is defined as the SUN 7, MON 1, after a sequence of adding, to the TUE 2, WED is a 3, ..., SAT 6.
3, according to the element of the enumeration constants process, but can not directly impart a constant value thereof. For example, the following statement is wrong:

 SUN=7;//SUN是枚举元素,不能直接赋予常量值

The need to attach the integer value to the enumeration, should be cast , for example:
SUN=(Weekday)7;//在值前面加(类型)是常用的强制类型转换方法
4, relational operation enumeration value may be:
1) can be used to compare two relational operators enumeration. For example, the following value is stored in the if statement is myweekday1 is smaller than the value stored in myweekday:
if (myweekday1 <myweekday2)
If the value is less than the value stored myweekday1 myweekday2, then the statement is true. Otherwise, the statement will be false.
2) may be used instead of an integer value symbolic name enumeration test. For example, if the following two statements are equivalent:

if  myweekday== MON)
if ( myweekday == 1)

5, if the assigned enumeration, they must be an integer . The following assignment statement will produce an error:
enum Weekday{SUN=1.1,MON=1.2,TUE=1.3,WED=1.4,THU,FRI,SAT }; //错误

6, one of the purposes enumerated data type is the symbolic name helps make the program self-explanatory. However, these names are not strings, but the values, so when the output is a digital output, instead of a string. E.g:

Weekday myweekday = SUN;
cout << myweekday;//输出的是0这个数值

Enter the following results:
Output

7, the enumerated values ​​can be assigned to integer variables, for example:

int a;
a = SUN;//枚举值可以赋给整形变量
cout << a;

8, since the symbolic name of the enumerated data type associated with the integer value, so that they can be used in a switch statement, the program as shown in the following:
Title: pocket red, yellow, blue, white and black balls 5 colors several. Each time out of the pocket three balls of different colors, and asked how many emulated?

 #include<iostream>
using namespace std;
void transfer(int i)//将相应的数值组合通过switch语句转换为字符串
{
    switch (i)
    {
    case 0:cout << "red "; break;
    case 1:cout << "yello "; break;
    case 2:cout << "blue "; break;
    case 3:cout << "white "; break;
    case 4:cout << "black "; break;
    }
}
enum ball {red,yello,blue,white,black};//定义球的枚举类型
int main()
{
    int i, j, k,count=0;
    for (int i = red; i <= black; i++)
    {
        for (int j = i+1; j <= black; j++)
        {   
                for (int k =j+1; k <= black; k++)
                {
                    
                        transfer(i);
                        transfer(j);
                        transfer(k);
                        cout <<endl;
                        count++;
                    
                }
        
        }
    }
    cout << count;
    return 0;
}

The result:
operation result
Third, the summary
when a variable There are several possible values, it can be defined as an enumerated type. Enumerated type in favor of readability. Enumeration constants by processing element, the element has a default value enumerated, but can not be directly applied to a constant value thereof. Because enumeration is an integer, it can also be used to compare two relational operators enumerations, and generally with a switch statement to a corresponding output value.

Guess you like

Origin www.cnblogs.com/-believe-me/p/11518382.html