boost中有一个lexical_cast可以用统一的方式来做基本类型之间的转换,比如字符串到数字,数字到字符串,bool和字符串及数字之间的相互转换。boost::lexical_cast的用法比较简单:

#include <boost/lexical_cast.hpp>
#include <iostream>
#include <string> 
#define ERROR_LEXICAL_CAST     1 
int main()
{
    using boost::lexical_cast;
    int         a = 0;
    double        b = 0.0;
    std::string s = ""; 
    int            e = 0;    
    try
    { 
        // ----- 字符串 --> 数值 
        a = lexical_cast<int>("123");
        b = lexical_cast<double>("123.12");
        // ----- 数值 --> 字符串
        s = lexical_cast<std::string>("123456.7"); 
        // ----- 异常处理演示
        e = lexical_cast<int>("abc");
    }
    catch(boost::bad_lexical_cast& e)
    {
        // bad lexical cast: source type value could not be interpreted as target
        std::cout << e.what() << std::endl;
        return ERROR_LEXICAL_CAST;
    } 
    
    std::cout << a << std::endl;    // 输出:123 
    std::cout << b << std::endl;    // 输出:123.12 
    std::cout << s << std::endl;     // 输出:123456.7 
    return 0;
}
View Code

相关文章: