Boost lexical_cast[转]

该博客演示了如何使用Boost库中的lexical_cast进行字符串与数值之间的转换。包括将字符串转换为整型和浮点型,以及将浮点数和整数转换回字符串。在转换过程中,还展示了异常处理机制来捕获可能的转换错误。
摘要由CSDN通过智能技术生成

Boost lexical_cast[转]

#include <iostream>
#include <iomanip>
#include <sstream>
#include<boost/lexical_cast.hpp>

using namespace  std;

int main()
{
	using   boost::lexical_cast;
	using   boost::bad_lexical_cast;
	try
	{
		//字符串 转 整型
		//int  a = lexical_cast<int>("123");
		int  a = lexical_cast<int>("123efd", 3);
		cout << a << endl;

		//字符串 转 浮点型
		float  b = lexical_cast<float>("1.23456");
		cout << b << endl;

		//浮点数 转 为字符串
		float float_c = 1.23456f;
		string   c = lexical_cast<string>(float_c);
		cout << c << endl;

		//整数转为字符串
		int int_d = 12345;
		string   d = lexical_cast<string>(int_d);
		cout << d << endl;
	}
	catch (const bad_lexical_cast &  e)
	{
		cout << e.what() << endl;
	}

#if 0
	double dble = 123456.7893232338;
	std::stringstream  ss;
	ss.precision(20);
	ss << setw(15) << dble;
	string ssd = ss.str();
	cout << ssd << endl;
#endif

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值