日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

C++字符串转换(stoi;stol;stoul;stoll;stoull;stof;stod;stold)

發布時間:2023/12/15 综合教程 38 生活家
生活随笔 收集整理的這篇文章主要介紹了 C++字符串转换(stoi;stol;stoul;stoll;stoull;stof;stod;stold) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、stoi

  頭文件string

int stoi (const string&  str, size_t* idx = 0, int base = 10);
int stoi (const wstring& str, size_t* idx = 0, int base = 10);

  

  將字符串轉換為整數
  解析str將其內容解釋為指定基數的整數,并以int值的形式返回。

  如果idx不是空指針,則該函數還將idx的值設置為數字后str中第一個字符的位置。

  該函數使用strtol(或wcstol)來執行轉換(有關該過程的更多詳細信息,請參閱strtol)。

參數

str
字符串對象,表示整數。
idx
指向size_t類型對象的指針,其值由函數設置為數值后str中下一個字符的位置。
此參數也可以是空指針,在這種情況下不使用它。
base
確定有效字符及其解釋的數字基數(基數)。
如果為0,則使用的基數由序列中的格式確定(有關詳細信息,請參閱strtol)。 請注意,默認情況下,此參數為10,而不是0。

返回值

  成功時,該函數將轉換后的整數作為int值返回。

// stoi example
#include <iostream>   // std::cout
#include <string>     // std::string, std::stoi

int main ()
{
  std::string str_dec = "2001, A Space Odyssey";
  std::string str_hex = "40c3";
  std::string str_bin = "-10010110001";
  std::string str_auto = "0x7f";

  std::string::size_type sz;   // alias of size_t

  int i_dec = std::stoi (str_dec,&sz);
  int i_hex = std::stoi (str_hex,nullptr,16);
  int i_bin = std::stoi (str_bin,nullptr,2);
  int i_auto = std::stoi (str_auto,nullptr,0);

  std::cout << str_dec << ": " << i_dec << " and [" << str_dec.substr(sz) << "]
";
  std::cout << str_hex << ": " << i_hex << '
';
  std::cout << str_bin << ": " << i_bin << '
';
  std::cout << str_auto << ": " << i_auto << '
';

  return 0;
}

復雜度

未指定,但通常在解釋的字符數中是線性的。

數據范圍

修改idx指向的值(如果不為零)。

異常

如果無法執行轉換,則拋出invalid_argument異常。
如果讀取的值超出int的可表示值范圍,則拋出out_of_range異常。
無效的idx會導致未定義的行為。

二、stol

頭文件string

long stol (const string&  str, size_t* idx = 0, int base = 10);
long stol (const wstring& str, size_t* idx = 0, int base = 10);

將字符串轉換為long int
解析str將其內容解釋為指定基數的整數,并以long int類型的值返回。

如果idx不是空指針,則該函數還將idx的值設置為數字后str中第一個字符的位置。

該函數使用strtol(或wcstol)來執行轉換(有關該過程的更多詳細信息,請參閱strtol)。

參數

str
字符串對象,表示整數。
idx
指向size_t類型對象的指針,其值由函數設置為數值后str中下一個字符的位置。
此參數也可以是空指針,在這種情況下不使用它。
base
確定有效字符及其解釋的數字基數(基數)。
如果為0,則使用的基數由序列中的格式確定(有關詳細信息,請參閱strtol)。 請注意,默認情況下,此參數為10,而不是0。

返回值

  成功時,該函數將轉換后的整數作為long int類型的值返回。

// stol example
#include <iostream>   // std::cout
#include <string>     // std::string, std::stol

int main ()
{
  std::string str_dec = "1987520";
  std::string str_hex = "2f04e009";
  std::string str_bin = "-11101001100100111010";
  std::string str_auto = "0x7fffff";

  std::string::size_type sz;   // alias of size_t

  long li_dec = std::stol (str_dec,&sz);
  long li_hex = std::stol (str_hex,nullptr,16);
  long li_bin = std::stol (str_bin,nullptr,2);
  long li_auto = std::stol (str_auto,nullptr,0);

  std::cout << str_dec << ": " << li_dec << '
';
  std::cout << str_hex << ": " << li_hex << '
';
  std::cout << str_bin << ": " << li_bin << '
';
  std::cout << str_auto << ": " << li_auto << '
';

  return 0;
}

  

復雜度

未指定,但通常在解釋的字符數中是線性的。

數據范圍

修改idx指向的值(如果不為零)。

異常

如果無法執行轉換,則拋出invalid_argument異常。
如果讀取的值超出long int的可表示值范圍,則拋出invalid_argument或out_of_range異常。
無效的idx會導致未定義的行為。

三、stoul

頭文件string

unsigned long stoul (const string&  str, size_t* idx = 0, int base = 10);
unsigned long stoul (const wstring& str, size_t* idx = 0, int base = 10);

將字符串轉換為無符號整數
解析str將其內容解釋為指定基數的整數,該基數作為無符號長整數值返回。

如果idx不是空指針,則該函數還將idx的值設置為數字后str中第一個字符的位置。

該函數使用strtoul(或wcstoul)執行轉換(有關該過程的更多詳細信息,請參閱strtol)。

參數

str
字符串對象,表示整數。
inx
指向size_t類型對象的指針,其值由函數設置為數值后str中下一個字符的位置。
此參數也可以是空指針,在這種情況下不使用它。
base
確定有效字符及其解釋的數字基數(基數)。
如果為0,則使用的基數由序列中的格式確定(有關詳細信息,請參閱strtol)。 請注意,默認情況下,此參數為10,而不是0。

返回值

  成功時,該函數將轉換后的整數作為無符號長整數值返回。

// stoul example
#include <iostream>   // std::cin, std::cout
#include <string>     // std::string, std::stoul, std::getline

int main ()
{
  std::string str;
  std::cout << "Enter an unsigned number: ";
  std::getline (std::cin,str);
  unsigned long ul = std::stoul (str,nullptr,0);
  std::cout << "You entered: " << ul << '
';
  return 0;
}

 

復雜度

  未指定,但通常在解釋的字符數中是線性的。

數據范圍

  修改idx指向的值(如果不為零)。

異常

  如果無法執行轉換,則拋出invalid_argument異常。
  如果讀取的值超出無符號長度的可表示值范圍,則拋出out_of_range異常。
  無效的idx會導致未定義的行為。

四、stoll

頭文件string

long long stoll (const string&  str, size_t* idx = 0, int base = 10);
long long stoll (const wstring& str, size_t* idx = 0, int base = 10);

將字符串轉換為long long
解析str將其內容解釋為指定基數的整數,并將其作為long long類型的值返回。

如果idx不是空指針,則該函數還將idx的值設置為數字后str中第一個字符的位置。

該函數使用strtoll(或wcstoll)來執行轉換(有關該過程的更多詳細信息,請參閱strtol)。

參數

str
字符串對象,表示整數。
idx
指向size_t類型對象的指針,其值由函數設置為數值后str中下一個字符的位置。
此參數也可以是空指針,在這種情況下不使用它。
base
確定有效字符及其解釋的數字基數(基數)。
如果為0,則使用的基數由序列中的格式確定(有關詳細信息,請參閱strtol)。 請注意,默認情況下,此參數為10,而不是0。

返回值

成功時,該函數返回轉換后的整數作為long long類型的值。

// stoll example
#include <iostream>   // std::cout
#include <string>     // std::string, std::stoll

int main ()
{
  std::string str = "8246821 0xffff 020";

  std::string::size_type sz = 0;   // alias of size_t

  while (!str.empty()) {
    long long ll = std::stoll (str,&sz,0);
    std::cout << str.substr(0,sz) << " interpreted as " << ll << '
';
    str = str.substr(sz);
  }

  return 0;
}

復雜度

未指定,但通常在解釋的字符數中是線性的。

數據范圍

修改idx指向的值(如果不為零)。

異常

如果無法執行轉換,則拋出invalid_argument異常。
如果讀取的值超出可表示值的范圍超長,則拋出out_of_range異常。
無效的idx會導致未定義的行為。

五、stoull

頭文件string

unsigned long long stoull (const string&  str, size_t* idx = 0, int base = 10);
unsigned long long stoull (const wstring& str, size_t* idx = 0, int base = 10);

將字符串轉換為unsigned long long
解析str將其內容解釋為指定基數的整數,該值作為unsigned long long類型的值返回。

如果idx不是空指針,則該函數還將idx的值設置為數字后str中第一個字符的位置。

該函數使用strtoull(或wcstoull)執行轉換(有關該過程的更多詳細信息,請參閱strtol)。

參數

str
字符串對象,表示整數。
idx
指向size_t類型對象的指針,其值由函數設置為數值后str中下一個字符的位置。
此參數也可以是空指針,在這種情況下不使用它。
base
確定有效字符及其解釋的數字基數(基數)。
如果為0,則使用的基數由序列中的格式確定(有關詳細信息,請參閱strtol)。 請注意,默認情況下,此參數為10,而不是0。

返回值

成功時,該函數將轉換后的整數作為unsigned long long類型的值返回。

// stoull example
#include <iostream>   // std::cout
#include <string>     // std::string, std::stoull

int main ()
{
  std::string str = "8246821 0xffff 020 -1";

  std::string::size_type sz = 0;   // alias of size_t

  while (!str.empty()) {
    unsigned long long ull = std::stoull (str,&sz,0);
    std::cout << str.substr(0,sz) << " interpreted as " << ull << '
';
    str = str.substr(sz);
  }

  return 0;
}

復雜度

未指定,但通常在解釋的字符數中是線性的。

數據范圍

修改idx指向的值(如果不為零)。

異常

如果無法執行轉換,則拋出invalid_argument異常。
如果讀取的值超出無符號long long的可表示值范圍,則拋出out_of_range異常。
無效的idx會導致未定義的行為。

