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

詳解C++中stoi/stol/stoll函數(shù)的用法

 更新時(shí)間:2023年03月23日 09:13:56   作者:微塵8  
這篇文章主要為大家詳細(xì)介紹了C++中stoi、stol、stoll函數(shù)的具體用法,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)校C++有一點(diǎn)的幫助,需要的可以參考一下

stoi()函數(shù)

#include <string>
int stoi(const std::string& str, std::size_t* pos = 0, int base = 10);
int stoi(const std::wstring& str, std::size_t* pos = 0, int base = 10);

功能:將字符串str轉(zhuǎn)成 有符號(hào) int 整數(shù)

參數(shù):

  • str:字符串
  • pos:存儲(chǔ)將字符串str轉(zhuǎn)成有符號(hào)整數(shù),處理了str中字符的個(gè)數(shù)的地址,默認(rèn)為NULL
  • base:進(jìn)制,10:十進(jìn)制,8:八進(jìn)制,16:十六進(jìn)制,0:則自動(dòng)檢測(cè)數(shù)值進(jìn)制,str是 0 開頭為八進(jìn)制,str是 0x 或 0X 開頭是十六進(jìn)制,默認(rèn)為十進(jìn)制

stoi()函數(shù)指定轉(zhuǎn)換字符串為十進(jìn)制用法

#include <iostream>
#include <string>
using namespace std;
 
int main(int argc, char *argv[])
{
    int a;
    size_t pos = 0;
    string str;
 
    str = "-1235";
    a = stoi(str);
    cout << "a = " << a << endl; //a = -1235
 
    str = "1235";
    a = stoi(str);
    cout << "a = " << a << endl; //a = 1235
 
    str = "  -12  35"; 
    a = stoi(str, &pos); //會(huì)舍棄空白符
    cout << "a = " << a << endl; //a = -12
    cout << "pos = " << pos << endl; //pos = 5
 
    str = "  -12ab35";
    a = stoi(str, &pos);
    cout << "a = " << a << endl; //a = -12
    cout << "pos = " << pos << endl; //pos = 5
 
    str = "0123";
    a = stoi(str);
    cout << "a = " << a << endl; //a = 123
 
    str = "0x123";
    a = stoi(str);
    cout << "a = " << a << endl; //a = 0
 
    return 0;
}

stoi()函數(shù)指定轉(zhuǎn)換字符串為十六進(jìn)制用法

#include <iostream>
#include <string>
using namespace std;
 
int main(int argc, char *argv[])
{
    int a;
    size_t pos = 0;
    string str;
 
    str = "0x123";
    a = stoi(str, NULL, 16); //base = 16,指定十六進(jìn)制
    cout << "a = " << a << endl; //a = 291
 
   str = "0x123";
    a = stoi(str, NULL, 0); //base = 0,自動(dòng)檢測(cè)數(shù)值進(jìn)制
    cout << "a = " << a << endl; //a = 291
 
    str = "-12";
    a = stoi(str, &pos, 16); //-(2 + 1*16)
    cout << "a = " << a << endl; //a = -18
    cout << "pos = " << pos << endl; //pos = 3
 
    str = "12";
    a = stoi(str, &pos, 16); //2 + 1*16
    cout << "a = " << a << endl; //a = 18
    cout << "pos = " << pos << endl; //pos = 2
 
    str = "  -12  35"; 
    a = stoi(str, &pos, 16); //會(huì)舍棄空白符
    cout << "a = " << a << endl; //a = -18
    cout << "pos = " << pos << endl; //pos = 5
 
    str = "  -ab";
    a = stoi(str, &pos, 16); //-(11 + 10*16)
    cout << "a = " << a << endl; //a = -171
    cout << "pos = " << pos << endl; //pos = 5
 
    str = "0123";
    a = stoi(str, NULL, 16); //(3 + 2*16 + 1*16*16)
    cout << "a = " << a << endl; //a = 291
 
    return 0;
}

stoi()函數(shù)指定轉(zhuǎn)換字符串為八進(jìn)制用法

#include <iostream>
#include <string>
using namespace std;
 
