欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

深入解析 C++中std::stoul 函數(shù)

 更新時(shí)間:2025年04月21日 10:44:27   作者:極客晨風(fēng)  
std::stoul是 C++ 標(biāo)準(zhǔn)庫(kù)中的一個(gè)字符串轉(zhuǎn)換函數(shù),它用于將?std::string?或?std::wstring?轉(zhuǎn)換為?unsigned long?類型的整數(shù),下面就來(lái)介紹一下

std::stoul(String to Unsigned Long)是 C++ 標(biāo)準(zhǔn)庫(kù)中的一個(gè)字符串轉(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)換的字符串,必須是有效的無(wú)符號(hào)整數(shù)格式。
  • pos(可選):如果提供 pos,stoul 會(huì)在 *pos 中存儲(chǔ)轉(zhuǎn)換結(jié)束的位置(即最后一個(gè)轉(zhuǎn)換的字符的下一個(gè)位置)。
  • base(可選):表示字符串所使用的進(jìn)制,默認(rèn)值為 10(十進(jìn)制),支持 2 到 36 進(jìn)制。

1.3 返回值

  • 成功:返回轉(zhuǎn)換后的 unsigned long 類型整數(shù)。
  • 失敗:如果字符串不是有效的數(shù)字或超出了 unsigned long 類型的范圍,會(huì)拋出異常。

2. std::stoul 的實(shí)現(xiàn)原理

在 GCC 標(biāo)準(zhǔn)庫(kù)的 <string> 頭文件中,std::stoul 的底層實(shí)現(xiàn)基于 std::strtoul,如下:

inline unsigned long
stoul(const string&amp; __str, size_t* __idx = 0, int __base = 10)
{
    return __gnu_cxx::__stoa(&amp;std::strtoul, "stoul", __str.c_str(),
                             __idx, __base);
}

2.1 解析實(shí)現(xiàn)代碼

  • 調(diào)用 std::strtoulstd::strtoul(String to Unsigned Long)是 C 語(yǔ)言中的標(biāo)準(zhǔn)庫(kù)函數(shù),它可以將 C 風(fēng)格的字符串轉(zhuǎn)換為 unsigned long 類型的整數(shù),同時(shí)支持進(jìn)制轉(zhuǎn)換。
  • 使用 __gnu_cxx::__stoa 進(jìn)行封裝__stoa 是 GNU C++ 擴(kuò)展庫(kù)中的一個(gè)工具函數(shù),它用于將 unsigned long 轉(zhuǎn)換為 unsigned long,并進(jìn)行錯(cuò)誤檢查。
  • __str.c_str()std::string 的 .c_str() 方法返回一個(gè) 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 會(huì)拋出 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 << "錯(cuò)誤: 無(wú)效的數(shù)字字符串 -> " << e.what() << std::endl;
    }

    return 0;
}

輸出:

錯(cuò)誤: 無(wú)效的數(shù)字字符串 -> stoul

4.2 數(shù)值超出 unsigned long 范圍

如果字符串表示的數(shù)值超出 unsigned long 的范圍,std::stoul 會(huì)拋出 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 << "錯(cuò)誤: 數(shù)值超出范圍 -> " << e.what() << std::endl;
    }

    return 0;
}

輸出:

錯(cuò)誤: 數(shù)值超出范圍 -> stoul

5. std::stoul 相關(guān)函數(shù)

函數(shù)作用返回類型
std::stoi轉(zhuǎn)換為 intint
std::stol轉(zhuǎn)換為 longlong
std::stoll轉(zhuǎn)換為 long longlong long
std::stoul轉(zhuǎn)換為 unsigned longunsigned long
std::stoull轉(zhuǎn)換為 unsigned long longunsigned 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)換無(wú)符號(hào)整數(shù),如 unsigned long,如果數(shù)值更大,可以使用 std::stoull。

到此這篇關(guān)于深入解析 C++中std::stoul 函數(shù)的文章就介紹到這了,更多相關(guān)C++ std::stoul 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

最新評(píng)論