六、stof

頭文件string

float stof (const string&  str, size_t* idx = 0);
float stof (const wstring& str, size_t* idx = 0);

將字符串轉換為float
解析str將其內容解釋為浮點數,該值作為float類型返回。

如果idx不是空指針,則該函數還將idx的值設置為數字后str中第一個字符的位置。

該函數使用strtod(或wcstod)來執行轉換(有關該過程的更多詳細信息,請參閱strtod)。 請注意,這些函數接受的格式取決于當前的語言環境。

參數

str
具有浮點數表示的String對象。
idx
指向size_t類型對象的指針,其值由函數設置為數值后str中下一個字符的位置。
此參數也可以是空指針,在這種情況下不使用它。

返回值

成功時,該函數將轉換后的浮點數作為float類型的值返回。

// stof example
#include <iostream>   // std::cout
#include <string>     // std::string, std::stof

int main ()
{
  std::string orbits ("686.97 365.24");
  std::string::size_type sz;     // alias of size_t

  float mars = std::stof (orbits,&sz);
  float earth = std::stof (orbits.substr(sz));
  std::cout << "One martian year takes " << (mars/earth) << " Earth years.
";
  return 0;
}

復雜度

未指定,但通常在解釋的字符數中是線性的。

數據范圍

修改idx指向的值(如果不為零)。

異常

如果無法執行轉換,則拋出invalid_argument異常。
如果讀取的值超出float的可表示值范圍(在某些庫實現中,這包括下溢),則拋出out_of_range異常。
無效的idx會導致未定義的行為。

七、stod

頭文件string

double stod (const string&  str, size_t* idx = 0);
double stod (const wstring& str, size_t* idx = 0);

將字符串轉換為double
解析str將其內容解釋為浮點數,該值返回為double類型的值。

如果idx不是空指針,則該函數還將idx的值設置為數字后str中第一個字符的位置。

該函數使用strtod(或wcstod)來執行轉換(有關該過程的更多詳細信息,請參閱strtod)。 請注意,這些函數接受的格式取決于當前的語言環境。

參數

str
具有浮點數表示的String對象。
idx
指向size_t類型對象的指針,其值由函數設置為數值后str中下一個字符的位置。
此參數也可以是空指針,在這種情況下不使用它。

返回值

成功時,該函數將轉換后的浮點數作為double類型的值返回。

// stod example
#include <iostream>   // std::cout
#include <string>     // std::string, std::stod

int main ()
{
  std::string orbits ("365.24 29.53");
  std::string::size_type sz;     // alias of size_t

  double earth = std::stod (orbits,&sz);
  double moon = std::stod (orbits.substr(sz));
  std::cout << "The moon completes " << (earth/moon) << " orbits per Earth year.
";
  return 0;
}

復雜度

未指定,但通常在解釋的字符數中是線性的。

數據范圍

修改idx指向的值(如果不為零)。

異常

如果無法執行轉換,則拋出invalid_argument異常。
如果讀取的值超出double的可表示值范圍(在某些庫實現中,這包括下溢),則拋出out_of_range異常。
無效的idx會導致未定義的行為。

八、stold

頭文件string

long double stold (const string&  str, size_t* idx = 0);
long double stold (const wstring& str, size_t* idx = 0);

將字符串轉換為long double
解析str將其內容解釋為浮點數,該值作為long double類型返回。

如果idx不是空指針,則該函數還將idx的值設置為數字后str中第一個字符的位置。

該函數使用strtold(或wcstold)來執行轉換(有關該過程的更多詳細信息,請參閱strtod)。

參數

str
具有浮點數表示的String對象。
idx
指向size_t類型對象的指針,其值由函數設置為數值后str中下一個字符的位置。
此參數也可以是空指針,在這種情況下不使用它。

返回值

成功時,該函數將轉換后的浮點數作為long double類型的值返回。

// stold example
#include <iostream>   // std::cout
#include <string>     // std::string, std::stod

int main ()
{
  std::string orbits ("90613.305 365.24");
  std::string::size_type sz;     // alias of size_t

  long double pluto = std::stod (orbits,&sz);
  long double earth = std::stod (orbits.substr(sz));
  std::cout << "Pluto takes " << (pluto/earth) << " years to complete an orbit.
";
  return 0;
}

復雜度

未指定,但通常在解釋的字符數中是線性的。

數據范圍

修改idx指向的值(如果不為零)。

異常

如果無法執行轉換,則拋出invalid_argument異常。
如果讀取的值超出了可表示值的范圍(在某些庫實現中,這包括下溢),則拋出out_of_range異常。
無效的idx會導致未定義的行為。

本文來自博客園,作者:Mr-xxx,轉載請注明原文鏈接:https://www.cnblogs.com/MrLiuZF/p/13591618.html

總結

以上是生活随笔為你收集整理的C++字符串转换(stoi;stol;stoul;stoll;stoull;stof;stod;stold)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。