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 ...
随机推荐
- jquerymobile动态添的无索刷新
当我们使用Ajax或者javascript动态在页面上添加元素后,如添加select控件的option元素,添加ul的li元素.添加之后我们会发现,它们显示的样式不是jQuery Mobile的炫酷样 ...
- 配置apache-maven-3.6.0时所遇到的坑(一)
在命令行窗口中输入:mvn -v 或 直接 mvn 时报错:“错误: 找不到或无法加载主类 org.codehaus.plexus.classworlds.launcher.Lau ...
- dfs——n的全排列(回溯)
#include <iostream> #include <cstring> #include <string> #include <map> #inc ...
- 20155219 2016-2017-2 《Java程序设计》第3周学习总结
20155219 2016-2017-2 <Java程序设计>第3周学习总结 教材学习内容总结 数组问题 -Scanner实例,猜数字. package src.week1; import ...
- transition的用法以及animation的用法
http://www.cnblogs.com/xiaohuochai/p/5347930.html transiton: 过渡属性 过渡所需要时间 过渡动画函数 过渡延迟时间: 触发过渡 单纯的代码不 ...
- 牛客国庆集训派对Day1 L-New Game!(最短路)
链接:https://www.nowcoder.com/acm/contest/201/L 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1048576K,其他语言20 ...
- Vue的路由动态重定向和导航守卫
一.重定向 重定向也是通过 routes 配置来完成,下面例子是从 /a 重定向到 /b: const router = new VueRouter({ routes: [ { path: '/a', ...
- Redis源码剖析--列表t_list实现
Redis中的列表对象比较特殊,在版本3.2之前,列表底层的编码是 ziplist 和 linkedlist 实现的, 但是在版本3.2之后,重新引入了一个 quicklist 的数据结构,列表的底层 ...
- mac-ppt-auto-open-recovery-files
mac 每次打开PPT都会出现一个自动保存的文件,不知道这个文件是保存在哪里,该怎么删除 打开finder(访达),按 shift+command+G,输入~/Library/Containers/c ...
- Unity 5.x Shader and Effects Cookbook(2nd) (Alan Zucconi Kenneth Lammers 著)
1. Creating Your First Shader 2. Surface Shaders and Texture Mapping 3. Understanding Lighting Model ...