c++中enum的用法——枚举类型

将变量的可取值全部列举出来,写在程序的开头,使用该类型的时候显示地指定取值即可(即对枚举变量进行赋予元素操作,这里之所以叫赋元素操作不叫赋值操作就是为了说明枚举变量是不能直接赋予算数值的)。

如:

enum egg {a,b,c}; 

enum egg test;     //在这里可以简写egg test; 

test = c;

注:

1)枚举类型具有默认值。

2)枚举类型可以进行关系运算。

3)要将整数值赋值给枚举类型时,要进行强制类型转换。

Code:

#include<iostream>

#include<stdio.h>

using namespace std;

enum egg {a=1,b,c};

int main()

{

    enum egg test;

    printf("%d ",test);

    test = c;

    printf("%d ",test);

    egg test1=egg(a);

    printf("%d",test1);

}

输出结果:2 3 1 

by TZ 2017.3.25

原文地址:https://www.cnblogs.com/acm-icpcer/p/6640684.html