int main(int argc, char *argv[])
{
    int a;
    size_t pos = 0;
    string str;
 
    str = "0x123";
    a = stoi(str, NULL, 8); //base = 8,指定八進(jìn)制
    cout << "a = " << a << endl; //a = 0
 
   str = "0123"; //(3 + 2*8 + 1*8*8)
    a = stoi(str, NULL, 0); //base = 0,自動(dòng)檢測(cè)數(shù)值進(jìn)制
    cout << "a = " << a << endl; //a = 83
 
    str = "-12";
    a = stoi(str, &pos, 8); //-(2 + 1*8)
    cout << "a = " << a << endl; //a = -10
    cout << "pos = " << pos << endl; //pos = 3
 
    str = "12";
    a = stoi(str, &pos, 8); //2 + 1*8
    cout << "a = " << a << endl; //a = 10
    cout << "pos = " << pos << endl; //pos = 2
 
    str = "  -12  35"; 
    a = stoi(str, &pos, 8); //會(huì)舍棄空白符
    cout << "a = " << a << endl; //a = -10
    cout << "pos = " << pos << endl; //pos = 5
 
    // str = "  -a78"; 
    // a = stoi(str, &pos, 8); //數(shù)字前有字母,調(diào)用會(huì)崩掉
    // cout << "a = " << a << endl; 
    // cout << "pos = " << pos << endl; 
 
    return 0;
}

stol()函數(shù)

#include <string>
long stol(const std::string& str, std::size_t* pos = 0, int base = 10);
long stol(const std::wstring& str, std::size_t* pos = 0, int base = 10);

功能:將字符串str轉(zhuǎn)成 有符號(hào) long 整數(shù)

參數(shù):

  • str:字符串
  • pos:存儲(chǔ)將字符串str轉(zhuǎn)成有符號(hào)整數(shù),處理了str中字符的個(gè)數(shù)的地址,默認(rèn)為NULL
  • base:進(jìn)制,10:十進(jìn)制,8:八進(jìn)制,16:十六進(jìn)制,0:則自動(dòng)檢測(cè)數(shù)值進(jìn)制,str是 0 開頭為八進(jìn)制,str是 0x 或 0X 開頭是十六進(jìn)制,默認(rèn)為十進(jìn)制

stol()函數(shù)指定轉(zhuǎn)換字符串為十進(jìn)制用法

#include <iostream>
#include <string>
using namespace std;
 
int main(int argc, char *argv[])
{
    long a;
    size_t pos = 0;
    string str;
 
    str = "-1235";
    a = stol(str);
    cout << "a = " << a << endl; //a = -1235
 
    str = "1235";
    a = stol(str);
    cout << "a = " << a << endl; //a = 1235
 
    str = "  -12  35"; 
    a = stol(str, &pos); //會(huì)舍棄空白符
    cout << "a = " << a << endl; //a = -12
    cout << "pos = " << pos << endl; //pos = 5
 
    str = "  -12ab35";
    a = stol(str, &pos);
    cout << "a = " << a << endl; //a = -12
    cout << "pos = " << pos << endl; //pos = 5
 
    str = "0123";
    a = stol(str);
    cout << "a = " << a << endl; //a = 123
 
    str = "0x123";
    a = stol(str);
    cout << "a = " << a << endl; //a = 0
 
    return 0;
}

stol()函數(shù)指定轉(zhuǎn)換字符串為十六進(jìn)制用法

#include <iostream>
#include <string>
using namespace std;
 
int main(int argc, char *argv[])
{
    long a;
    size_t pos = 0;
    string str;
 
    str = "0x123";
    a = stol(str, NULL, 16); //base = 16,指定十六進(jìn)制
    cout << "a = " << a << endl; //a = 291
 
   str = "0x123";
    a = stol(str, NULL, 0); //base = 0,自動(dòng)檢測(cè)數(shù)值進(jìn)制
    cout << "a = " << a << endl; //a = 291
 
    str = "-12";
    a = stol(str, &pos, 16); //-(2 + 1*16)
    cout << "a = " << a << endl; //a = -18
    cout << "pos = " << pos << endl; //pos = 3
 
    str = "12";
    a = stol(str, &pos, 16); //2 + 1*16
    cout << "a = " << a << endl; //a = 18
    cout << "pos = " << pos << endl; //pos = 2
 
    str = "  -12  35"; 
    a = stol(str, &pos, 16); //會(huì)舍棄空白符
    cout << "a = " << a << endl; //a = -18
    cout << "pos = " << pos << endl; //pos = 5
 
    str = "  -ab";
    a = stol(str, &pos, 16); //-(11 + 10*16)
    cout << "a = " << a << endl; //a = -171
    cout << "pos = " << pos << endl; //pos = 5
 
    str = "0123";
    a = stol(str, NULL, 16); //(3 + 2*16 + 1*16*16)
    cout << "a = " << a << endl; //a = 291
 
    return 0;
}

