枚举旧用法
// 枚举旧用法
#include<iostream>
using namespace std;
enum day {MON, THU, WEN};
// C中习惯中typedef来重命名类型以避免每次类型使用时都加enum
/*typedef enum {MON, THU, WEN}day;*/
int main()
{
// C语言的用法,定义了一个day类型的变量,变量名为d1。
//前面的enum不能省略
//enum day d1;
day d1; // C++中定义时可以省掉前面的enum
d1 = MON;
if(d1 not_eq WEN)
{
cout << "not WEN" << endl;
}
else
{
cout << "WEN" << endl;
}
return 0;
}
C++11枚举新用法
// C++11枚举新用法
#include<iostream>
using namespace std;
//旧用法的枚举变量的类型是编译器随机默认给的
//enum day{mon, thu = 2, wen};
//C++11新用法中的完全写法,在:后面可以指定枚举变量的类型
//enum class day:unsigned int {mon, thu, wen};
//简写方法1
//enum class day{mon, thu, wen};
//简写方法2,没有创造命名空间
enum day{mon, thu, wen};
int main()
{
day d1;
// 在使用完全写法和简写方法1是必须要写命名空间的名字day::
d1 = day::wen;
// 简写方法2不需要写命名空间的名字
// d1 = wen;
if(d1 != day::mon){
cout << "not mon" << endl;
}
else{
cout << "trun" << endl;
}
return 0;
}
//cout不能输出枚举类型, 不能++运算,除非类型强制转换成其他类型,如int