如果你是在C++11之前,可能比較麻煩
string->int (#include <cstdlib>)
int->string
1.使用stringstream (#include <sstream>)
2.使用sprintf
而C++11中提供了更簡單的方案:
string->int
函數原型在這
int stoi (const string& str, size_t* idx = 0, int base = 10);
就是要注意的是,這裏的idx是個指針,而非下標,指向開始轉換的地方
當然,你也可以很自然的想到,還有
- stol
- Convert string to long int (function template )
- stoul
- Convert string to unsigned integer (function template )
- strtol
- Convert string to long integer (function )
- stof
- Convert string to float (function template )
- stod
- Convert string to double (function template )
int->string
轉成string SO EASY~