#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;
}