当前位置: 首页 > news >正文

建站模板工程造价/代刷网站推广

建站模板工程造价,代刷网站推广,页面做的好看的网站,罗湖区网站建设模块功能 Lexical_Cast模块提供了一种方便且一致的形式用于支持与任意类型对应的文本域其值之间的通用转换。 版本说明 本文中介绍的Lexical_Cast模块内容是基于Boost 1.56.0的。 模块用法 字符串转数值 #include <boost/lexical_cast.hpp> #include <iostream&…

模块功能

Lexical_Cast模块提供了一种方便且一致的形式用于支持与任意类型对应的文本域其值之间的通用转换。

版本说明

本文中介绍的Lexical_Cast模块内容是基于Boost 1.56.0的。

模块用法

字符串转数值

#include <boost/lexical_cast.hpp>
#include <iostream>using namespace std;int main() {using boost::bad_lexical_cast;using boost::lexical_cast;using boost::conversion::try_lexical_convert;try {// 测试使用lexical_castshort s = lexical_cast<short>("123");cout << s << endl;// 测试使用try_lexical_convertfloat f;for (auto str : {"abc", "1.2"}) {if (try_lexical_convert<float>(str, f)) {cout << "Succes to convert \"" << str << "\" to " << f << endl;} else {cout << "Fail to convert \"" << str << "\" to float" << endl;}}// 测试抛出bad_lexical_cast异常double d = lexical_cast<double>("123abc");cout << d << endl;} catch (const bad_lexical_cast& ex) {cout << ex.what() << endl;}
}

上面代码的运行结果如下:

123
Fail to convert "abc" to float
Succes to convert "1.2" to 1.2
bad lexical cast: source type value could not be interpreted as target

 

数值转字符串

#include <boost/lexical_cast.hpp>
#include <iostream>
#include <string>using namespace std;int main() {using boost::bad_lexical_cast;using boost::lexical_cast;using boost::conversion::try_lexical_convert;//测试使用lexical_castconst auto s1 = lexical_cast<string>(1.532e5);cout << s1 << endl;string s2;int val = 123;//测试使用try_lexical_convertif (try_lexical_convert<string>(val, s2)) {cout << "Success to convert " << val << " to \"" << s2 << "\"" << endl;} else {cout << "Fail to convert " << val << " to string" << endl;}
}

上面程序的运行结果如下:

153200

自定义类转字符串

#include <boost/lexical_cast.hpp>
#include <iostream>
#include <string>using namespace std;class Point {public:Point(float x, float y) : x_(x), y_(y) {}friend ostream &operator<<(ostream &output, const Point &p) {output << "(" << p.x_ << ", " << p.y_ << ")";return output;}private:const float x_;const float y_;
};int main() {using boost::bad_lexical_cast;using boost::lexical_cast;const auto s = lexical_cast<string>(Point(1.1f, 2.2f));cout << s << endl;
}

上面程序的运行结果如下:

(1.1, 2.2)

字符串转自定义类

class Point {public:friend istream &operator>>(istream &input, Point &p) {input >> p.x_;return input;}friend ostream &operator<<(ostream &output, const Point &p) {output << "Point(" << p.x_ << ")";return output;}Point() { x_ =0; }Point(const Point &pt) {x_ = pt.x_;}private:float x_;
};int main() {using boost::bad_lexical_cast;using boost::lexical_cast;const auto p = lexical_cast<Point>(string("3.3"));cout << p << endl;
}

上面程序的运行结果如下:

Point(3.3)

为什么不使用标准库里的转换

标准C和c++库提供了许多执行此类转换的工具。但是,它们在易用性、可扩展性和安全性方面有所不同。

例如,由atoi代表的标准C函数家族有许多限制:

  • 只支持单向转换:从文本到内部数据类型。使用C库以另一种方式进行转换,要么会带来sprintf函数的不便和安全问题,要么会丧失与itoa等非标准函数相关的可移植性;
  • 支持的类型范围仅是内置数值类型的子集,即int、long和double;
  • 类型的范围不能以统一的方式扩展。例如,将字符串转换成复数(Complex)或有理数(Rational)。

以strtol为代表的标准C函数具有相同的基本限制,但对转换过程提供了更好的控制。然而,对于一般情况,这种控制通常不是不需要就是不使用。

scanf函数家族提供了更强大的控制,但也缺乏安全性和易用性。

标准c++库的stringstream对I/O与文本任意类型之间的格式和转换提供了大量的控制。然而,对于简单的转换,直接使用stringstream可能是笨拙的(引入额外的局部变量并失去了插入表达式的便捷性)或隐晦的(在表达式中创建stringstream对象作为临时对象)。

标准c++库的facet为控制文本表示提供了全面的概念和工具,但是的概它们的复杂性和高入门级别要求对简单的转换进行大量编码,并且除了少数程序员外,所有人都不能参与。

参考文章

Chapter 18. Boost.Lexical_Cast 1.0

查看全文http://www.xqgm.cn/news/2701.html

相关文章:

  • 安徽美丽乡村张家口网站建设哪家服务好
  • 高校邦营销型网站建设测验答案
  • 网站建设主要包括什么
  • 曹妃甸建设局网站
  • 户县住房和城乡建设局官方网站
  • 廊坊视频优化推广/太原seo快速排名
  • 学做衣服网站知乎/淘宝关键词挖掘工具
  • 做网络销售怎么建立网站/千峰培训可靠吗?
  • 做网站得每年续费吗/黄页推广平台有哪些
  • 制作自己的个人网站/互联网公司有哪些
  • 百度右边相关网站怎么做的/外链交换平台
  • 模板网站配置文件/查域名的网址
  • 北京网站建设设计/最佳的资源磁力搜索引擎
  • 能免费做网站吗/友情连接
  • 青海网站制作哪家好/百度云盘资源共享链接群组链接
  • 网站服务器租赁费高吗/电商运营平台
  • 做网站怎么做鼠标跟随/谷歌海外广告投放
  • 大连建设网缴费查询/seo如何优化关键词上首页
  • 一般电商都是在哪些网站上做/优化大师官网
  • 网站视频背景怎么做/商城推广
  • 域名为www.com的网站/网店推广是什么
  • 租好服务器咋做网站呢/有域名有服务器怎么做网站
  • wordpress优点缺点/小程序seo推广技巧
  • linux打包网站做备份/什么软件可以免费引流
  • 网站建设 功能需求/巨量引擎广告投放平台
  • 用vs做html网站/推广什么软件可以长期赚钱
  • 安阳网络推广/互联网seo是什么
  • 网站开发域名/手机网站百度关键词排名
  • 帝国网站后台编辑器没有了/长沙靠谱的关键词优化
  • asp网站木马扫描/友情链接源码
  • 片多多免费观看高清/品牌seo是什么
  • h5自适应网站模板下载/互联网网络推广公司
  • 上海个人网站制作公司/google关键词工具
  • 微信怎样建立公众号/seo的优化步骤
  • 东营做网站公司/怎样建立一个自己的网站
  • 相关文章: