c++ 判断值是否包含在枚举类型中
时间: 2024-03-24 08:40:05 浏览: 81
在C++中,我们可以使用枚举类型和switch语句来判断一个值是否包含在枚举类型中。例如:
```c++
enum Fruit { APPLE, BANANA, ORANGE };
Fruit myFruit = BANANA;
switch(myFruit) {
case APPLE:
std::cout << "My fruit is an apple." << std::endl;
break;
case BANANA:
std::cout << "My fruit is a banana." << std::endl;
break;
case ORANGE:
std::cout << "My fruit is an orange." << std::endl;
break;
default:
std::cout << "My fruit is not in the enum." << std::endl;
break;
}
```
在上面的例子中,我们定义了一个枚举类型Fruit,包含三个值:APPLE,BANANA和ORANGE。我们将变量myFruit赋值为BANANA,并使用switch语句来判断它是否包含在Fruit中。如果myFruit的值等于APPLE,我们输出"My fruit is an apple.",如果等于BANANA,我们输出"My fruit is a banana.",如果等于ORANGE,我们输出"My fruit is an orange.",否则我们输出"My fruit is not in the enum."。