enum

#include<iostream>
using namespace std;

int main()
{
	enum Week{monday=1,tuesday,wednesday,thursday,friday,staturday,sunday};//monday,tuesday等称为枚举子
        Week one=tuesday;
        //在当前代码中不可以出现不同枚举类型的相同枚举子。例如此代码中又出现enum Today{monday,tuesday}这样的话,是会报错的
	cout<<monday<<endl;//-->1
        cout<<one<<endl;//-->2
        return 0;
}

假设我们要写一个函数,通过今天天气是晴还是雨来决定将要做什么

void Plan(int weather)//weather只能是0或1
{
	if(weather==0)//雨天的话
		do something;
	else if(weather==1)
		dosomething;
}

换成枚举

#include<iostream>
using namespace std;
enum Weather{sunny,rainy};
void Plan(Weather x)
{
	;
}
int main()
{
	
	Plan(sunny);//使用的时候:Plan(sunny);这样就ok了
	//切记不要写成Plan(1);

	return 0;
}

枚举可以进行显示转换

</pre><pre name="code" class="cpp">#include<iostream>
using namespace std;
enum Weather{sunny,rainy};

int main()
{
	int x;
	cin>>x;//记住该枚举的范围只能是0或1,如果你输入5的话,是没有效果的
	Weather plan=(Weather)x;
	switch(plan)
	{
	case sunny:cout<<"sunny"<<endl;break;
	case rainy:cout<<"rainy"<<endl;break;
	}
	

	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值