boost/lexical_cast.hpp的简单使用方法_学着站在巨人的肩膀上_百度空间

摘要:
C++代码intd=boost::legal_cast<int>;intd=boost::lexical_cast<int>;等效于C++代码intd;标准::字符串流;s˂ ˃d;整数;标准::字符串流;s˂ ˃d;5.总结我们经历了增强::legal_cast当然,legal_Cast不限于字符串类型和数字类型之间的转换:它可以在可以输出到stringstream的任何类型和可以从stringstreem输入的任何类型之间进行转换。

boost/lexical_cast.hpp的简单使用方法_学着站在巨人的肩膀上_百度空间

boost/lexical_cast.hpp的简单使用方法

1、字符串->数值
C++代码
  1. #include <boost/lexical_cast.hpp>
  2. #include <iostream>
  3. int main()
  4. {
  5. using boost::lexical_cast;
  6. int a = lexical_cast<int>("123");
  7. double b = lexical_cast<double>("123.12");
  8. std::cout<<a<<std::endl
  9. std::cout<<b<<std::endl;
  10. return 0;
  11. }

#include <boost/lexical_cast.hpp>#include <iostream>int main(){ using boost::lexical_cast; int a = lexical_cast<int>("123"); double b = lexical_cast<double>("123.12"); std::cout<<a<<std::endl std::cout<<b<<std::endl; return 0;}
2、数值->字符串
C++代码

  1. #include <boost/lexical_cast.hpp>
  2. #include <string>
  3. #include <iostream>
  4. int main()
  5. {
  6. using std::string;
  7. constdouble d = 123.12;
  8. string s = boost::lexical_cast<string>(d);
  9. std::cout<<s<<std::endl;
  10. return 0;
  11. }

#include <boost/lexical_cast.hpp>#include <string>#include <iostream>int main(){ using std::string; const double d = 123.12; string s = boost::lexical_cast<string>(d); std::cout<<s<<std::endl; return 0;}
3、异常
C++代码

  1. #include <boost/lexical_cast.hpp>
  2. #include <iostream>
  3. int main()
  4. {
  5. using std::cout;
  6. using std::endl;
  7. int i;
  8. try
  9. {
  10. i = boost::lexical_cast<int>("xyz");
  11. }
  12. catch(boost::bad_lexical_cast& e)
  13. {
  14. cout<<e.what()<<endl;
  15. return 1;
  16. }
  17. cout<<i<<endl;
  18. return 0;
  19. }

#include <boost/lexical_cast.hpp>#include <iostream>int main(){ using std::cout; using std::endl; int i; try { i = boost::lexical_cast<int>("xyz"); } catch(boost::bad_lexical_cast& e) { cout<<e.what()<<endl; return 1; } cout<<i<<endl; return 0;}
显然“xyz”并不能转换为一个int类型的数值,于是抛出异常,捕捉后输出“bad lexical cast: source type value could not be interpreted as target”这样的信息。
4、注意事项
lexical_cast依赖于字符流std::stringstream,其原理相当简单:把源类型读入到字符流中,再写到目标类型中,就大功告成。
C++代码

  1. int d = boost::lexical_cast<int>("123");

int d = boost::lexical_cast<int>("123");
相当于
C++代码

  1. int d;
  2. std::stringstream s;
  3. s<<"123";
  4. s>>d;

int d;std::stringstream s;s<<"123";s>>d;
5、小结
我们已经体验了boost::lexcial_cast。当然,lexical_cast不仅仅局限于字符串类型与数值类型之间的转换:可在任意可输出到stringstream的类型和任意可从stringstream输入的类型间转换。
;-)

免责声明:文章转载自《boost/lexical_cast.hpp的简单使用方法_学着站在巨人的肩膀上_百度空间》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇图像识别模型教你用神经网络求解高级数学方程!下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

C++中stringstream的使用方法和样例

  之前在leetcode中进行string和int的转化时使用过istringstream,现在大致总结一下用法和测试用例。     介绍:C++引入了ostringstream、istringstream、stringstream这三个类,要使用他们创建对象就必须包含sstream.h头文件。 istringstream类用于执行C++风格的串流的输...

将图片以字符串方式保存

将图片转换成字符   delphi的*.dfm文件 mht的文件   //------------------------------------------------------------------------------//jpg转换为 txt字符串 //JpegToText('C:1.jpg', 'C:1.txt');function Jpe...

stringstream用法

分为istream和ostringstream. 1 std::string name("zeta");2 int age = 27;3 4 ostringstream os;5 os << "name:"<<name<<""<<"age:"<<age<<...