C++ 枚举的用法

2023-12-21 15:27:49

#include <iostream>

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
在这里插入图片描述

文章来源:https://blog.csdn.net/qq_40464599/article/details/135131623
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。