C++ 枚举的用法


#include 

enum NavigationState {
    PLANNING,
    CONTROLLING,
    CLEARING
};

int main() {
    NavigationState state = CLEARING;
    
    if (state == PLANNING) {
        std::cout << "Currently in planning state" << std::endl;
    } else if (state == CONTROLLING) {
        std::cout << "Currently in controlling state" << std::endl;
    } else if (state == CLEARING) {
        std::cout << "Currently in clearing state" << std::endl;
    }
    
    return 0;
}

上述代码定义了一个枚举类型NavigationState,并创建了一个state变量并初始化为PLANNING。接着使用if-else语句根据不同的状态进行判断并输出相应的信息。在这个例子中,输出结果将为"Currently in clearing state
C++ 枚举的用法_第1张图片

你可能感兴趣的:(c++,c++,开发语言)