See there are many implementations of this on the web, and will involve the processing of details. For the convenience of later use, this provides functions that can be used directly.
Resources:
1: Explain C + + StringStream (Details): http://blog.csdn.net/leonardwang/article/details/4881122
2:c++ template (very detailed, there are examples): http://www.cnblogs.com/gaojun/archive/2010/09/10/1823354.html
1#include <iostream>2#include <sstream>3 using namespacestd;4 5 6 //because StringStream constructs and parsing functions are time-consuming, try to create only one. 7Template <classType>//the type to be converted with string8Type Stringandtype (Type strtmp) {//string and other types convert to each other9 StringStream stream;TenStream <<strtmp; One Type typetmp; AStream >>typetmp; -Stream.clear ();//just reset the status flag of the StringStream and did not clear the data -STREAM.STR ("");//Clear Data the returntypetmp; - } - - intMain () {//Test + - stringstrtmp; + intinttmp; A __int64 int64tmp; at Doubledbetmp; - - while(true) { -cout <<"-----int to string-----"<<Endl; -CIN >>inttmp; -cout << Stringandtype (inttmp) <<Endl; incout <<"-----String to int-----"<<Endl; -CIN >>strtmp; tocout << Stringandtype (strtmp) <<Endl; + - thecout <<"-----Double to string-----"<<Endl; *CIN >>dbetmp; $cout << Stringandtype (dbetmp) <<Endl;Panax Notoginsengcout <<"-----String to double-----"<<Endl; -CIN >>strtmp; thecout << Stringandtype (strtmp) <<Endl; + A thecout <<"-----Int64 to string-----"<<Endl; +CIN >>int64tmp; -cout << Stringandtype (int64tmp) <<Endl; $cout <<"-----String to Int64-----"<<Endl; $CIN >>strtmp; -cout << Stringandtype (strtmp) <<Endl; -cout <<"--------------------------End-------------------------"<<Endl; the } - return 0;Wuyi}
Conversion of C + + string to int and other types