C++:使用 boolalpha

发布于 2024-10-13 19:40:27 字数 312 浏览 7 评论 0原文

我正在使用一个函数(TinyXML 的 TiXmlElement::QueryValueAttribute(const std::string &name, T * outValue)),该函数尝试将字符串读入传递的数据类型中。在我的例子中,我我正在传递一个 bool ,所以我想使用 boolalpha 标志,以便输入可以是 truefalse。而不是 01

我该怎么做

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无人接听 2024-10-20 19:40:27

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

TiXmlElement::QueryValueAttribute uses a std::istringstream to parse the value. So, you can create a wrapper class around bool that overloads operator >> to always set boolalpha before extraction:

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
抚笙 2024-10-20 19:40:27

您可以使用字符串值构造 istringstream,然后从那里流式传输到 *T 变量。 I/O 方面如下所示。

#include <iostream>                                                             
#include <iomanip>                                                              
#include <sstream>                                                              

int main()                                                                      
{                   
    // output example                                                            
    std::cout << std::boolalpha << true << ' ' << false << '\n';

    // input example                
    std::istringstream iss("true false");                                       
    bool x = false, y = true;                                                   
    iss >> x >> y;                                                              
    std::cout << std::boolalpha << x << ' ' << y << '\n';                       
}

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.

#include <iostream>                                                             
#include <iomanip>                                                              
#include <sstream>                                                              

int main()                                                                      
{                   
    // output example                                                            
    std::cout << std::boolalpha << true << ' ' << false << '\n';

    // input example                
    std::istringstream iss("true false");                                       
    bool x = false, y = true;                                                   
    iss >> x >> y;                                                              
    std::cout << std::boolalpha << x << ' ' << y << '\n';                       
}
命硬 2024-10-20 19:40:27

您可以只在 main() 中使用

std::cout << std::boolalpha;

例如

int main()
{
    std::cout << std::boolalpha;
    int x {1};
    int y {2};
    bool z = y < x;
    std::cout << z << std::endl;
    
    return 0;
}

输出将为 false 而不是 0。

You could just use

std::cout << std::boolalpha;

in main().

For example

int main()
{
    std::cout << std::boolalpha;
    int x {1};
    int y {2};
    bool z = y < x;
    std::cout << z << std::endl;
    
    return 0;
}

The output will be false instead of 0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文