[cpp]
// 王智泉 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: www.2cto.com
T _val;
};
#endif
作者:xuhongwei0411