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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

C++字符串和数字转换完全攻略

發布時間:2024/4/18 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++字符串和数字转换完全攻略 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

以字符串形式存儲的數字和以數字形式存儲的數字之間是有區別的。

例如,字符串 "2679" 就不是一個數字:它是由 2、6、7、9 這 4 個字符的 ASCII 碼組成的序列。由于字符串 "2679" 不是一個數字,所以編譯器將不允許對它進行加法、乘法和除法之類的數學運算。以數字表示的字符串必須首先轉換為數字形式,然后才能與算術運算符一起使用。

同樣地,數字形式的程序值,例如 int、long 和 double 等,有時也需要轉換為字符串形式,這樣結果字符串才能立即輸出到文件或其他輸入輸出設備,或者存入內存中的某個字符串對象,供以后使用。

當用戶在鍵盤上輸入一個數字時,該數字以字符串的形式輸入,就好像用戶輸入的一系列字符(數字)。在?C++?中,這樣的數字通常通過流提取運算符 >> 來讀取。在存儲到數字類型的變量之前,該運算符會根據需要自動執行轉換。在輸出期間,數字到字符串的反向轉換由流輸出運算符 << 執行。

使用字符串流對象進行數字轉換

C++ 有兩個類,ostringstream?和?istringstream,可以用來對內存中的值執行字符串/數字轉換。

ostringstream 類是 ostream 的子類(cout 也屬于該類),并使用流插入運算符 << 將數值轉換為字符串。ostringstream 類型對象的工作方式與cout和文件對象的工作方式相同,但它不是將數據寫入屏幕或文件,而是寫入它所包含的字符串對象中。

每次在 ostringstream 對象上使用 << 時,它都會執行必要的數字到字符串的轉換,并將結果追加到其字符串的末尾。除了支持 ostream 類的所有成員函數和運算符外,ostringstream 對象還支持表 1 中所示的 str 成員函數。

istringstream 類是從 istream 派生的。它內部包含一個字符串對象,函數將它作為可以從中"讀取"的輸入流。

輸入流可以在創建對象時由 istringstream 構造函數設置,也可以在創建對象后通過調用 str(string s) 函數來設置。流提取操作符 >> 從封閉的字符串中讀取,并在必要時將字符串轉換為數字。表 1 列出了 istringstream 的成員函數,必須在程序中包含?sstream?頭文件才能使用這些類。
?

表 1 ostringstream 和 istringstream 類的成員函數成員函數描?述
istringstream(string s)istringstream 的構造函數:設置對象的輸入流的初始值。示例:
istringstream istr ("50 64 28");
ostringstream(string s)ostringstream 的構造函數:設置對象的輸出流的初始值。示例:
ostringstream ostr("50 64 28");
string str()返回 ostringstream 或 istringstream 對象中包含的字符串。示例:
string is?=?istr.str();
string os = ostr.str ();
void str(string &s)設置作為對象的輸入或輸出流的字符串。示例:
ostr.str ("50 64 28");
istr.str("50 64 28");


