C++ string to enum

前端 未结 12 1293
无人及你
无人及你 2020-11-29 12:01

Is there a simple way in C++ to convert a string to an enum (similar to Enum.Parse in C#)? A switch statement would be very long, so I was wondering i

12条回答
  •  被撕碎了的回忆
    2020-11-29 12:30

    Use std::map and use boost::map_list_of to easily initialize it.

    Example,

    enum X
    {
       A,
       B,
       C
    };
    
    std::map xmap = boost::map_list_of("A", A)("B", B)("C",C);
    

提交回复
热议问题