inline std::string stringify(double x)
{
  std::ostringstream o;
  if (!(o << x))
    throw BadConversion("stringify(double)");
  return o.str();
}

template<typename T>
inline T convertToNumber(std::string const& s,bool failIfLeftoverChars = true)
{
  std::istringstream i(s);
  T x;
  char c ;
  if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
    throw BadConversion("convertToDouble(\"" + s + "\")");
  return x;
}


int main()
{
    std::string s("12345.6789") ;
    double d = convertToNumber<double>(s) ;
    printf("(%f) \n",d) ;
}

http://www.parashift.com/c++-faq-lite/convert-string-to-num.html

arrow
arrow
    全站熱搜

    hedgezzz 發表在 痞客邦 留言(0) 人氣()