c++中的枚举是一种包含固定常量的数据类型。
枚举可以用于星期几(sunday
,monday
,tuesday
,wednesday
,thursday
,friday
和saturday
),方向(north
,south
,east
和west
等)。c++枚举常量是静态和最终隐式。
c++枚举可以认为是具有固定的常量集合的类。
c++中枚举注意事项
switch
语句块中使用enum
类下面来看看看在c++程序中使用的枚举数据类型的简单例子。
#include <iostream>
using namespace std;
enum week { monday, tuesday, wednesday, thursday, friday, saturday, sunday };
int main()
{
week day;
day = friday;
cout << "day: " << day+1<<endl;
return 0;
}
执行上面代码得到以下结果 -
day: 5