C++:使用 boolalpha
我正在使用一个函数(TinyXML 的 TiXmlElement::QueryValueAttribute(const std::string &name, T * outValue
)),该函数尝试将字符串读入传递的数据类型中。在我的例子中,我我正在传递一个 bool
,所以我想使用 boolalpha
标志,以便输入可以是 true
或 false
。而不是 0
或 1
我该怎么做
?
I am using a function (TinyXML's TiXmlElement::QueryValueAttribute(const std::string &name, T * outValue
) that attempts to read a string into the data type that is passed. In my case I am passing a bool
. So I want to use the boolalpha
flag so that the input can be true
or false
instead of 0
or 1
.
How do I do this?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
TiXmlElement::QueryValueAttribute
使用std::istringstream
来解析值。因此,您可以围绕bool
创建一个包装类,它重载operator >>
以始终在提取之前设置boolalpha
:TiXmlElement::QueryValueAttribute
uses astd::istringstream
to parse the value. So, you can create a wrapper class aroundbool
that overloadsoperator >>
to always setboolalpha
before extraction:您可以使用字符串值构造 istringstream,然后从那里流式传输到 *T 变量。 I/O 方面如下所示。
You can use the string value to construct a istringstream, then stream from there into your *T variable. The I/O aspects are illustrated below.
您可以只在
main()
中使用。
例如
输出将为 false 而不是 0。
You could just use
in
main()
.For example
The output will be false instead of 0.