stol()函數(shù)指定轉(zhuǎn)換字符串為八進(jìn)制用法

#include <iostream>
#include <string>
using namespace std;
 
int main(int argc, char *argv[])
{
    long a;
    size_t pos = 0;
    string str;
 
    str = "0x123";
    a = stol(str, NULL, 8); //base = 8,指定八進(jìn)制
    cout << "a = " << a << endl; //a = 0
 
   str = "0123"; //(3 + 2*8 + 1*8*8)
    a = stol(str, NULL, 0); //base = 0,自動(dòng)檢測(cè)數(shù)值進(jìn)制
    cout << "a = " << a << endl; //a = 83
 
    str = "-12";
    a = stol(str, &pos, 8); //-(2 + 1*8)
    cout << "a = " << a << endl; //a = -10
    cout << "pos = " << pos << endl; //pos = 3
 
    str = "12";
    a = stol(str, &pos, 8); //2 + 1*8
    cout << "a = " << a << endl; //a = 10
    cout << "pos = " << pos << endl; //pos = 2
 
    str = "  -12  35"; 
    a = stol(str, &pos, 8); //會(huì)舍棄空白符
    cout << "a = " << a << endl; //a = -10
    cout << "pos = " << pos << endl; //pos = 5
 
    // str = "  -a78"; 
    // a = stol(str, &pos, 8); //數(shù)字前有字母,調(diào)用會(huì)崩掉
    // cout << "a = " << a << endl; 
    // cout << "pos = " << pos << endl; 
 
    return 0;
}

stoll()函數(shù)

#include <string>
long long stoll(const std::string& str, std::size_t* pos = 0, int base = 10);
long long stoll(const std::wstring& str, std::size_t* pos = 0, int base = 10);

功能:將字符串str轉(zhuǎn)成 有符號(hào) long long 整數(shù)

參數(shù):

  • str:字符串
  • pos:存儲(chǔ)將字符串str轉(zhuǎn)成有符號(hào)整數(shù),處理了str中字符的個(gè)數(shù)的地址,默認(rèn)為NULL
  • base:進(jìn)制,10:十進(jìn)制,8:八進(jìn)制,16:十六進(jìn)制,0:則自動(dòng)檢測(cè)數(shù)值進(jìn)制,str是 0 開頭為八進(jìn)制,str是 0x 或 0X 開頭是十六進(jìn)制,默認(rèn)為十進(jìn)制

stoll()函數(shù)指定轉(zhuǎn)換字符串為十進(jìn)制用法

#include <iostream>
#include <string>
using namespace std;
 
int main(int argc, char *argv[])
{
    long long a;
    size_t pos = 0;
    string str;
 
    str = "-1235";
    a = stoll(str);
    cout << "a = " << a << endl; //a = -1235
 
    str = "1235";
    a = stoll(str);
    cout << "a = " << a << endl; //a = 1235
 
    str = "  -12  35"; 
    a = stoll(str, &pos); //會(huì)舍棄空白符
    cout << "a = " << a << endl; //a = -12
    cout << "pos = " << pos << endl; //pos = 5
 
    str = "  -12ab35";
    a = stoll(str, &pos);
    cout << "a = " << a << endl; //a = -12
    cout << "pos = " << pos << endl; //pos = 5
 
    str = "0123";
    a = stoll(str);
    cout << "a = " << a << endl; //a = 123
 
    str = "0x123";
    a = stoll(str);
    cout << "a = " << a << endl; //a = 0
 
    return 0;
}

stoll()函數(shù)指定轉(zhuǎn)換字符串為十六進(jìn)制用法

