C++ string to enum

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

    "Additional question: Is it possibile to handle undefined strings ? I mean if I try to get the value for responseHeaderMap["cookie"], what will be the value? (provided that "cookie" is not defined in the responseHeaderMap – bart s Nov 22 '16 at 12:04"

    well, you can just make check before:

    auto it = responseHeaderMap.find("cookie");
    if (it != responseHeaderMap.end())
    {
         // "cookie" exist, can take value 
    }
    

    After "cookie" exist check, you can get it value with use:

    responseHeaderMap["cookie"]
    

    hope this help

提交回复
热议问题