SSCANF, sprintf can also be used, but not recommended, in fact C + + implementation of string and other data types of conversion is easy to complete, see the following code:
[CPP] View plain copy #include <iostream> using std::cout; using std:: endl; #include <string> using std::string; # include <sstream> using std::stringstream; Using std::ostringstream ; template <class t> string tostring (const t& s); int main () { string value = ""; int inum = 0; double dnum = 0.0; value = "123"; stringstream strStream; strStream << value; strstream >> inum; strstream.clear (); // must be clear , otherwise the next call is unsuccessful value = "123.45"; strStream << value; strStream >> dnum; cout << inum << " " << dnum <<endl; string test = ""; test = ToString (dnum); cout << test << endl; return 0; } //other types converted to String type template <class t> string tostring (const T& s) { ostringstream os; os << s; return os.str (); }
Note: If the data in the text is a scientific enumeration, for example, the text is: 1.44e-07 can still normal conversion.
Reference:
Istringstream, Ostringstream, stringstream implementation data type into string