- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用一个函数(TinyXML 的 TiXmlElement::QueryValueAttribute(const std::string &name, T * outValue
)尝试将字符串读入传递的数据类型。在我的例子中我正在传递一个 bool
。所以我想使用 boolalpha
标志,以便输入可以是 true
或 false
而不是 0
或 1
。
我该怎么做?
谢谢。
最佳答案
TiXmlElement::QueryValueAttribute
使用 std::istringstream
来解析值。因此,您可以围绕 bool
创建一个包装类,重载 operator >>
以在提取之前始终设置 boolalpha
:
class TinyXmlBoolWrapper
{
public:
TinyXmlBoolWrapper(bool& value) : m_value(value) {}
bool& m_value;
};
std::istream& operator >> (std::istream& stream, TinyXmlBoolWrapper& boolValue)
{
// Save the state of the boolalpha flag & set it
std::ios_base::fmtflags fmtflags = stream.setf(std::ios_base::boolalpha);
std::istream& result = stream >> boolValue.m_value;
stream.flags(fmtflags); // restore previous flags
return result;
}
...
bool boolValue;
TinyXmlBoolWrapper boolWrapper(boolValue);
myTinyXmlElement->QueryAttribute("attributeName", &boolWrapper);
// boolValue now contains the parsed boolean value with boolalpha used for
// parsing
关于C++:使用 boolalpha,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4789193/
我正在使用一个函数(TinyXML 的 TiXmlElement::QueryValueAttribute(const std::string &name, T * outValue)尝试将字符串读入
#include #include #include using namespace std; string STRING; bool isEqual(double a, double
以下代码使用 g++ 7 编译器和 Apple clang++ 给出了不同的结果。当使用 std::boolalpha 时,我是否在 bool 输出的对齐中遇到了 clang 中的错误,或者我是否犯了
我看到了其他 boost::lexical_cast 问题的一些答案,断言以下是可能的: bool b = boost::lexical_cast("true"); 这不适用于 g++ 4.4.3 b
我已经看到其他 boost::lexical_cast 问题的一些答案,这些问题断言以下是可能的: bool b = boost::lexical_cast("true"); 这不适用于 g++ 4.
我是一名优秀的程序员,十分优秀!