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

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

 更新時間:2023年03月23日 09:13:56   作者:微塵8  
這篇文章主要為大家詳細(xì)介紹了C++中stoi、stol、stoll函數(shù)的具體用法,文中的示例代碼講解詳細(xì),對我們學(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)成 有符號 int 整數(shù)

參數(shù):

  • str:字符串
  • pos:存儲將字符串str轉(zhuǎn)成有符號整數(shù),處理了str中字符的個數(shù)的地址,默認(rèn)為NULL
  • base:進(jìn)制,10:十進(jìn)制,8:八進(jìn)制,16:十六進(jìn)制,0:則自動檢測數(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); //會舍棄空白符
    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,自動檢測數(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); //會舍棄空白符
    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,自動檢測數(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); //會舍棄空白符
    cout << "a = " << a << endl; //a = -10
    cout << "pos = " << pos << endl; //pos = 5
 
    // str = "  -a78"; 
    // a = stoi(str, &pos, 8); //數(shù)字前有字母,調(diào)用會崩掉
    // 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)成 有符號 long 整數(shù)

參數(shù):

  • str:字符串
  • pos:存儲將字符串str轉(zhuǎn)成有符號整數(shù),處理了str中字符的個數(shù)的地址,默認(rèn)為NULL
  • base:進(jìn)制,10:十進(jìn)制,8:八進(jìn)制,16:十六進(jìn)制,0:則自動檢測數(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); //會舍棄空白符
    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,自動檢測數(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); //會舍棄空白符
    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,自動檢測數(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); //會舍棄空白符
    cout << "a = " << a << endl; //a = -10
    cout << "pos = " << pos << endl; //pos = 5
 
    // str = "  -a78"; 
    // a = stol(str, &pos, 8); //數(shù)字前有字母,調(diào)用會崩掉
    // 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)成 有符號 long long 整數(shù)

參數(shù):

  • str:字符串
  • pos:存儲將字符串str轉(zhuǎn)成有符號整數(shù),處理了str中字符的個數(shù)的地址,默認(rèn)為NULL
  • base:進(jìn)制,10:十進(jìn)制,8:八進(jìn)制,16:十六進(jìn)制,0:則自動檢測數(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); //會舍棄空白符
    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,自動檢測數(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); //會舍棄空白符
    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,自動檢測數(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); //會舍棄空白符
    cout << "a = " << a << endl; //a = -10
    cout << "pos = " << pos << endl; //pos = 5
 
    // str = "  -a78"; 
    // a = stoll(str, &pos, 8); //數(shù)字前有字母,調(diào)用會崩掉
    // 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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:

相關(guān)文章

  • C中的volatile使用方法

    C中的volatile使用方法

    volatile 影響編譯器編譯的結(jié)果,指出,volatile 變量是隨時可能發(fā)生變化的,與volatile變量有關(guān)的運(yùn)算,不要進(jìn)行編譯優(yōu)化,以免出錯
    2013-02-02
  • C語言實(shí)現(xiàn)計算器的兩種方法

    C語言實(shí)現(xiàn)計算器的兩種方法

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)計算器的兩種方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • 簡單談?wù)凜++ 中指針與引用

    簡單談?wù)凜++ 中指針與引用

    下面用通俗易懂的話來概述一下,指針-對于一個類型T,T*就是指向T的指針類型,也即一個T*類型的變量能夠保存一個T對象的地址,而類型T是可以加一些限定詞的,引用-引用是一個對象的別名,主要用于函數(shù)參數(shù)和返回值類型,符號X&表示X類型的引用。
    2015-09-09
  • 深入理解C++模板如何實(shí)現(xiàn)多態(tài)思想

    深入理解C++模板如何實(shí)現(xiàn)多態(tài)思想

    這篇文章主要為大家詳細(xì)介紹了C++模板如何實(shí)現(xiàn)多態(tài)的相關(guān)資料,文中的示例代碼講解詳細(xì),對我們深入了解C++有一定的幫助,感興趣的可以了解一下
    2023-03-03
  • 詳解vs2022創(chuàng)建及調(diào)用.lib的方法

    詳解vs2022創(chuàng)建及調(diào)用.lib的方法

    這篇文章主要介紹了vs2022創(chuàng)建及調(diào)用.lib的方法,調(diào)用Lib的原則就是可以讓編譯器找到頭文件和庫文件的目錄,并正確引入,本文給大家詳細(xì)講解需要的朋友可以參考下
    2022-11-11
  • 桶排序算法的理解及C語言版代碼示例

    桶排序算法的理解及C語言版代碼示例

    桶排序算法顧名思義,就是把要排序的元素分桶排序后合并結(jié)果,這里我們就來看一下桶排序算法的理解及C語言版代碼示例:
    2016-07-07
  • 手把手教你用C語言實(shí)現(xiàn)三子棋

    手把手教你用C語言實(shí)現(xiàn)三子棋

    三子棋是黑白棋的一種。三子棋是一種民間傳統(tǒng)游戲,又叫九宮棋、圈圈叉叉、一條龍、井字棋等。這篇文章就教你如何用C語言實(shí)現(xiàn)三子棋的功能
    2021-08-08
  • C語言中字符串常用函數(shù)strcat與strcpy的用法介紹

    C語言中字符串常用函數(shù)strcat與strcpy的用法介紹

    以下是對C語言中字符串常用函數(shù)strcat與strcpy的使用方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以參考下
    2013-07-07
  • 從頭學(xué)習(xí)C語言之if語句的使用

    從頭學(xué)習(xí)C語言之if語句的使用

    這篇文章主要為大家詳細(xì)介紹了C語言之if語句的使用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-01-01
  • C語言實(shí)現(xiàn)十六進(jìn)制與二進(jìn)制的相互轉(zhuǎn)換

    C語言實(shí)現(xiàn)十六進(jìn)制與二進(jìn)制的相互轉(zhuǎn)換

    這篇文章主要為大家詳細(xì)介紹了如何利用c語言實(shí)現(xiàn)將文件中十六進(jìn)制數(shù)據(jù)與二進(jìn)制數(shù)據(jù)相互轉(zhuǎn)換,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,感興趣的可以學(xué)習(xí)一下
    2022-11-11

最新評論