最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2025年04月21日 10:44:27   作者:極客晨風(fēng)  
std::stoul是 C++ 標(biāo)準(zhǔn)庫中的一個字符串轉(zhuǎn)換函數(shù),它用于將?std::string?或?std::wstring?轉(zhuǎn)換為?unsigned long?類型的整數(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&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 語言中的標(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)換為 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)換無符號整數(shù),如 unsigned long,如果數(shù)值更大,可以使用 std::stoull。

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

相關(guān)文章

  • Qt QChart實(shí)現(xiàn)折線圖的繪制

    Qt QChart實(shí)現(xiàn)折線圖的繪制

    QChart是常用的圖表,這篇文章主要為大家詳細(xì)介紹了Qt如何利用QChart實(shí)現(xiàn)折線圖的繪制,文中的示例代碼講解詳細(xì),感興趣的可以了解一下
    2023-04-04
  • C++中單調(diào)棧的基本性質(zhì)介紹

    C++中單調(diào)棧的基本性質(zhì)介紹

    這篇文章主要介紹了單調(diào)棧的基本性質(zhì)介紹,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • C++第三方日志庫Glog基本語法詳解

    C++第三方日志庫Glog基本語法詳解

    這篇文章主要介紹了C++第三方日志庫Glog基本語法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-02-02
  • C語言字符串函數(shù)與內(nèi)存函數(shù)精講

    C語言字符串函數(shù)與內(nèi)存函數(shù)精講

    這篇文章主要介紹一些c語言中常用字符串函數(shù)和內(nèi)存函數(shù)的使用,并且為了幫助讀者理解和使用,也都模擬實(shí)現(xiàn)了他們的代碼,需要的朋友可以參考一下
    2022-04-04
  • VS2019+MPI配置過程的實(shí)現(xiàn)步驟

    VS2019+MPI配置過程的實(shí)現(xiàn)步驟

    本文介紹了在VS2019上配置MPI,包括下載和安裝MPI、創(chuàng)建項(xiàng)目、配置屬性、導(dǎo)入頭文件和庫文件、添加依賴項(xiàng)等步驟,具有一定的參考價值,感興趣的可以了解一下
    2024-12-12
  • C++ std::async的使用總結(jié)

    C++ std::async的使用總結(jié)

    這篇文章主要介紹了C++ std::async的使用總結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • c語言隨機(jī)數(shù)函數(shù)示例

    c語言隨機(jī)數(shù)函數(shù)示例

    這篇文章主要介紹了c語言隨機(jī)數(shù)函數(shù)示例,需要的朋友可以參考下
    2014-04-04
  • ffmpeg?在?win平臺下的編譯以及集成

    ffmpeg?在?win平臺下的編譯以及集成

    這篇文章主要為大家介紹了ffmpeg?在?win平臺下的編譯以及集成詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪
    2023-05-05
  • C/C++通過IP獲取局域網(wǎng)網(wǎng)卡MAC地址

    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
  • C語言字符串大小比較

    C語言字符串大小比較

    本文給大家分享給大家的是C語言的字符串大小比較的函數(shù),有需要的小伙伴可以參考下。
    2015-07-07

最新評論

黄大仙区| 武宁县| 宜君县| 香格里拉县| 恩平市| 舞钢市| 日喀则市| 岱山县| 济宁市| 渑池县| 治多县| 沽源县| 西平县| 博客| 天津市| 大厂| 融水| 皮山县| 洛扎县| 六枝特区| 蒙自县| 长沙县| 米易县| 铜鼓县| 佳木斯市| 寻甸| 翼城县| 易门县| 乐平市| 临西县| 芷江| 樟树市| 玛纳斯县| 保靖县| 彩票| 兖州市| 准格尔旗| 凯里市| 绥芬河市| 汝南县| 石泉县|