深入解析 C++中std::stoul 函數(shù)
std::stoul(String to Unsigned Long)是 C++ 標(biāo)準(zhǔn)庫中的一個字符串轉(zhuǎn)換函數(shù),它用于將 std::string 或 std::wstring 轉(zhuǎn)換為 unsigned long 類型的整數(shù),并支持不同進(jìn)制的轉(zhuǎn)換(如二進(jìn)制、十進(jìn)制、十六進(jìn)制等)。
1. std::stoul 的基本介紹
std::stoul 定義在 <string> 頭文件中,并位于 std 命名空間下。它的主要功能是將字符串轉(zhuǎn)換為 unsigned long 類型的整數(shù),并且支持不同進(jìn)制的轉(zhuǎn)換
1.1 std::stoul 的函數(shù)原型
unsigned long stoul(const std::string& str, size_t* pos = nullptr, int base = 10); unsigned long stoul(const std::wstring& str, size_t* pos = nullptr, int base = 10);
1.2 參數(shù)解析
str:要轉(zhuǎn)換的字符串,必須是有效的無符號整數(shù)格式。pos(可選):如果提供pos,stoul會在*pos中存儲轉(zhuǎn)換結(jié)束的位置(即最后一個轉(zhuǎn)換的字符的下一個位置)。base(可選):表示字符串所使用的進(jìn)制,默認(rèn)值為10(十進(jìn)制),支持2到36進(jìn)制。
1.3 返回值
- 成功:返回轉(zhuǎn)換后的
unsigned long類型整數(shù)。 - 失敗:如果字符串不是有效的數(shù)字或超出了
unsigned long類型的范圍,會拋出異常。
2. std::stoul 的實(shí)現(xiàn)原理
在 GCC 標(biāo)準(zhǔn)庫的 <string> 頭文件中,std::stoul 的底層實(shí)現(xiàn)基于 std::strtoul,如下:
inline unsigned long
stoul(const string& __str, size_t* __idx = 0, int __base = 10)
{
return __gnu_cxx::__stoa(&std::strtoul, "stoul", __str.c_str(),
__idx, __base);
}
2.1 解析實(shí)現(xiàn)代碼
- 調(diào)用
std::strtoulstd::strtoul(String to Unsigned Long)是 C 語言中的標(biāo)準(zhǔn)庫函數(shù),它可以將 C 風(fēng)格的字符串轉(zhuǎn)換為unsigned long類型的整數(shù),同時支持進(jìn)制轉(zhuǎn)換。 - 使用
__gnu_cxx::__stoa進(jìn)行封裝__stoa是 GNU C++ 擴(kuò)展庫中的一個工具函數(shù),它用于將unsigned long轉(zhuǎn)換為unsigned long,并進(jìn)行錯誤檢查。 __str.c_str()std::string的.c_str()方法返回一個const char*,將std::string轉(zhuǎn)換為 C 風(fēng)格字符串,以便std::strtoul解析。
3. std::stoul 的實(shí)際用法
3.1 基礎(chǔ)示例
#include <iostream>
#include <string>
int main() {
std::string str = "123456789";
unsigned long num = std::stoul(str); // 默認(rèn)10進(jìn)制
std::cout << "轉(zhuǎn)換結(jié)果: " << num << std::endl;
return 0;
}
輸出:
轉(zhuǎn)換結(jié)果: 123456789
3.2 使用 pos 參數(shù)記錄轉(zhuǎn)換結(jié)束的位置
#include <iostream>
#include <string>
int main() {
std::string str = "12345xyz";
std::size_t pos;
unsigned long num = std::stoul(str, &pos);
std::cout << "轉(zhuǎn)換結(jié)果: " << num << std::endl;
std::cout << "轉(zhuǎn)換結(jié)束位置: " << pos << std::endl;
return 0;
}
輸出:
轉(zhuǎn)換結(jié)果: 12345
轉(zhuǎn)換結(jié)束位置: 5
解析:
stoul解析"12345"后遇到"xyz",停止轉(zhuǎn)換,并將pos設(shè)為5。
3.3 處理不同進(jìn)制的轉(zhuǎn)換
#include <iostream>
#include <string>
int main() {
std::string str = "1A"; // 16進(jìn)制
unsigned long num = std::stoul(str, nullptr, 16); // 以16進(jìn)制解析
std::cout << "轉(zhuǎn)換結(jié)果: " << num << std::endl;
return 0;
}
輸出:
轉(zhuǎn)換結(jié)果: 26
解析:
1A在十六進(jìn)制中等于26,所以stoul返回26。
其他進(jìn)制示例
std::stoul("1010", nullptr, 2); // 以二進(jìn)制解析,返回 10
std::stoul("75", nullptr, 8); // 以八進(jìn)制解析,返回 61
std::stoul("1F", nullptr, 16); // 以十六進(jìn)制解析,返回 31
4. 處理異常情況
4.1 輸入不是有效的數(shù)字
如果 str 不是有效的整數(shù)格式,std::stoul 會拋出 std::invalid_argument 異常:
#include <iostream>
#include <string>
#include <stdexcept>
int main() {
try {
std::string str = "abc";
unsigned long num = std::stoul(str);
std::cout << "轉(zhuǎn)換結(jié)果: " << num << std::endl;
} catch (const std::invalid_argument& e) {
std::cerr << "錯誤: 無效的數(shù)字字符串 -> " << e.what() << std::endl;
}
return 0;
}
輸出:
錯誤: 無效的數(shù)字字符串 -> stoul
4.2 數(shù)值超出 unsigned long 范圍
如果字符串表示的數(shù)值超出 unsigned long 的范圍,std::stoul 會拋出 std::out_of_range 異常:
#include <iostream>
#include <string>
#include <climits>
int main() {
try {
std::string str = "99999999999999999999";
unsigned long num = std::stoul(str);
std::cout << "轉(zhuǎn)換結(jié)果: " << num << std::endl;
} catch (const std::out_of_range& e) {
std::cerr << "錯誤: 數(shù)值超出范圍 -> " << e.what() << std::endl;
}
return 0;
}
輸出:
錯誤: 數(shù)值超出范圍 -> stoul
5. std::stoul 相關(guān)函數(shù)
| 函數(shù) | 作用 | 返回類型 |
|---|---|---|
std::stoi | 轉(zhuǎn)換為 int | int |
std::stol | 轉(zhuǎn)換為 long | long |
std::stoll | 轉(zhuǎn)換為 long long | long long |
std::stoul | 轉(zhuǎn)換為 unsigned long | unsigned long |
std::stoull | 轉(zhuǎn)換為 unsigned long long | unsigned long long |
6. 結(jié)論
std::stoul是std::strtoul的封裝,支持進(jìn)制轉(zhuǎn)換。- 提供
pos參數(shù)記錄轉(zhuǎn)換位置。 - 可能拋出
std::invalid_argument和std::out_of_range異常。 - 適用于轉(zhuǎn)換無符號整數(shù),如
unsigned long,如果數(shù)值更大,可以使用std::stoull。
到此這篇關(guān)于深入解析 C++中std::stoul 函數(shù)的文章就介紹到這了,更多相關(guān)C++ std::stoul 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言字符串函數(shù)與內(nèi)存函數(shù)精講
這篇文章主要介紹一些c語言中常用字符串函數(shù)和內(nèi)存函數(shù)的使用,并且為了幫助讀者理解和使用,也都模擬實(shí)現(xiàn)了他們的代碼,需要的朋友可以參考一下2022-04-04
VS2019+MPI配置過程的實(shí)現(xiàn)步驟
本文介紹了在VS2019上配置MPI,包括下載和安裝MPI、創(chuàng)建項(xiàng)目、配置屬性、導(dǎo)入頭文件和庫文件、添加依賴項(xiàng)等步驟,具有一定的參考價值,感興趣的可以了解一下2024-12-12
C/C++通過IP獲取局域網(wǎng)網(wǎng)卡MAC地址
這篇文章主要為大家詳細(xì)介紹了C++如何通過Win32API函數(shù)SendARP從IP地址獲取局域網(wǎng)內(nèi)網(wǎng)卡的MAC地址,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-02-02

