boost::lexical_cast库的使用

boost::lexical_cast库是用来各种类型进行转换的,但是要注意一下几点:

1、lexical_cast依赖于字符流std::stringstream,其原理相当简单:把源类型读入到字符流中,再写到目标类型中,就大功告成。

2、 boost::lexcial_cast。当然,lexical_cast不仅仅局限于字符串类型与数值类型之间的转换:可在任意可输出到stringstream的类型和任意可从stringstream输入的类型间转换。

应用例子:

1 /**                                                                                                                                                      
  2 @author Amiber
  3 @date 2012-12-16
  4 @brief boost::lexical_cast
  5 **/
  6 
  7 #include <iostream> //for std::cout
  8 #include <boost/lexical_cast.hpp>
  9 
 10 
 11 int main(int argc,char* argv[])
 12 {
 13 
 14 
 15         /**
 16         boost::lexical_cast
 17         for simple-datatype
 18         **/
 19         std::cout<<boost::lexical_cast<int>(argv[1])<<std::endl;
 20 
 21         /**
 22         boost::lexical_cast
 23         **/
 24 
 25         std::cout<<boost::lexical_cast<double>(argv[2])<<std::endl;
 26 
 27 
 28         double a= 201.12;
 29 
 30         std::cout<<boost::lexical_cast<std::string>(a)<<std::endl;
 31 
 32         return 0;
 33 }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值