字符串数值互转

// 王智泉 2012/06/13

#ifndef __StringHelper__H__
#define __StringHelper__H__

#include <string>
#include <sstream>

#define iToStdS(v) ValueToString<int>(v).str()
#define fToStdS(v) ValueToString<float>(v).str()
#define dToStdS(v) ValueToString<double>(v).str()

#define StdSToI(v) StringToValue<int>(v).value()
#define StdSToF(v) StringToValue<float>(v).value()
#define StdSToD(v) StringToValue<double>(v).value()

// 数值转字符串
template<typename T>
class ValueToString
{
public:

	ValueToString(T v)
	{
		std::stringstream strem;
		strem << v;
		_str = strem.str();
	}

	std::string& str()  
	{ return _str; }

private:
	std::string _str;
};

// 字符串转数值
template<typename T>
class StringToValue
{
public:

	StringToValue(const std::string& s)
	{
		std::stringstream strem(s);
		strem >> _val;
	}

	T value()
	{
		return _val;
	}

private:

	T _val;
};

#endif

 

你可能感兴趣的:(字符串数值互转)