转载地址:http://www.habadog.com/2011/05/07/boost-lexical_cast-intro/

一、lexical_cast的作用
lexical_cast使用统一的接口实现字符串与目标类型之间的转换。

二、lexical_cast与c/c++提供类似接口的比较
标准c家族中包含此类函数,例如atoi与itoa等,它们的缺点是:
(1)各个转换都是单向的,双向转换为不同函数,各种转换函数不同,接口众多;
(2)仅支持基础数据类型的子集,如int,long,double;
(3)不能提供统一的接口,易用性差;

c++中提供了stringstream,使用它进行格式转换可读性较差,使用起点较高,只是简单的转换,stringstream太重量级。

boost提供了lexical_cast,使用统一接口形式实现任意类型之间的转换,增强了易用性。但如果需要严密控制精度的转换,仍然推荐使用stringstream;数值之间的转换,推荐使用numeric_cast。

三、lexical_cast的接口形式

1
2
3
template<typename target,="" typename="" source="">
Target lexical_cast(const Source& arg);
</typename>

四、lexical_cast的样例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "iostream"
#include "boost/lexical_cast.hpp" // 需要包含的头文件
 
using boost::lexical_cast;
using boost::bad_lexical_cast;
using namespace std;
 
int main()
{
    char* p="32768";
    int i=0;
    try
    {
        i=lexical_cast<int>(p); // 将字符串转化为整数
    }
    catch(bad_lexical_cast&)    // 转换失败会抛出一个异常
    {
        i=0;
    }
    cout << i << endl;
    return i;
}
</int>

[转] boost:lexical_cast用法的更多相关文章

  1. boost/lexical_cast.hpp的简单使用方法_行动_新浪博客

    boost/lexical_cast.hpp的简单使用方法_行动_新浪博客     boost/lexical_cast.hpp的简单使用方法    (2010-03-19 16:31:13)    ...

  2. boost.lexical_cast 学习

    1,字符串 到 数值类型的转换 2,数值 到 字符串的转换 3,异常处理情况 4,boost::lexical_cast 的原型: template<typename Target, typen ...

  3. boost::lexical_cast

    boost::lexical_cast为数值之间的转换(conversion)提供了一揽子方案,比如:将一个字符串"转换成整数123,代码如下: "; int a = lexica ...

  4. Boost::lexical_cast类型转换

    1.字符串->数值 C++代码 #include <boost/lexical_cast.hpp> #include <iostream> int main() { us ...

  5. Boost::Lexical_cast 的使用

    .C++代码 #include <boost/lexical_cast.hpp> #include <iostream> int main() { using boost::l ...

  6. 用boost::lexical_cast进行数值转换

    在STL库中,我们可以通过stringstream来实现字符串和数字间的转换: int i = 0;    stringstream ss; ss << "123";  ...

  7. 初识boost之boost::share_ptr用法

    boost中提供了几种智能指针方法:scoped_ptr shared_ptr intrusive_ptr weak_ptr,而标准库中提供的智能指针为auto_ptr. 这其中,我最喜欢,使用最多的 ...

  8. [转] boost::function用法详解

    http://blog.csdn.net/benny5609/article/details/2324474 要开始使用 Boost.Function, 就要包含头文件 "boost/fun ...

  9. boost::thread用法

    最近在做一个消息中间件里面涉及到多线程编程,由于跨平台的原因我采用了boost线程库.在创建线程时遇到了几种线程创建方式现总结如下: 首先看看boost::thread的构造函数吧,boost::th ...

随机推荐

  1. error MSB6006: “CL.exe”已退出,代码为 -1073741502。

    错误1error MSB6006: “CL.exe”已退出,代码为 -1073741502.C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Plat ...

  2. 第九章-IO编程

    IO是输出输入的意思 在计算机中常用到的数据交换的地方是磁盘, 网络等 输入流是从外面(磁盘, 网络)流进内存 输出流是从内存流到外面(磁盘, 网络) 同步IO是指等待IO完成再继续执行 异步IO是在 ...

  3. ubuntu16.04 跑Apollo Demo

    1.安装docker(参考网址:https://docs.docker.com/install/linux/docker-ce/ubuntu/) Uninstall old versions Olde ...

  4. bzoj1799同类分布——数位DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1799 数位DP. 1.循环方法 预处理出每个位数上,和为某个数,模某个数余某个数的所有情况: ...

  5. POJ2406(next原理理解)

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 40448   Accepted: 16828 D ...

  6. 探究c++默认初始化

    按照c++ primer 5th第40页的描述,如果定义变量时没有指定初值,则变量被默认初始化,此时变量被赋予了“默认值”. 根据变量定义的位置,分为两种情况: 1.定义于任何函数体之外的变量被初始化 ...

  7. Flutter实战视频-移动电商-17.首页_楼层组件的编写技巧

    17.首页_楼层组件的编写技巧 博客地址: https://jspang.com/post/FlutterShop.html#toc-b50 楼层的效果: 标题 stlessW快速生成: 接收一个St ...

  8. Exists 方法

    public void ExistsMethodDemo() { string userId = "123"; string userName = "admin" ...

  9. Weekly Contest 78-------->811. Subdomain Visit Count (split string with space and hash map)

    A website domain like "discuss.leetcode.com" consists of various subdomains. At the top le ...

  10. Unity3d的批渲染 batch rendering

    http://blog.csdn.net/leonwei/article/details/41942157 批渲染(Batch) batch render 是大部分引擎提高渲染效率的方法,基本原理就是 ...