Boost--lexical_cast 一个方便安全高效的string转换库
#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转换库的更多相关文章
- Boost::Lexical_cast 的使用
.C++代码 #include <boost/lexical_cast.hpp> #include <iostream> int main() { using boost::l ...
- 用boost::lexical_cast进行数值转换
在STL库中,我们可以通过stringstream来实现字符串和数字间的转换: int i = 0; stringstream ss; ss << "123"; ...
- boost/lexical_cast.hpp的简单使用方法_行动_新浪博客
boost/lexical_cast.hpp的简单使用方法_行动_新浪博客 boost/lexical_cast.hpp的简单使用方法 (2010-03-19 16:31:13) ...
- boost.lexical_cast 学习
1,字符串 到 数值类型的转换 2,数值 到 字符串的转换 3,异常处理情况 4,boost::lexical_cast 的原型: template<typename Target, typen ...
- boost::lexical_cast
boost::lexical_cast为数值之间的转换(conversion)提供了一揽子方案,比如:将一个字符串"转换成整数123,代码如下: "; int a = lexica ...
- Boost::lexical_cast类型转换
1.字符串->数值 C++代码 #include <boost/lexical_cast.hpp> #include <iostream> int main() { us ...
- [转] boost:lexical_cast用法
转载地址:http://www.habadog.com/2011/05/07/boost-lexical_cast-intro/ 一.lexical_cast的作用lexical_cast使用统一的接 ...
- 编写一个Car类,具有String类型的属性品牌,具有功能drive; 定义其子类Aodi和Benchi,具有属性:价格、型号;具有功能:变速; 定义主类E,在其main方法中分别创建Aodi和Benchi的对象并测试对象的特 性。
package com.hanqi.test; public class Car { //构造一个汽车的属性 private String name; //创建getter和setter方法 publ ...
- 一个快速、高效的Levenshtein算法实现——代码实现
在网上看到一篇博客讲解Levenshtein的计算,大部分内容都挺好的,只是在一些细节上不够好,看了很长时间才明白.我对其中的算法描述做了一个简单的修改.原文的链接是:一个快速.高效的Levensht ...
随机推荐
- [LeetCode&Python] Problem 122. Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Socket远程桌面
自建Socket转发,使用远程桌面(mstsc)连接家中电脑 网络结构图如下: 开题先放图,一切全靠编哈哈. 进入正题! 如图所示,我们需要一个公网服务器,利用公网服务器将内网的数据进行转发,从而 ...
- rest-framework之响应器(渲染器)
rest-framework之响应器(渲染器) 本文目录 一 作用 二 内置渲染器 三 局部使用 四 全局使用 五 自定义显示模版 回到目录 一 作用 根据 用户请求URL 或 用户可接受的类型,筛选 ...
- C++学习(一)之Visual Studio安装以及首次使用
一.安装Visual Studio 首先下载Visual Studio 链接: http://pan.baidu.com/s/1pLhJt0Z 密码:uqyc 将.ios文件解压得到以下文件: 点击v ...
- Go Example--map
package main import "fmt" func main() { //初始化map make(map[类型][类型]) m:= make(map[string]int ...
- 工作记录 - OBB的解决方案
之前关于OBB的内容: Android上使用native IO 最近工作中的问题笔记 工作记录[续] android OBB 自从用了Java来mount OBB, 再也没有遇到挂载的问题. 但最近在 ...
- 浮动IP(Floating IPs):开始构建你的高可用性的应用
高可用性是所有生产环境的关键.开发者因此可以高枕无忧因为他们知道他们的应用被设计为可以承受住故障. 今天,我们非常激动的宣布我们应用了浮动IP技术.浮动IP指的是一个IP地址可以立即从一个Drople ...
- 改变codeblocks里面各种注释的颜色。
->Setting->Editor->Syntax highlighting->“Comment(normal)” /* 改变块注释颜色 */"Comment lin ...
- ESB雏形 -- 项目企业服务总线初始
今天要厚着脸皮给大家推荐一个自己做的通信中间件——ServiceAnt,目前已经在我们团队的两个产品线上投入了使用. ServiceAnt是什么 它最初的定位是ESB(企业服务总线),但目前还没有达到 ...
- MySQL插入,更新,删除数据
插入 单行插入 1.insert into 表名 values(col1_value,col2_value,...); 每个列必须提供一个值,如果没有值,要提供NULL值 每个列必须与它在表中定义的次 ...