首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用boost::lexical_cast和std::boolalpha?即boost::lexical_cast< bool >("true")

如何使用boost::lexical_cast和std::boolalpha?即boost::lexical_cast< bool >("true")
EN

Stack Overflow用户
提问于 2010-12-16 00:13:04
回答 3查看 21.3K关注 0票数 20

我已经看到了其他boost::lexical_cast问题的一些答案,它们断言以下问题是可能的:

代码语言:javascript
复制
bool b = boost::lexical_cast< bool >("true");

这不适用于我的g++ 4.4.3 boost 1.43。(也许它在默认设置了std::boolalpha的平台上工作是真的)

This是一个很好的字符串到布尔问题的解决方案,但是它缺少boost::lexical_cast提供的输入验证。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-12-18 04:21:42

我在这里发布了我自己的问题的答案,供其他可能正在寻找这样的东西的人使用:

代码语言:javascript
复制
struct LocaleBool {
    bool data;
    LocaleBool() {}
    LocaleBool( bool data ) : data(data) {}
    operator bool() const { return data; }
    friend std::ostream & operator << ( std::ostream &out, LocaleBool b ) {
        out << std::boolalpha << b.data;
        return out;
    }
    friend std::istream & operator >> ( std::istream &in, LocaleBool &b ) {
        in >> std::boolalpha >> b.data;
        return in;
    }
};

用法:

代码语言:javascript
复制
#include <boost/lexical_cast.hpp>
#include <iostream>
#include "LocaleBool.hpp"

int main() {
    bool b = boost::lexical_cast< LocaleBool >("true");
    std::cout << std::boolalpha << b << std::endl;
    std::string txt = boost::lexical_cast< std::string >( LocaleBool( b ) );
    std::cout << txt << std::endl;
    return 0;
}
票数 16
EN

Stack Overflow用户

发布于 2013-07-18 03:43:15

除了答案形式poindexter之外,您还可以将来自here的方法包装在boost::lexical_cast的专用版本中

代码语言:javascript
复制
namespace boost {
    template<> 
    bool lexical_cast<bool, std::string>(const std::string& arg) {
        std::istringstream ss(arg);
        bool b;
        ss >> std::boolalpha >> b;
        return b;
    }

    template<>
    std::string lexical_cast<std::string, bool>(const bool& b) {
        std::ostringstream ss;
        ss << std::boolalpha << b;
        return ss.str();
    }
}

并使用它:

代码语言:javascript
复制
#include <iostream>
#include <boost/lexical_cast.hpp>

//... specializations

int main() {
    bool b = boost::lexical_cast<bool>(std::string("true"));
    std::cout << std::boolalpha << b << std::endl;
    std::string txt = boost::lexical_cast< std::string >(b);
    std::cout << txt << std::endl;

    return 0;
}

我个人喜欢这种方法,因为它隐藏了任何特殊的代码(例如,使用链接中的LocaleBoolto_bool(...) )来转换为bools。

票数 16
EN

Stack Overflow用户

发布于 2013-10-23 00:14:31

将您自己的模板放在boost lexical cast之上,以便进行解析。请注意示例中的"default“参数,以确保重载正常工作(如果需要,可以使用其他方法)。

代码语言:javascript
复制
template<typename T>
T Parse(const std::string& valStr, const T& default=T()) {
   T result = boost::lexical_cast<T>(valStr);
}

然后,您可以专攻任何东西,包括bools:

代码语言:javascript
复制
template<>
bool Parse(const std::string& valStr, const bool& default=true) {
   if(strcmp(valStr.c_str(), "true") == 0) {
       return true;
   }
   return false;
}

显然,有许多方法可以做到这一点,你可以为true和false添加更多的条件(我会确保"TRUE“和"FALSE”的所有变体,比如"True",加上"T“和"F”都能正常工作)。您甚至可以将其扩展到数值解析。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4452136

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档