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 350. Intersection of Two Arrays II
Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...
- Unity游戏开发常用的一些函数用法
Unity游戏开发常用函数 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分享. ...
- 如何在git上创建的本地仓库
一.安装git(在git) 二. 三.输入个人信息(代码提交者) git config --global user.name "xxxx" git config --global ...
- TP thinkphp 权限管理 权限认证 功能
(如有打扰,请忽略)阿里云ECS大羊群,2U4G低至1.4折,限实名新用户,需要的点吧https://promotion.aliyun.com/ntms/act/vm/aliyun-group/tea ...
- iostat iotop 查看硬盘的读写、 free 查看内存的命令 、netstat 命令查看网络、tcpdump 命令
iostat 命令 查看硬盘的使用情况: iostat iostat -x iotop 命令: 若没安装先安装: yum install iotop -y free 命令,用于查看内存的使用量: fr ...
- RESTful规范(一)
一.学习restframework之前准备 1.json格式若想展示中文,需要ensure_ascii=False import json dic={'name':'你好'} print(json.d ...
- linux freopen函数
编程之路刚刚开始,错误难免,希望大家能够指出. 有些需求需要我们不断的输入数据很庞大,如果我们安装常规方法不断地在终端输入值很麻烦(前提是输入的数据是固定的,并不会随程序的运行而改变),这个时候我们就 ...
- oracle数据库经常提示27102 out of memory解决方法
网上有很多解决方案,但是我一个也看不懂,不知道写的什么鬼的东西,为什么一句话就能解决的事,非得长篇大论说原理是什么,看得一脸懵逼 我的解决方法入戏 vi /etc/sysctl.conf 修改kern ...
- 使用uflare/smtp2http 将smtp 转转化为http 请求
uflare/smtp2http 是一个很不错的工具,我们使用这个工具,可以快速的将smtp 服务转换为http 服务 用途实际上挺多的 devops 系统 需要使用smtp的系统(测试) 基于smt ...
- Space Shooter 学习
using UnityEngine; using System.Collections; /// <summary> /// 背景滚动 /// </summary> publi ...