C++ string to enum

前端 未结 12 1309
无人及你
无人及你 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:26

    saw this example somewhere

    #include 
    #include 
    
    enum responseHeaders
    {
        CONTENT_ENCODING,
        CONTENT_LENGTH,
        TRANSFER_ENCODING,
    };
    
    // String switch paridgam   
    struct responseHeaderMap : public std::map
    {
        responseHeaderMap()
        {
            this->operator[]("content-encoding") =  CONTENT_ENCODING;
            this->operator[]("content-length") = CONTENT_LENGTH;
            this->operator[]("transfer-encoding") = TRANSFER_ENCODING;
        };
        ~responseHeaderMap(){}
    };
    

提交回复
热议问题