string love = "77";
int ilove = atoi(love.c_str());
int ilove = strtol(love.c_str(),NULL,10);
(2)string 转换为 uint32_t
unsigned long int strtoul (const char* str, char** endptr, int base);
string love="77";
unsigned long ul;
ul = strtoul(love.c_str(), NULL, 10);
(3)string 转换为 int64_t
string love = "77";
long long llLove=atoll(love.c_str());
(4)string 转换为 uint64_t
unsigned long long int strtoull (const char* str, char** endptr, int base);
string love="77";
unsigned long long ull;
ull = strtoull (love.c_str(), NULL, 0);
(5)string 转换为 float 或 double
string love = "77.77";
float fLove = atof(love.c_str());
double dLove = atof(love.c_str());
(6)string 转换为 long double
long double strtold(const char* str, char** endptr);
使用 C++11 引入的 C++ 库函数将 string 转换为数值类型,相应的库函数申明于头文件<string>
中。
名称 | 原型 | 说明 |
---|
stoi | int stoi (const string& str, size_t* idx = 0, int base = 10); int stoi (const wstring& str, size_t* idx = 0, int base = 10); | Convert string to integer (function template ) |
stol | long stol (const string& str, size_t* idx = 0, int base = 10); long stol (const wstring& str, size_t* idx = 0, int base = 10); | Convert string to long int (function template) |
stoul | unsigned long stoul (const string& str, size_t* idx = 0, int base = 10); unsigned long stoul (const wstring& str, size_t* idx = 0, int base = 10); | Convert string to unsigned integer (function template) |
stoll | long long stoll (const string& str, size_t* idx = 0, int base = 10); long long stoll (const wstring& str, size_t* idx = 0, int base = 10); | Convert string to long long (function template) |
stoull | unsigned long long stoull (const string& str, size_t* idx = 0, int base = 10); unsigned long long stoull (const wstring& str, size_t* idx = 0, int base = 10); | Convert string to unsigned long long (function template) |
stof | float stof (const string& str, size_t* idx = 0); float stof (const wstring& str, size_t* idx = 0); | Convert string to float (function template ) |
stod | double stod (const string& str, size_t* idx = 0); double stod (const wstring& str, size_t* idx = 0); | Convert string to double (function template ) |
stold | long double stold (const string& str, size_t* idx = 0); long double stold (const wstring& str, size_t* idx = 0); | Convert string to long double (function template) |
形参说明:
str:重载了 string 和 wstring 版本,表示被转换的字符串。
idx:表示一个size_t*
的指针类型,默认为空值。不为空时,转换成功时获取第一个非数值字符相对于首字符的偏移下标,也表示成功转换的字符数量,如 “10” 成功转为数值 10 时,*idx
的值为 2。
base:表示数值的基数,默认是 10 进制。
[1] C++ Reference
[2] strtoul.C++ Reference
[3] strtoull.C++ Reference
[4] strtold.C++ Reference
to_string(val) 把val转换成string
stoi(s,p,b) 把字符串s从p开始转换成b进制的int
stol(s,p,b) 把字符串s从p开始转换成b进制的long
stoul(s,p,b) 把字符串s从p开始转换成b进制的unsigned long
stoll(s,p,b) 把字符串s从p开始转换成b进制的long long
stoull(s,p,b) 把字符串s从p开始转换成b进制的...
使用函数模板将基本数据类型(整型、字符型、实型、布尔型)转换成string。
//ostringstream对象用来进行格式化的输出,常用于将各种类型转换为string类型
//ostringstream只支持<<操作符
template string toString(const T& t){
经常需要循环读入多组序号的图像,需要将int转换为string,简单的函数代码如下:
#include <sstream>#include <string>string IntToString(int num)
stringstream ss;
string s;
ss << num;
ss >> ...