C++详解:枚举类型 --- enum | Xunlan_blog


一、概念

枚举类型 (enumeration),是C++中的一种派生数据类型,是用户创建的一个集合,可以增加程序的可读性,在一些需要重复用到一些元素时颇有益处。

二、定义

枚举类型的定义格式:

enum enum_name {/*enum_table*/};
enum enum_name {/*enum_table*/} enum_val;

其中:

  • enum :枚举类型关键字,声明后面定义的是枚举类型。
  • e n u m _ n a m e enum\_name enum_name :枚举名称,相当于class后面跟的类名称。
  • { e n u m _ t a b l e } \{enum\_table\} {enum_table} :枚举元素表,该枚举枚举类型仅能使用元素表中的 枚举元素(或称 枚举常量)。
  • e n u m _ v a l enum\_val enum_val :[可选] 声明枚举类型后,可以紧跟着一个枚举对象,也可以分开。如,以下两段代码作用一致:
    enum exam{a,b,c,d,e} stu1,stu2;
    
    enum exam{a,b,c,d,e};
    exam stu1,stu2;
    

枚举元素表

以下是一个正确的枚举类型声明:

enum week{MON,TUE,WED,THR,FRI,SAT,SUN};

以下是一个错误的枚举类型声明:

enum week{"MON","TUE","WED","THR","FRI","SAT","SUN"};

enum类型 不允许元素为字符常量、字符串常量、整型常量、浮点常量、布尔常量等字面常量(见常量

元素间应以【,】隔开。

默认情况下,枚举类型的每个元素可以看作是数组元素,但元素的值为 从0开始 的索引。特别地,你可以在声明时对部分或所有元素索引赋值,对于赋值元素之前的元素,仍以原来的方式,对于赋值元素,变为所赋的值,对于之后的元素,从该元素索引+1开始。
或言之,将第一个元素赋值0,再将指定的元素赋值,然后从第2个元素起每个的值为上一个的值+1。

三、定义

定义一个枚举对象:

enum_type enum_val;
enum enum_type{/*enum_type*/} enum_val;//声明类型时同时定义

枚举对象的操作

  1. 枚举对象仅能取该类型中的值,即值 ∈ \in 该类型索引。
  2. 枚举变量占用内存与int一致。
  3. 枚举变量能且仅能参与赋值、比较、输出操作,参与运算时使用索引值。
    enum ex{a,b=3,c,d=0,e};//索引值:{0,3,4,0,1}
    ex stu1,stu2;
    stu1=a,stu2=c;//赋值
    std::cout << stu1 << ' ' << stu2 << std::endl;//输出:0 4\n
    int i1=stu1,i2=stu2;
    printf("%d %d\n",i1,i2);//输出:0 4\n
    bool b=stu1<stu2;//true
    
    stu2=d;
    cout<<(stu1==stu2)? "equal!\n" : "not equal!\n" //输出:equal!\n
    

四、要点&技巧

  1. 不同类型枚举常量不能相互赋值(即使指向元素一样),同类型则可以:
    enum ex1{a,b,c} stu1,stu2;
    enum ex2{a,d,e} stu3;
    stu1=a;
    stu2=stu1;//合法
    stu3=stu1;//非法
    
  2. 枚举变量可以输出不能输入:
    ex stu1;
    cin>>stu;//非法
    ex stu2=a;
    cout<<stu2;//合法
    
  3. 因为枚举变量输出的是索引,可以使用switch(){}语句转换成字符串:
    enum ex{a,b,c,d,e} stu;
    stu=c;
    cout<<stu<<endl;
    switch(stu){
    	case a:	cout<<'a';break;
    	case b:	cout<<'b';break;
    	case c:	cout<<'c';break;
    	case d:	cout<<'d';break;
    	case e:	cout<<'e';
    }
    /*输出:
    ---
    2
    c
    ---
    */
    

实例

题目:

口袋中有红、黄、蓝、白、黑五种颜色的球若干个,每次从口袋中取三个不同颜色的球,统计并输出所有的取法。

#include<bits/stdc++.h>
using namespace std;

signed main(){
	enum color{red,yellow,blue,white,black} choose;
	int i,j,k,cnt=0,loop;//i,j,k为三种球
	for(i=red;i<=black;++i){
		for(j=red;j<=black;++j){
			if(i==j) continue; //确保不一样
			for(k=red;k<=black;++k){
				if(k==i||k==j) continue;
				cnt++;
				if(!(cnt%22)){
					system("pause");//每22行暂停一下。
				}
				cout<<setw(15)<<cnt;
				//开始输出选法
				for(loop=1;loop<=3;++loop){
					switch(loop){
						case 1: choose=(color)i;break;
						case 2: choose=(color)j;break;
						default: choose=(color)k;
					}//三次输出i、j、k。
					switch(choose){
						case red:   	cout<<setw(15)<<"red";   break;
                        case yellow:	cout<<setw(15)<<"yellow";break;
                        case blue:  	cout<<setw(15)<<"blue";  break;
                        case white:		cout<<setw(15)<<"white"; break;
                        case black: 	cout<<setw(15)<<"black"; break;
					}
				}
				cout<<endl;//换行
			}
		}
	}
	cout<<"total:"<<cnt;
	return 0;
}

result:

              1            red         yellow           blue
              2            red         yellow          white
              3            red         yellow          black
              4            red           blue         yellow
              5            red           blue          white
              6            red           blue          black
              7            red          white         yellow
              8            red          white           blue
              9            red          white          black
             10            red          black         yellow
             11            red          black           blue
             12            red          black          white
             13         yellow            red           blue
             14         yellow            red          white
             15         yellow            red          black
             16         yellow           blue            red
             17         yellow           blue          white
             18         yellow           blue          black
             19         yellow          white            red
             20         yellow          white           blue
             21         yellow          white          black
按任意键继续...
             22         yellow          black            red
             23         yellow          black           blue
             24         yellow          black          white
             25           blue            red         yellow
             26           blue            red          white
             27           blue            red          black
             28           blue         yellow            red
             29           blue         yellow          white
             30           blue         yellow          black
             31           blue          white            red
             32           blue          white         yellow
             33           blue          white          black
             34           blue          black            red
             35           blue          black         yellow
             36           blue          black          white
             37          white            red         yellow
             38          white            red           blue
             39          white            red          black
             40          white         yellow            red
             41          white         yellow           blue
             42          white         yellow          black
             43          white           blue            red
按任意键继续...
             44          white           blue         yellow
             45          white           blue          black
             46          white          black            red
             47          white          black         yellow
             48          white          black           blue
             49          black            red         yellow
             50          black            red           blue
             51          black            red          white
             52          black         yellow            red
             53          black         yellow           blue
             54          black         yellow          white
             55          black           blue            red
             56          black           blue         yellow
             57          black           blue          white
             58          black          white            red
             59          black          white         yellow
             60          black          white           blue
total:60

按任意键关闭此窗口. . .

根据乘法原理, t o t a l = 5 × 4 × 3 = 60 total=5\times4\times3=60 total=5×4×3=60,故代码无误。


@HaohaoCppDebuger|寻兰 
2021/11/16 

-----THE END-----
THANK YOU !

  • 21
    点赞
  • 76
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值