zoukankan      html  css  js  c++  java
  • [boost] boost::lexical_cast类型转换

    boost是一个开源的c++开发库,提供了一些通用库用来弥补c++标准库的不足。

    boost::lexical_cast是一个类型转换库,可用于不同类型之间的转换,字符串、整型数等。

    具体使用例子如下:

    #include <iostream>
    #include <boost/lexical_cast.hpp>

    int main(int argc, char* argv[])
    {
        std::string strNum = "1.0";
        float num = boost::lexical_cast<float>(strNum);      // 从std:::string 转成浮点数

        strNum = "2.49484984abd";
        try
        {
            num = boost::lexical_cast<float>(strNum);         // 如果该字符串不是一个浮点数,则会报异常,需要用try-catch捕获
        }
        catch(boost::bad_lexical_cast&  ex)
        {
            std::cout << "Failed to change type." << std::endl;
        }

        num = 2.484;
        std::string  s2  = boost::lexical_cast<std::string>(num);

        std::wstring  w2  =  L"2.498494894";                       // 双字节字符串转成浮点数
        float wNum = boost::lexical_cast<float>(w2);
        return 0;
    }

  • 相关阅读:
    Lucene.net系列六 search 下
    Lucene.net 系列三 index 中
    初识Antlr
    Antlr首页计算机器实验成功
    C#语言学习之旅(1):C#基础
    NeatUpload js 判断上传文件的大小是否超过了空间的大小
    对XML的各种操作
    多表求和
    xmlhttp 最简单的无刷新
    xml 查询
  • 原文地址:https://www.cnblogs.com/ityujian/p/3027191.html
Copyright © 2011-2022 走看看