我有一个类型为LPWSTR
的变量,其中包含"true"
或"false"
。有没有办法用true
或false
将其转换为bool类型的变量?
理想情况下,我想做这样的事情:
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.
}
std::wistringstream
可以帮助:
std::wistringstream(variableOne) >> std::boolalpha >> variableTwo;
头文件:
#include <sstream>
#include <string>
鉴于先决条件,即variableOne
指向"true"
或"false"
,将其转换为布尔值的最有效方法是:
bool const variableTwo { *variableOne == L't' };
足以测试单个区别属性,例如第一个字符。其他任何字符,甚至字符串的长度都足够。