代码之家  ›  专栏  ›  技术社区  ›  user3587624

LPWSTR字符串到bool?

  •  0
  • user3587624  · 技术社区  · 5 年前

    我有一个变量类型 LPWSTR 其中包含 "true" "false" 在里面。 true false

    理想情况下,我想做这样的事情:

    FooClas::MyMethod()
    {
        LPWSTR  variableOne;
        bool    variableTwo;
        MyMethodOne(&variableOne);
    
        // At this point, variableOne can be either "true" or "false".
        // Do something to check if "true", then variableTwo = true, otherwise variableTwo = false.
    }
    
    0 回复  |  直到 5 年前
        1
  •  3
  •   IInspectable    5 年前

    考虑到先决条件 variableOne 任一点 "true" "false" ,将其转换为布尔值的最有效方法是:

    bool const variableTwo { *variableOne == L't' };
    

    这足以测试一个单一的辨别属性,比如第一个字符。任何其他字符,甚至字符串的长度都同样足够。

        2
  •  3
  •   Drake Wu    5 年前

    std::wistringstream

    std::wistringstream(variableOne) >> std::boolalpha >> variableTwo;
    

    头文件:

    #include <sstream>
    #include <string>