天天看點

C++11中的to_string

C++11之前,标準庫沒有提供數字類型轉字元串的函數,需要借助sprintf、stringstream等,現在C++11提供了std::to_string函數,可以直接使用了:

點選(此處)折疊或打開

string to_string (int val);

string to_string (long val);

string to_string (long long val);

string to_string (unsigned val);

string to_string (unsigned long val);

string to_string (unsigned long long val);

string to_string (float val);

string to_string (double val);

string to_string (long double val);

除此之外,C++11标準庫還引入了字元串轉整數系列函數:

int stoi (const string& str, size_t* idx = 0, int base = 10);

long stol (const string& str, size_t* idx = 0, int base = 10);

long long stoll (const string& str, size_t* idx = 0, int base = 10);

unsigned long stoul (const string& str, size_t* idx = 0, int base = 10);

unsigned long long stoull (const string& str, size_t* idx = 0, int base = 10);

double stod (const string& str, size_t* idx = 0);

float stof (const string& str, size_t* idx = 0);

繼續閱讀