首頁> 後端開發> C++> 主體

C++程式將字串類型變數轉換為布林類型

WBOY
發布: 2023-09-15 17:49:02
轉載
1066 人瀏覽過

C++程式將字串類型變數轉換為布林類型

在 C 中,布林變數由二進位資料 true 或 false 組成,字串變數是字母、數字和特殊字元的序列。編譯器本身無法將字串轉換為布林值,但有多種方法可以執行此轉換。我們探索將字串值轉換為布林值的各種方法。

如果我們考慮一下演算法,那就很簡單了。我們採用字串值並使用各種方式將其轉換為布林值。

演算法(廣義)

  • 在字串變數中取得輸入。
  • 將字串值(true 或 false)轉換為布林值。
  • 輸出值。

使用 boolalpha 和 istringstream

Boolalpha 是一個流 I/O 操縱器,可用於操縱布林值和字母數字值。 Istringstream 是一個字串流,用於在字元流上實現不同的功能。由於 boolalpha 與流一起使用,因此它可以與 istringstream 一起使用,將字串值轉換為布林值。

文法

string ip = ; bool op; istringstream(ip) >> std::boolalpha >> op;
登入後複製

演算法
  • 在字串變數中取得輸入。
  • 將值放入 istringstream 物件中,並使用 boolalpha 將值指派給布林變數。
  • 輸出值。

範例

#include  #include using namespace std; bool solve(string ip) { bool op; istringstream(ip) >> std::boolalpha >> op; return op; } int main() { string ip = "true"; bool op = solve(ip); cout << "The value of ip is: " << ip <
         
登入後複製

輸出

The value of ip is: true The value of op is: 1
登入後複製

在此範例中,我們採用字串值作為輸入。然後,我們使用 istringstream 物件來包含字串值,然後使用 boolalpha 修飾符將其轉換為布林變數。我們列印輸入和輸出值進行比較。

使用字串比較

In the next example, we have done a basic string comparison to convert a string value into a Boolean value. If the string value is equal to 'false', then 0 is returned; otherwise, 1 is returned. to be noted that this returns true for all strings other than 'false'. But this method is the easiest to implement.

語法

string ip = ; bool op = ip != “false”;
登入後複製

演算法
  • 在字串變數 ip 中取得輸入。
  • 採用布林變數運算。
    • 如果 ip 與「false」相同,則
    • op = false
    • 否則,
    • op = true
  • 顯示op的值。

範例

#include  using namespace std; bool solve(string s) { return s != "false"; } using namespace std; int main() { string ip = "true"; bool op = solve(ip); cout<< "The input value is: " << ip << endl; cout<< "The output value is: " << op << endl; return 0; }
登入後複製

輸出

The input value is: true The output value is: 1
登入後複製

使用 std::stoi

在前面的範例中,我們只將“true”轉換為布林值“1”,將“false”轉換為布林值“0”。現在,在某些情況下,字串值可能是 0 或 1。對於這種情況,我們可以使用 stoi 函數將字串值轉換為整數,然後轉換為布林值。 stoi 函數將字串值轉換為整數,並使用明確類型轉換,我們可以將該值轉換為布林值。

文法

string ip = ; bool op = (bool)stoi(ip);
登入後複製

演算法
  • 在字串變數 ip 中取得輸入。
  • 採用布林變數運算。
  • 將值明確轉換為 bool 為 stoi(ip) 的結果。
  • 顯示op的值。

範例

#include  #include  using namespace std; bool solve(string s) { //using std:: stoi function return (bool)stoi(s); } using namespace std; int main() { string ip = "1"; bool op = solve(ip); cout<< "The input value is: " << ip << endl; cout<< "The output value is: " << op << endl; return 0; }
登入後複製

輸出

The input value is: 1 The output value is: 1
登入後複製
結論

我們將字串作為輸入,其中可能包含任何值「true」、「1」、「false」或「0」。前兩個方法將「true」或「false」分別轉換為 1 和 0。如果我們用“1”或“0”替換“true”或“false”,它也會以相同的方式運作。但在第三個範例中,如果我們將'1' 或'0' 更改為'true' 或'false',它將不起作用,因為stoi 函數無法將不包含字母數字字元的字串轉換為整數值,因此不能轉換為布林值。因此,根據使用情況,我們必須確定要使用的最佳方法。

在特定專案中使用某些第三方程式庫或 API 時,需要進行字串到布林值的轉換。有些API或函式庫以字串格式輸出,為了讓結果相容,我們必須將字串值轉換為布林值。 ###

以上是C++程式將字串類型變數轉換為布林類型的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!