#include <iostream>
#include <string>
using namespace std;
 
int main(int argc, char *argv[])
{
    long long a;
    size_t pos = 0;
    string str;
 
    str = "0x123";
    a = stoll(str, NULL, 16); //base = 16,指定十六進(jìn)制
    cout << "a = " << a << endl; //a = 291
 
   str = "0x123";
    a = stoll(str, NULL, 0); //base = 0,自動(dòng)檢測(cè)數(shù)值進(jìn)制
    cout << "a = " << a << endl; //a = 291
 
    str = "-12";
    a = stoll(str, &pos, 16); //-(2 + 1*16)
    cout << "a = " << a << endl; //a = -18
    cout << "pos = " << pos << endl; //pos = 3
 
    str = "12";
    a = stoll(str, &pos, 16); //2 + 1*16
    cout << "a = " << a << endl; //a = 18
    cout << "pos = " << pos << endl; //pos = 2
 
    str = "  -12  35"; 
    a = stoll(str, &pos, 16); //會(huì)舍棄空白符
    cout << "a = " << a << endl; //a = -18
    cout << "pos = " << pos << endl; //pos = 5
 
    str = "  -ab";
    a = stoll(str, &pos, 16); //-(11 + 10*16)
    cout << "a = " << a << endl; //a = -171
    cout << "pos = " << pos << endl; //pos = 5
 
    str = "0123";
    a = stoll(str, NULL, 16); //(3 + 2*16 + 1*16*16)
    cout << "a = " << a << endl; //a = 291
 
    return 0;
}

stoll()函數(shù)指定轉(zhuǎn)換字符串為八進(jìn)制用法

#include <iostream>
#include <string>
using namespace std;
 
int main(int argc, char *argv[])
{
    long long a;
    size_t pos = 0;
    string str;
 
    str = "0x123";
    a = stoll(str, NULL, 8); //base = 8,指定八進(jìn)制
    cout << "a = " << a << endl; //a = 0
 
   str = "0123"; //(3 + 2*8 + 1*8*8)
    a = stoll(str, NULL, 0); //base = 0,自動(dòng)檢測(cè)數(shù)值進(jìn)制
    cout << "a = " << a << endl; //a = 83
 
    str = "-12";
    a = stoll(str, &pos, 8); //-(2 + 1*8)
    cout << "a = " << a << endl; //a = -10
    cout << "pos = " << pos << endl; //pos = 3
 
    str = "12";
    a = stoll(str, &pos, 8); //2 + 1*8
    cout << "a = " << a << endl; //a = 10
    cout << "pos = " << pos << endl; //pos = 2
 
    str = "  -12  35"; 
    a = stoll(str, &pos, 8); //會(huì)舍棄空白符
    cout << "a = " << a << endl; //a = -10
    cout << "pos = " << pos << endl; //pos = 5
 
    // str = "  -a78"; 
    // a = stoll(str, &pos, 8); //數(shù)字前有字母,調(diào)用會(huì)崩掉
    // cout << "a = " << a << endl; 
    // cout << "pos = " << pos << endl; 
 
    return 0;
}

注意:stoi、stol、stoll 函數(shù)是C++11標(biāo)準(zhǔn)加入的,用g++編譯器編譯需要加參數(shù):-std=c++11

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

