首页 > 其他 > 详细

boost lexical_cast

时间:2019-06-05 11:43:15      阅读:92      评论:0      收藏:0      [点我收藏+]

Boost.LexicalCast provides a cast operator, boost::lexical_cast, that can covert numberes from strings to numeric types like int or double.

#include <boost/lexical_cast.hpp>
#include <string>
#include <iostream>

int main() {
  std::string s = boost::lexical_cast<std::string>(123);
  std::cout << s << std::endl;
  double d = boost::lexical_cast<double>(s);
  std::cout << d << std::endl;
  return 0;
}

boost::lexical_cast uses streams internally to perform the conversion. Therefore, only types with overloader operator<< and operator>> can be converted.

If a conversion fails, an exception of type boost::bad_lexical_cast, which is derived from std::bad_cast, is thrown.

boost lexical_cast

原文:https://www.cnblogs.com/sssblog/p/10978528.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!