C++字符串转换(stoi;stol;stoul;stoll;stoull;stof;stod;stold)
一、stoi
頭文件string
int stoi (const string& str, size_t* idx = 0, int base = 10); int stoi (const wstring& str, size_t* idx = 0, int base = 10);
將字符串轉(zhuǎn)換為整數(shù)
解析str將其內(nèi)容解釋為指定基數(shù)的整數(shù),并以int值的形式返回。
如果idx不是空指針,則該函數(shù)還將idx的值設(shè)置為數(shù)字后str中第一個(gè)字符的位置。
該函數(shù)使用strtol(或wcstol)來執(zhí)行轉(zhuǎn)換(有關(guān)該過程的更多詳細(xì)信息,請(qǐng)參閱strtol)。
參數(shù)
str
字符串對(duì)象,表示整數(shù)。
idx
指向size_t類型對(duì)象的指針,其值由函數(shù)設(shè)置為數(shù)值后str中下一個(gè)字符的位置。
此參數(shù)也可以是空指針,在這種情況下不使用它。
base
確定有效字符及其解釋的數(shù)字基數(shù)(基數(shù))。
如果為0,則使用的基數(shù)由序列中的格式確定(有關(guān)詳細(xì)信息,請(qǐng)參閱strtol)。 請(qǐng)注意,默認(rèn)情況下,此參數(shù)為10,而不是0。
返回值
成功時(shí),該函數(shù)將轉(zhuǎn)換后的整數(shù)作為int值返回。
// stoi example
#include <iostream> // std::cout
#include <string> // std::string, std::stoi
int 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_t
int 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) << "]
";
std::cout << str_hex << ": " << i_hex << '
';
std::cout << str_bin << ": " << i_bin << '
';
std::cout << str_auto << ": " << i_auto << '
';
return 0;
}
復(fù)雜度
未指定,但通常在解釋的字符數(shù)中是線性的。
數(shù)據(jù)范圍
修改idx指向的值(如果不為零)。
異常
如果無法執(zhí)行轉(zhuǎn)換,則拋出invalid_argument異常。
如果讀取的值超出int的可表示值范圍,則拋出out_of_range異常。
無效的idx會(huì)導(dǎo)致未定義的行為。
二、stol
頭文件string
long stol (const string& str, size_t* idx = 0, int base = 10); long stol (const wstring& str, size_t* idx = 0, int base = 10);
將字符串轉(zhuǎn)換為long int
解析str將其內(nèi)容解釋為指定基數(shù)的整數(shù),并以long int類型的值返回。
如果idx不是空指針,則該函數(shù)還將idx的值設(shè)置為數(shù)字后str中第一個(gè)字符的位置。
該函數(shù)使用strtol(或wcstol)來執(zhí)行轉(zhuǎn)換(有關(guān)該過程的更多詳細(xì)信息,請(qǐng)參閱strtol)。
參數(shù)
str
字符串對(duì)象,表示整數(shù)。
idx
指向size_t類型對(duì)象的指針,其值由函數(shù)設(shè)置為數(shù)值后str中下一個(gè)字符的位置。
此參數(shù)也可以是空指針,在這種情況下不使用它。
base
確定有效字符及其解釋的數(shù)字基數(shù)(基數(shù))。
如果為0,則使用的基數(shù)由序列中的格式確定(有關(guān)詳細(xì)信息,請(qǐng)參閱strtol)。 請(qǐng)注意,默認(rèn)情況下,此參數(shù)為10,而不是0。
返回值
成功時(shí),該函數(shù)將轉(zhuǎn)換后的整數(shù)作為long int類型的值返回。
// stol example
#include <iostream> // std::cout
#include <string> // std::string, std::stol
int 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_t
long 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 << '
';
std::cout << str_hex << ": " << li_hex << '
';
std::cout << str_bin << ": " << li_bin << '
';
std::cout << str_auto << ": " << li_auto << '
';
return 0;
}
復(fù)雜度
未指定,但通常在解釋的字符數(shù)中是線性的。
數(shù)據(jù)范圍
修改idx指向的值(如果不為零)。
異常
如果無法執(zhí)行轉(zhuǎn)換,則拋出invalid_argument異常。
如果讀取的值超出long int的可表示值范圍,則拋出invalid_argument或out_of_range異常。
無效的idx會(huì)導(dǎo)致未定義的行為。
三、stoul
頭文件string
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);
將字符串轉(zhuǎn)換為無符號(hào)整數(shù)
解析str將其內(nèi)容解釋為指定基數(shù)的整數(shù),該基數(shù)作為無符號(hào)長(zhǎng)整數(shù)值返回。
如果idx不是空指針,則該函數(shù)還將idx的值設(shè)置為數(shù)字后str中第一個(gè)字符的位置。
該函數(shù)使用strtoul(或wcstoul)執(zhí)行轉(zhuǎn)換(有關(guān)該過程的更多詳細(xì)信息,請(qǐng)參閱strtol)。
參數(shù)
str
字符串對(duì)象,表示整數(shù)。
inx
指向size_t類型對(duì)象的指針,其值由函數(shù)設(shè)置為數(shù)值后str中下一個(gè)字符的位置。
此參數(shù)也可以是空指針,在這種情況下不使用它。
base
確定有效字符及其解釋的數(shù)字基數(shù)(基數(shù))。
如果為0,則使用的基數(shù)由序列中的格式確定(有關(guān)詳細(xì)信息,請(qǐng)參閱strtol)。 請(qǐng)注意,默認(rèn)情況下,此參數(shù)為10,而不是0。
返回值
成功時(shí),該函數(shù)將轉(zhuǎn)換后的整數(shù)作為無符號(hào)長(zhǎng)整數(shù)值返回。
// stoul example
#include <iostream> // std::cin, std::cout
#include <string> // std::string, std::stoul, std::getline
int 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 << '
';
return 0;
}
復(fù)雜度
未指定,但通常在解釋的字符數(shù)中是線性的。
數(shù)據(jù)范圍
修改idx指向的值(如果不為零)。
異常
如果無法執(zhí)行轉(zhuǎn)換,則拋出invalid_argument異常。
如果讀取的值超出無符號(hào)長(zhǎng)度的可表示值范圍,則拋出out_of_range異常。
無效的idx會(huì)導(dǎo)致未定義的行為。
四、stoll
頭文件string
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);
將字符串轉(zhuǎn)換為long long
解析str將其內(nèi)容解釋為指定基數(shù)的整數(shù),并將其作為long long類型的值返回。
如果idx不是空指針,則該函數(shù)還將idx的值設(shè)置為數(shù)字后str中第一個(gè)字符的位置。
該函數(shù)使用strtoll(或wcstoll)來執(zhí)行轉(zhuǎn)換(有關(guān)該過程的更多詳細(xì)信息,請(qǐng)參閱strtol)。
參數(shù)
str
字符串對(duì)象,表示整數(shù)。
idx
指向size_t類型對(duì)象的指針,其值由函數(shù)設(shè)置為數(shù)值后str中下一個(gè)字符的位置。
此參數(shù)也可以是空指針,在這種情況下不使用它。
base
確定有效字符及其解釋的數(shù)字基數(shù)(基數(shù))。
如果為0,則使用的基數(shù)由序列中的格式確定(有關(guān)詳細(xì)信息,請(qǐng)參閱strtol)。 請(qǐng)注意,默認(rèn)情況下,此參數(shù)為10,而不是0。
返回值
成功時(shí),該函數(shù)返回轉(zhuǎn)換后的整數(shù)作為long long類型的值。
// stoll example
#include <iostream> // std::cout
#include <string> // std::string, std::stoll
int main ()
{
std::string str = "8246821 0xffff 020";
std::string::size_type sz = 0; // alias of size_t
while (!str.empty()) {
long long ll = std::stoll (str,&sz,0);
std::cout << str.substr(0,sz) << " interpreted as " << ll << '
';
str = str.substr(sz);
}
return 0;
}
復(fù)雜度
未指定,但通常在解釋的字符數(shù)中是線性的。
數(shù)據(jù)范圍
修改idx指向的值(如果不為零)。
異常
如果無法執(zhí)行轉(zhuǎn)換,則拋出invalid_argument異常。
如果讀取的值超出可表示值的范圍超長(zhǎng),則拋出out_of_range異常。
無效的idx會(huì)導(dǎo)致未定義的行為。
五、stoull
頭文件string
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);
將字符串轉(zhuǎn)換為unsigned long long
解析str將其內(nèi)容解釋為指定基數(shù)的整數(shù),該值作為unsigned long long類型的值返回。
如果idx不是空指針,則該函數(shù)還將idx的值設(shè)置為數(shù)字后str中第一個(gè)字符的位置。
該函數(shù)使用strtoull(或wcstoull)執(zhí)行轉(zhuǎn)換(有關(guān)該過程的更多詳細(xì)信息,請(qǐng)參閱strtol)。
參數(shù)
str
字符串對(duì)象,表示整數(shù)。
idx
指向size_t類型對(duì)象的指針,其值由函數(shù)設(shè)置為數(shù)值后str中下一個(gè)字符的位置。
此參數(shù)也可以是空指針,在這種情況下不使用它。
base
確定有效字符及其解釋的數(shù)字基數(shù)(基數(shù))。
如果為0,則使用的基數(shù)由序列中的格式確定(有關(guān)詳細(xì)信息,請(qǐng)參閱strtol)。 請(qǐng)注意,默認(rèn)情況下,此參數(shù)為10,而不是0。
返回值
成功時(shí),該函數(shù)將轉(zhuǎn)換后的整數(shù)作為unsigned long long類型的值返回。
// stoull example
#include <iostream> // std::cout
#include <string> // std::string, std::stoull
int main ()
{
std::string str = "8246821 0xffff 020 -1";
std::string::size_type sz = 0; // alias of size_t
while (!str.empty()) {
unsigned long long ull = std::stoull (str,&sz,0);
std::cout << str.substr(0,sz) << " interpreted as " << ull << '
';
str = str.substr(sz);
}
return 0;
}
復(fù)雜度
未指定,但通常在解釋的字符數(shù)中是線性的。
數(shù)據(jù)范圍
修改idx指向的值(如果不為零)。
異常
如果無法執(zhí)行轉(zhuǎn)換,則拋出invalid_argument異常。
如果讀取的值超出無符號(hào)long long的可表示值范圍,則拋出out_of_range異常。
無效的idx會(huì)導(dǎo)致未定義的行為。
六、stof
頭文件string
float stof (const string& str, size_t* idx = 0); float stof (const wstring& str, size_t* idx = 0);
將字符串轉(zhuǎn)換為float
解析str將其內(nèi)容解釋為浮點(diǎn)數(shù),該值作為float類型返回。
如果idx不是空指針,則該函數(shù)還將idx的值設(shè)置為數(shù)字后str中第一個(gè)字符的位置。
該函數(shù)使用strtod(或wcstod)來執(zhí)行轉(zhuǎn)換(有關(guān)該過程的更多詳細(xì)信息,請(qǐng)參閱strtod)。 請(qǐng)注意,這些函數(shù)接受的格式取決于當(dāng)前的語言環(huán)境。
參數(shù)
str
具有浮點(diǎn)數(shù)表示的String對(duì)象。
idx
指向size_t類型對(duì)象的指針,其值由函數(shù)設(shè)置為數(shù)值后str中下一個(gè)字符的位置。
此參數(shù)也可以是空指針,在這種情況下不使用它。
返回值
成功時(shí),該函數(shù)將轉(zhuǎn)換后的浮點(diǎn)數(shù)作為float類型的值返回。
// stof example
#include <iostream> // std::cout
#include <string> // std::string, std::stof
int main ()
{
std::string orbits ("686.97 365.24");
std::string::size_type sz; // alias of size_t
float mars = std::stof (orbits,&sz);
float earth = std::stof (orbits.substr(sz));
std::cout << "One martian year takes " << (mars/earth) << " Earth years.
";
return 0;
}
復(fù)雜度
未指定,但通常在解釋的字符數(shù)中是線性的。
數(shù)據(jù)范圍
修改idx指向的值(如果不為零)。
異常
如果無法執(zhí)行轉(zhuǎn)換,則拋出invalid_argument異常。
如果讀取的值超出float的可表示值范圍(在某些庫實(shí)現(xiàn)中,這包括下溢),則拋出out_of_range異常。
無效的idx會(huì)導(dǎo)致未定義的行為。
七、stod
頭文件string
double stod (const string& str, size_t* idx = 0); double stod (const wstring& str, size_t* idx = 0);
將字符串轉(zhuǎn)換為double
解析str將其內(nèi)容解釋為浮點(diǎn)數(shù),該值返回為double類型的值。
如果idx不是空指針,則該函數(shù)還將idx的值設(shè)置為數(shù)字后str中第一個(gè)字符的位置。
該函數(shù)使用strtod(或wcstod)來執(zhí)行轉(zhuǎn)換(有關(guān)該過程的更多詳細(xì)信息,請(qǐng)參閱strtod)。 請(qǐng)注意,這些函數(shù)接受的格式取決于當(dāng)前的語言環(huán)境。
參數(shù)
str
具有浮點(diǎn)數(shù)表示的String對(duì)象。
idx
指向size_t類型對(duì)象的指針,其值由函數(shù)設(shè)置為數(shù)值后str中下一個(gè)字符的位置。
此參數(shù)也可以是空指針,在這種情況下不使用它。
返回值
成功時(shí),該函數(shù)將轉(zhuǎn)換后的浮點(diǎn)數(shù)作為double類型的值返回。
// stod example
#include <iostream> // std::cout
#include <string> // std::string, std::stod
int main ()
{
std::string orbits ("365.24 29.53");
std::string::size_type sz; // alias of size_t
double earth = std::stod (orbits,&sz);
double moon = std::stod (orbits.substr(sz));
std::cout << "The moon completes " << (earth/moon) << " orbits per Earth year.
";
return 0;
}
復(fù)雜度
未指定,但通常在解釋的字符數(shù)中是線性的。
數(shù)據(jù)范圍
修改idx指向的值(如果不為零)。
異常
如果無法執(zhí)行轉(zhuǎn)換,則拋出invalid_argument異常。
如果讀取的值超出double的可表示值范圍(在某些庫實(shí)現(xiàn)中,這包括下溢),則拋出out_of_range異常。
無效的idx會(huì)導(dǎo)致未定義的行為。
八、stold
頭文件string
long double stold (const string& str, size_t* idx = 0); long double stold (const wstring& str, size_t* idx = 0);
將字符串轉(zhuǎn)換為long double
解析str將其內(nèi)容解釋為浮點(diǎn)數(shù),該值作為long double類型返回。
如果idx不是空指針,則該函數(shù)還將idx的值設(shè)置為數(shù)字后str中第一個(gè)字符的位置。
該函數(shù)使用strtold(或wcstold)來執(zhí)行轉(zhuǎn)換(有關(guān)該過程的更多詳細(xì)信息,請(qǐng)參閱strtod)。
參數(shù)
str
具有浮點(diǎn)數(shù)表示的String對(duì)象。
idx
指向size_t類型對(duì)象的指針,其值由函數(shù)設(shè)置為數(shù)值后str中下一個(gè)字符的位置。
此參數(shù)也可以是空指針,在這種情況下不使用它。
返回值
成功時(shí),該函數(shù)將轉(zhuǎn)換后的浮點(diǎn)數(shù)作為long double類型的值返回。
// stold example
#include <iostream> // std::cout
#include <string> // std::string, std::stod
int main ()
{
std::string orbits ("90613.305 365.24");
std::string::size_type sz; // alias of size_t
long 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.
";
return 0;
}
復(fù)雜度
未指定,但通常在解釋的字符數(shù)中是線性的。
數(shù)據(jù)范圍
修改idx指向的值(如果不為零)。
異常
如果無法執(zhí)行轉(zhuǎn)換,則拋出invalid_argument異常。
如果讀取的值超出了可表示值的范圍(在某些庫實(shí)現(xiàn)中,這包括下溢),則拋出out_of_range異常。
無效的idx會(huì)導(dǎo)致未定義的行為。
本文來自博客園,作者:Mr-xxx,轉(zhuǎn)載請(qǐng)注明原文鏈接:https://www.cnblogs.com/MrLiuZF/p/13591618.html
總結(jié)
以上是生活随笔為你收集整理的C++字符串转换(stoi;stol;stoul;stoll;stoull;stof;stod;stold)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: StAX XMLInputFactory
- 下一篇: Ubuntu 安装 Tomcat 7.