字符串转换为数据

#include <iostream>
#include <sstream>

//通过字符流数据转换
template<class T>
static T str2num(const std::string& string_tmp)
{
	std::stringstream stream(string_tmp);
	T tmp;
	stream >> tmp;
	return tmp;
}

//c++11新标准提供了一系列函数
//C++11标准允许字符串里出现非数字字符--会忽略起始的白空格,直到遇到无法转换的字符为止。
assert(std::atoi(" 100   ") == 100);	//转换int,运行有空格
static void str2int(int& int_tmp, const string& string_tmp)
{
	int_tmp = std::atoi(string_tmp.c_str());
}

assert(std::atof(" 3.1415926") == 3.1415926);	//转换float,运行有空格
static void str2float(float& float_tmp, const string& string_tmp)
{
	float_tmp = std::atof(string_tmp.c_str());
}

assert(std::stol("100L") == 100L);       //转换long, 支持L等后缀
static void str2long(long& long_tmp, const string& string_tmp)
{
	long_tmp = std::atol(string_tmp.c_str());
}

assert(std::stol("1000 9") == 1000L);    //转换long, 后面的被忽略
static void str2long(long& long_tmp, const string& string_tmp)
{
	long_tmp = std::atol(string_tmp.c_str());
}

assert(std::stod("3.14ispi") == 3.14);   //转换double, 遇到无效的字符停止
static void str2double(double& double_tmp, const string& string_tmp)
{
	double_tmp = std::atod(string_tmp.c_str());
}



//boost库中的lexical_cast,提供了一组模板,不同的模板对应不同的数据类型转换
//下面列出两种主要使用的模板类型
template <typename Target, typename Source>
inline Target lexical_cast(const Source &arg)
{
	Target result = Target();

	if (!boost::conversion::detail::try_lexical_convert(arg, result)) {
		boost::conversion::detail::throw_bad_cast<Source, Target>();
	}

	return result;
}

template <typename Target>
inline Target lexical_cast(const char* chars, std::size_t count)
{
	return ::boost::lexical_cast<Target>(
		::boost::iterator_range<const char*>(chars, chars + count)
		);
}

//下面是lexical_cast调用的基本用法
int x = boost::lexical_cast<int>("100");           //字符串->整型 100
long y = boost::lexical_cast<long>("2000");        //字符串->长整型 2000
float pai = boost::lexical_cast<float>("3.14159"); //字符串->符点型 3.14159
double e = boost::lexical_cast<double>("2.71828"); //字符串->双精度符点型 2.71828
double r = boost::lexical_cast<double>("1.414abcdefg", 5); //C字符串->双精度符点型(只转前5位) 1.141
std::string ss = boost::lexical_cast<std::string>(1234567);	//数值->字符串 1234567
std::string str = boost::lexical_cast<std::string>(0.618);	//双精度符点型->字符串 0.61799999999999999
std::string str2 = boost::lexical_cast<std::string>(0x10);	//16进制数->字符串 16
//注:字符串转数字时,字符串中不能包含除数字以外的字符(表示指数的e/E除外)。
//例如,不能将"123L"、"0x100"这样的字符串转换成数字。


//写成通用的模板形式
#include <string>
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>
template<class DTYPE>
static bool str2num(const std::string& str, DTYPE& dDefault = 0)
{
	try
	{
		dDefault = boost::lexical_cast<DTYPE>(str);
		return true;
	}
	catch(...)
	{
		std::cout << str << " 字符串转换为数据出错!!!!!" << std::endl;
		return false;
	}
}

//把字符串按指定的分隔符转换为数字
template<class T>
static bool Str2Nums(const std::string& sSrc, std:; vector<T>& aNums, const std::string sSep = " ")
{
	std::vector<std::string> vec;
	boost::split(vec, sSrc, boost::is_any_of(sSep), boost::token_compress_on);
	std::vector<std::string>::const_iterator it = vec.begin();

	for (; it != vec.end(); it++)
	{
		try
		{
			if (!it->empty())
			{
				int tmp = boost::lexical_cast<int>(it->c_str());
				aNums.push_back((T)tmp);
			}
		}
		catch (const boost::bad_lexical_cast& e)
		{
			std:; cout << sSrc << *it << e.what() << std::endl;
			return false;
		}
	}

	return true;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值