下面的程序演示了這些類的用法:

  • //This program illustrates the use of sstream objects
  • #include <sstream>
  • #include <iostream>
  • #include <string>
  • using namespace std;
  • ?
  • int main()
  • {
  • string str = "John 20 50"; // string to read from
  • const char *cstr = "Amy 30 42"; // Cstring to read from
  • istringstream istr1 (str); // istr1 will read from str
  • istringstream istr2; // istr2 will read from cstr
  • ostringstream ostr; // The ostringstream object to write to
  • string name;
  • int score1, score2, average_score;
  • ?
  • // Read name and scores and compute average then write to ostr
  • istr1 >> name >> score1 >> score2;
  • average_score = (score1 + score2)/2;
  • ostr << name << " has average score" << average_score << "\n";
  • // Set istr2 to read from the C string and repeat the above
  • istr2.str(cstr);
  • istr2 >> name >> score1 >> score2;
  • average_score = (score1 + score2)/2;
  • ostr << name << " has average score" << average_score << "\n";
  • // Switch to hexadeximal output on ostr
  • ostr << hex;
  • // Write Amy's scores in hexadecimal
  • ostr << name << "'s scores in hexadecimal are: " << score1 << " and " << score2 << "\n";
  • // Extract the string from ostr and print it to the screen
  • cout << ostr.str();
  • return 0;
  • }
  • 程序輸出結果:

    John has average score35
    Amy has average score36
    Amy's scores in hexadecimal are: 1e and 2a

    請注意,這些類具有 ostream 和 istream 對象的全部功能,包括使用八進制和十六進制等不同進制將數字轉換為字符串的能力。當然,它們也是有缺陷的,它們強制程序員創建 sstream 對象,只有這樣才能使用它們的插入和提取運算符執行轉換。

    數字轉換函數

    C++ 11 提供了若干 to_string(T value) 函數來將 T 類型的數字值轉換為字符串形式。以下是幾個 to_string() 函數的列表:

    string to_string(int value)
    string to_string(long value)
    string to_string(double value)

    來看以下代碼示例:

  • int a = 5;
  • string str = to_string(a*a);
  • cout << " The square of 5 is " << str << endl;
  • 以上示例即顯示了該系列函數的用法。它將打印如下字符串:

    The square of 5 is 25

    to_string() 函數無法處理非十進制整數的轉換。如果需要該功能,則應該使用 ostringsteam 對象來完成該轉換。

    字符串到數字的轉換可以通過 stoX() 系列函數來執行。該系列函數的成員可以將字符串轉換為 int、long、float 和 double 類型的數字。具體語法如下所示:

    int stoi(const strings str, size_t* pos = 0, int base = 10)
    long stol(const strings str, size_t* pos = 0, int base = 10)
    float stof(const strings str, size_t* pos = 0)
    double stod(const strings str, size_t* pos = 0)

    第一個形參 str 是一個字符串(例如 "-342" 或 "3.48" 等),它將被轉換為恰當的數字形式。這些函數可以將 str 可能的最長前綴轉換為數字,并返回一個整數地址 pos,pos 中保存了 str 無法被轉換的第一個字符的索引。類型 size_t 是在標準庫中定義的,常用于表示無符號整數的大小或數組、矢量、字符串中的一個索引。

    例如,如果試圖轉換字符串 "-34iseven",則將成功返回整數 -34,而無法轉換的第一個字符的位置 pos 則被設置為 3。base 形參僅適用于整數轉換,指示用于轉換的進制。pos 和 base 形參都是可選的,所以它們可以被忽略。如果 pos 被忽略,則不會存儲停止字符的索引。如果 base 被忽略,則默認為十進制。如果字符串 str 包含一個無效值,例如 "is-34 even?",則不會進行轉換,函數將拋出一個 invalid_argument 異常。

    下面的程序演示了字符串轉換函數的用法:

    純文本復制
  • // This program demonstrates the use of the stoXXX()
  • // numeric conversion functions.
  • #include <string>
  • #include <iostream>
  • using namespace std;
  • ?
  • int main()
  • {
  • string str; // string to convert
  • size_t pos; // Hold position of stopping character
  • //Convert string to double
  • str = "-342.57is a number";
  • cout << "The string is " << str << endl;
  • double d = stod(str, &pos);
  • cout << "The converted double is " << d << endl;
  • cout << "The stopping character is " << str[pos] << " at position " << pos << endl;
  • // Convert string to int (default to decimal)
  • str = "-342.57is a number";
  • cout << "\nThe string is " << str << endl;
  • int i = stoi(str, &pos);
  • cout << "The converted integer is " << i << endl;
  • cout << "The stopping character is " << str[pos] <<" at position " << pos << endl;
  • // Convert string to int (base is binary)
  • str = "01110binary number";
  • cout << "\nThe string is " << str << endl;
  • i = stoi (str, &pos, 2);
  • cout << "The converted binary integer is " << i << endl;
  • cout << "The stopping character is " << str[pos] << " at position " << pos << endl;
  • return 0;
  • }
  • 程序輸出結果:

    The string is -342.57is a number
    The converted double is -342.57
    The stopping character is i at position 7

    The string is -342.57is a number
    The converted integer is -342
    The stopping character is . at position 4

    The string is 01110binary number
    The converted binary integer is 14
    The stopping character is b at position 5

    總結

    以上是生活随笔為你收集整理的C++字符串和数字转换完全攻略的全部內容,希望文章能夠幫你解決所遇到的問題。

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