相關(guān)文章

  • c++ 如何在libuv中實(shí)現(xiàn)tcp服務(wù)器

    c++ 如何在libuv中實(shí)現(xiàn)tcp服務(wù)器

    這篇文章主要介紹了c++ 如何在libuv中實(shí)現(xiàn)tcp服務(wù)器,幫助大家更好的理解和使用libuv,感興趣的朋友可以了解下
    2021-02-02
  • 一篇文章帶你了解C++的KMP算法

    一篇文章帶你了解C++的KMP算法

    這篇文章主要介紹了c++ 實(shí)現(xiàn)KMP算法的示例,幫助大家更好的理解和學(xué)習(xí)c++,感興趣的朋友可以了解下,希望能給你帶來(lái)幫助
    2021-08-08
  • C++ 非遞歸實(shí)現(xiàn)二叉樹的前中后序遍歷

    C++ 非遞歸實(shí)現(xiàn)二叉樹的前中后序遍歷

    本文將結(jié)合動(dòng)畫和代碼演示如何通過C++ 非遞歸實(shí)現(xiàn)二叉樹的前中后序的遍歷,代碼具有一定的價(jià)值,感興趣的同學(xué)可以學(xué)習(xí)一下
    2021-11-11
  • C語(yǔ)言超詳細(xì)講解棧的實(shí)現(xiàn)及代碼

    C語(yǔ)言超詳細(xì)講解棧的實(shí)現(xiàn)及代碼

    棧(stack)又名堆棧,它是一種運(yùn)算受限的線性表。限定僅在表尾進(jìn)行插入和刪除操作的線性表。這一端被稱為棧頂,相對(duì)地,把另一端稱為棧底。向一個(gè)棧插入新元素又稱作進(jìn)棧、入?;驂簵#前研略胤诺綏m斣氐纳厦?,使之成為新的棧頂元素
    2022-04-04
  • C++中默認(rèn)無(wú)參構(gòu)造函數(shù)的工作機(jī)制淺析

    C++中默認(rèn)無(wú)參構(gòu)造函數(shù)的工作機(jī)制淺析

    構(gòu)造函數(shù)主要作用在于創(chuàng)建對(duì)象時(shí)為對(duì)象的成員屬性賦值,構(gòu)造函數(shù)由編譯器自動(dòng)調(diào)用,無(wú)須手動(dòng)調(diào)用;析構(gòu)函數(shù)主要作用在于對(duì)象銷毀前系統(tǒng)自動(dòng)調(diào)用,執(zhí)行一些清理工作
    2023-02-02
  • 基于C++實(shí)現(xiàn)簡(jiǎn)單日期計(jì)算器

    基于C++實(shí)現(xiàn)簡(jiǎn)單日期計(jì)算器

    這篇文章主要介紹了基于C++實(shí)現(xiàn)簡(jiǎn)單日期計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • 基于C++的攝像頭圖像采集及拼接程序的簡(jiǎn)單實(shí)現(xiàn)

    基于C++的攝像頭圖像采集及拼接程序的簡(jiǎn)單實(shí)現(xiàn)

    本程序是在?ubuntu14.04?平臺(tái)下實(shí)現(xiàn)的,在本項(xiàng)目目錄下,已經(jīng)有編譯生成的可執(zhí)行程序,其中Camera_to_Frmae.cpp是我們從雙攝像頭實(shí)時(shí)抓取單幀圖像的源碼,對(duì)基于C++的攝像頭圖像采集及拼接程序的實(shí)現(xiàn)感興趣的朋友一起看看吧
    2022-01-01
  • Qt把文件夾從A移動(dòng)到B的實(shí)現(xiàn)示例

    Qt把文件夾從A移動(dòng)到B的實(shí)現(xiàn)示例

    本文主要介紹了Qt把文件夾從A移動(dòng)到B的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2025-03-03
  • C語(yǔ)言實(shí)現(xiàn)三子棋小游戲詳解

    C語(yǔ)言實(shí)現(xiàn)三子棋小游戲詳解

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)三子棋小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • QT出現(xiàn)沒有MySQL驅(qū)動(dòng)手動(dòng)編譯詳細(xì)步驟

    QT出現(xiàn)沒有MySQL驅(qū)動(dòng)手動(dòng)編譯詳細(xì)步驟

    這篇文章主要給大家介紹了關(guān)于QT出現(xiàn)沒有MySQL驅(qū)動(dòng)手動(dòng)編譯詳細(xì)步驟的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用QT具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2023-04-04

最新評(píng)論

高唐县| 太湖县| 吉水县| 凤山市| 新乐市| 合水县| 收藏| 太保市| 托克逊县| 霍林郭勒市| 徐水县| 正宁县| 岳阳县| 古交市| 荔浦县| 揭东县| 开平市| 大宁县| 封开县| 丰顺县| 收藏| 湛江市| 怀柔区| 陆河县| 阳城县| 武穴市| 南京市| 大埔县| 江城| 宁海县| 普宁市| 玛多县| 鹤峰县| 鸡东县| 洛宁县| 嘉荫县| 荣成市| 老河口市| 泸水县| 东港市| 泰和县|