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);
阅读(4460) | 评论(0) | 转发(1) |