#include "boost\lexical_cast.hpp"
#include <vector>
#include <iostream>
#include <array> using namespace std;
using boost::lexical_cast;
using boost::bad_lexical_cast; int main()
{ // C++自带的函数不好记,且命名不统一,有些a开头有些是str开头
/* string转成其他类型
atof Convert string to double (function )
atoi Convert string to integer (function )
atol Convert string to long integer (function )
atoll Convert string to long long integer (function )
strtod Convert string to double (function )
strtof Convert string to float (function )
strtol Convert string to long integer (function )
strtold Convert string to long double (function )
strtoll Convert string to long long integer (function )
strtoul Convert string to unsigned long integer (function )
strtoull Convert string to unsigned long long integer (function )
sscanf() 其他类型转string需要完全不同的方法
stringstream strm;
strm << int_val;
string s = strm.str();
sprintf()
itoa // non-standard
*/ try
{
int s = 345;
// 只需要使用同一个函数就可以完成不同类型的转换
string str = lexical_cast<string>(s);
str = "Message: " + lexical_cast<string>('A') + "==" + lexical_cast<string>(34.5);
cout << str << endl;
//也可以转成char类型的array
array<char, 64> msg = lexical_cast< array<char, 64> >(23456); s = lexical_cast<int>("5678");
//s = lexical_cast<int>("56.78"); // bad_lexical_cast
//s = lexical_cast<int>("3456yut"); // bad_lexical_cast
s = lexical_cast<int>("3456yut", 4); //ok
cout << s << endl;
}
catch(bad_lexical_cast & e)
{
cout << "Exception caught:" << e.what() << endl;
} }

Boost--lexical_cast 一个方便安全高效的string转换库的更多相关文章

  1. Boost::Lexical_cast 的使用

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

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

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

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

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

  4. boost.lexical_cast 学习

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

  5. boost::lexical_cast

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

  6. Boost::lexical_cast类型转换

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

  7. [转] boost:lexical_cast用法

    转载地址:http://www.habadog.com/2011/05/07/boost-lexical_cast-intro/ 一.lexical_cast的作用lexical_cast使用统一的接 ...

  8. 编写一个Car类,具有String类型的属性品牌,具有功能drive; 定义其子类Aodi和Benchi,具有属性:价格、型号;具有功能:变速; 定义主类E,在其main方法中分别创建Aodi和Benchi的对象并测试对象的特 性。

    package com.hanqi.test; public class Car { //构造一个汽车的属性 private String name; //创建getter和setter方法 publ ...

  9. 一个快速、高效的Levenshtein算法实现——代码实现

    在网上看到一篇博客讲解Levenshtein的计算,大部分内容都挺好的,只是在一些细节上不够好,看了很长时间才明白.我对其中的算法描述做了一个简单的修改.原文的链接是:一个快速.高效的Levensht ...

随机推荐

  1. 使用rsync, 向另外一台服务器同步目录和文件的脚本

    #!/bin/bash #亚特兰蒂斯-同步目录#定时任务ini_file="/usr/local/sunlight/conf/rsync-file.ini"target_ip=&q ...

  2. 利用sql语句进行增删改查

    1.查询 函数:raw(sql语句) 语法:Entry.objects.raw(sql) 返回:QuerySet 2.增删改 from django.db import connection def ...

  3. Mypwd 的解读与实现 20155208

    Mypwd 的解读与实现 20155208 linux下pwd命令的编写 实验要求: 1 .学习pwd命令 2 . 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 3 .实现my ...

  4. hihocoder1489 Legendary Items 概率期望

    Little Hi is playing a video game. Each time he accomplishes a quest in the game, Little Hi has a ch ...

  5. WCF- 契约Contract(ServiceContract、OperationContract、DataContract、ServiceKnownType和DataMember)(转)

    示例 1.服务 IPersonManager.cs using System; using System.Collections.Generic; using System.Linq; using S ...

  6. Singer 学习七 运行&&开发taps、targets (二 targets 运行说明)

    接上文: Singer 学习六 运行&&开发taps.targets (一 taps 运行说明) 说明target 需要tap 进行配合运行,所以需要了解tap 的使用 运行targe ...

  7. Compoxure 微服务组合proxy 中间件

    Compoxure 是一个不错的微服务组合中间件,使用此工具我们可以快速的进行micro frontends 应用的开发 使用此工具我们可以替换esi+ ssi 的开发模型(尽管都挺不错). 同时支持 ...

  8. Introducing Makisu: Uber’s Fast, Reliable Docker Image Builder for Apache Mesos and Kubernetes

    转自:https://eng.uber.com/makisu/?amp To ensure the stable, scalable growth of our diverse tech stack, ...

  9. c++简单的ATL COM开发和调用实例

    1.打开VS2010.新建ATL COM 项目.步骤:"文件" -->"新建" -->"项目",选择"Visual C ...

  10. Unity 原厂免费资源学习