C++ 中的 Bool 轉 int 轉換


我們將在本文中瞭解如何在 C++ 中轉換 Bool 為 int 等價物。Bool 是 C++ 中的資料型別,我們針對它可以使用 truefalse 關鍵字。如果要轉換 Bool 為 int,則可以使用型別轉換。始終 true 的值將為 1,而 false 的值為 0。

示例

#include <iostream>
using namespace std;
main() {
   bool my_bool;
   my_bool = true;
   cout << "The int equivalent of my_bool is: " << int(my_bool) << endl;
   my_bool = false;
   cout << "The int equivalent of my_bool is: " << int(my_bool);
}

輸出

The int equivalent of my_bool is: 1
The int equivalent of my_bool is: 0

更新於: 30-7-2019

11K+ 瀏覽

助力您的 職業

完成課程獲得認證

立即開始
廣告
© . All rights reserved.