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
解析str,将其内容解释为指定基数的整数,该整数作为int值返回。 如果idx不是空指针,函数还会将idx的值设置为str中数字后面第一个字符的位置。 函数使用strtol(或wcstol)来执行转换(有关过程的更多详细信息,请参阅strtol)。
// stoi example
#include <iostream> // std::cout
#include <string> // std::string, std::stoiint main ()
{std::string str_dec = "2001, A Space Odyssey";std::string str_hex = "40c3";std::string str_bin = "-10010110001";std::string str_auto = "0x7f";std::string::size_type sz; // alias of size_tint i_dec = std::stoi (str_dec,&sz);int i_hex = std::stoi (str_hex,nullptr,16);int i_bin = std::stoi (str_bin,nullptr,2);int i_auto = std::stoi (str_auto,nullptr,0);std::cout << str_dec << ": " << i_dec << " and [" << str_dec.substr(sz) << "]\n";std::cout << str_hex << ": " << i_hex << '\n';std::cout << str_bin << ": " << i_bin << '\n';std::cout << str_auto << ": " << i_auto << '\n';return 0;
}
Output:
2001, A Space Odyssey: 2001 and [, A Space Odyssey] 40c3: 16579 -10010110001: -1201 0x7f: 127 |
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
解析str,将其内容解释为指定基数的整数,该整数作为long int类型的值返回。 如果idx不是空指针,函数还会将idx的值设置为str中数字后面第一个字符的位置。 函数使用strtol(或wcstol)来执行转换(有关过程的更多详细信息,请参阅strtol)。
// stol example
#include <iostream> // std::cout
#include <string> // std::string, std::stolint main ()
{std::string str_dec = "1987520";std::string str_hex = "2f04e009";std::string str_bin = "-11101001100100111010";std::string str_auto = "0x7fffff";std::string::size_type sz; // alias of size_tlong li_dec = std::stol (str_dec,&sz);long li_hex = std::stol (str_hex,nullptr,16);long li_bin = std::stol (str_bin,nullptr,2);long li_auto = std::stol (str_auto,nullptr,0);std::cout << str_dec << ": " << li_dec << '\n';std::cout << str_hex << ": " << li_hex << '\n';std::cout << str_bin << ": " << li_bin << '\n';std::cout << str_auto << ": " << li_auto << '\n';return 0;
}
Output:
1987520: 1987520 2f04e009: 788848649 -11101001100100111010: -956730 0x7fffff: 8388607 |
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
分析str,将其内容解释为指定基数的整数,该整数作为无符号长值返回。 如果idx不是空指针,函数还会将idx的值设置为str中数字后面第一个字符的位置。 该函数使用strtoul(或wcstoul)来执行转换(有关该过程的更多详细信息,请参阅strtol)。
// stoul example
#include <iostream> // std::cin, std::cout
#include <string> // std::string, std::stoul, std::getlineint main ()
{std::string str;std::cout << "Enter an unsigned number: ";std::getline (std::cin,str);unsigned long ul = std::stoul (str,nullptr,0);std::cout << "You entered: " << ul << '\n';return 0;
}
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
解析str,将其内容解释为指定基数的整数,该整数作为long-long类型的值返回。如果idx不是空指针,函数还会将idx的值设置为str中数字后面第一个字符的位置。该函数使用strtoll(或wcstoll)来执行转换(有关该过程的更多详细信息,请参阅strtol)。
// stoll example
#include <iostream> // std::cout
#include <string> // std::string, std::stollint main ()
{std::string str = "8246821 0xffff 020";std::string::size_type sz = 0; // alias of size_twhile (!str.empty()) {long long ll = std::stoll (str,&sz,0);std::cout << str.substr(0,sz) << " interpreted as " << ll << '\n';str = str.substr(sz);}return 0;
}
Output:
8246821 interpreted as 82468210xffff interpreted as 65535020 interpreted as 16 |
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
分析str,将其内容解释为指定基数的整数,该整数作为unsigned long-long类型的值返回。 如果idx不是空指针,函数还会将idx的值设置为str中数字后面第一个字符的位置。 该函数使用strtoull(或wcstoull)来执行转换(有关该过程的更多详细信息,请参阅strtol)。
// stoull example
#include <iostream> // std::cout
#include <string> // std::string, std::stoullint main ()
{std::string str = "8246821 0xffff 020 -1";std::string::size_type sz = 0; // alias of size_twhile (!str.empty()) {unsigned long long ull = std::stoull (str,&sz,0);std::cout << str.substr(0,sz) << " interpreted as " << ull << '\n';str = str.substr(sz);}return 0;
}
Possible output:
8246821 interpreted as 82468210xffff interpreted as 65535020 interpreted as 16-1 interpreted as 18446744073709551615 |
stof
float stof (const string& str, size_t* idx = 0);float stof (const wstring& str, size_t* idx = 0);
Convert string to float
分析str,将其内容解释为浮点数,该浮点数作为float类型的值返回。 如果idx不是空指针,函数还会将idx的值设置为str中数字后面第一个字符的位置。 该函数使用strtod(或wcstod)来执行转换(有关该过程的更多详细信息,请参阅strtod)。请注意,这些函数所接受的格式取决于当前的语言环境。
// stof example
#include <iostream> // std::cout
#include <string> // std::string, std::stofint main ()
{std::string orbits ("686.97 365.24");std::string::size_type sz; // alias of size_tfloat mars = std::stof (orbits,&sz);float earth = std::stof (orbits.substr(sz));std::cout << "One martian year takes " << (mars/earth) << " Earth years.\n";return 0;
}
Possible output:
One martian year takes 1.88087 Earth years. |
stod
double stod (const string& str, size_t* idx = 0);double stod (const wstring& str, size_t* idx = 0);
Convert string to double
分析str,将其内容解释为浮点数,该浮点数作为double类型的值返回。 如果idx不是空指针,函数还会将idx的值设置为str中数字后面第一个字符的位置。 该函数使用strtod(或wcstod)来执行转换(有关该过程的更多详细信息,请参阅strtod)。请注意,这些函数所接受的格式取决于当前的语言环境。
// stod example
#include <iostream> // std::cout
#include <string> // std::string, std::stodint main ()
{std::string orbits ("365.24 29.53");std::string::size_type sz; // alias of size_tdouble earth = std::stod (orbits,&sz);double moon = std::stod (orbits.substr(sz));std::cout << "The moon completes " << (earth/moon) << " orbits per Earth year.\n";return 0;
}
Possible output:
The moon completes 12.3684 orbits per Earth year. |
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
分析str,将其内容解释为浮点数,该浮点数作为长双精度类型的值返回。 如果idx不是空指针,函数还会将idx的值设置为str中数字后面第一个字符的位置。 该函数使用strtell(或wcstold)来执行转换(有关该过程的更多详细信息,请参阅strtod)。
// stold example
#include <iostream> // std::cout
#include <string> // std::string, std::stodint main ()
{std::string orbits ("90613.305 365.24");std::string::size_type sz; // alias of size_tlong double pluto = std::stod (orbits,&sz);long double earth = std::stod (orbits.substr(sz));std::cout << "Pluto takes " << (pluto/earth) << " years to complete an orbit.\n";return 0;
}
Possible output:
Pluto takes 248.093 years to complete an orbit. |
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);
Convert numerical value to string
返回一个以val表示的字符串。
// to_string example
#include <iostream> // std::cout
#include <string> // std::string, std::to_stringint main ()
{std::string pi = "pi is " + std::to_string(3.1415926);std::string perfect = std::to_string(1+2+4+7+14) + " is a perfect number";std::cout << pi << '\n';std::cout << perfect << '\n';return 0;
}
Possible output:
pi is 3.141593 28 is a perfect number |
to_wstring
wstring to_wstring (int val);wstring to_wstring (long val);wstring to_wstring (long long val);wstring to_wstring (unsigned val);wstring to_wstring (unsigned long val);wstring to_wstring (unsigned long long val);wstring to_wstring (float val);wstring to_wstring (double val);wstring to_wstring (long double val);
Convert numerical value to wide string
返回一个表示为val的wstring。
// to_wstring example
#include <iostream> // std::wcout
#include <string> // std::wstring, std::to_wstringint main ()
{std::wstring pi = L"pi is " + std::to_wstring(3.1415926);std::wstring perfect = std::to_wstring(1+2+4+7+14) + L" is a perfect number";std::wcout << pi << L'\n';std::wcout << perfect << L'\n';return 0;
}
Possible output:
pi is 3.141593 28 is a perfect number |