C++ keywords (关键字): bool

C++ keywords (关键字): bool

1. Usage - 用法

bool type: as the declaration of the type
bool 类型:用作该类型的声明

2. Boolean type - 布尔类型

bool - type, capable of holding one of the two values: true or false. The value of sizeof(bool) is implementation defined and might differ from 1.
足以保有二个值 truefalse 之一的类型。sizeof(bool) 的值由实现定义,而且可以异于 1。

3. Boolean literals - 布尔字面量

The Boolean literals are the keywords true and false. They are prvalues of type bool.
布尔字面量是关键词 truefalse。它们是 bool 类型的纯右值。

prvalue (pure rvalue):纯右值

See Integral conversions for implicit conversions from bool to other types and boolean conversions for the implicit conversions from other types to bool.
从 bool 到其他类型的隐式转换见整数转换,从其他类型到 bool 的隐式转换见布尔转换。

//============================================================================
// Name        : std::map::bool
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>

int main()
{
	std::cout << std::boolalpha << true << '\n' << false << '\n' << std::noboolalpha << true << '\n' << false << '\n';

	return 0;
}


true
false
1
0

4. Boolean conversions - 布尔转换

A prvalue of integral, floating-point, unscoped enumeration, pointer, and pointer-to-member types can be converted to a prvalue of type bool.
整数、浮点、无作用域枚举、指针和成员指针类型的纯右值,可转换成 bool 类型的纯右值。

The value zero (for integral, floating-point, and unscoped enumeration) and the null pointer and the null pointer-to-member values become false. All other values become true.
零值 (对于整数、浮点和无作用域枚举)、空指针值和空成员指针值变为 false。所有其他值变为 true

In the context of a direct-initialization, a bool object may be initialized from a prvalue of type std::nullptr_t, including nullptr. The resulting value is false. However, this is not considered to be an implicit conversion.
直接初始化的语境中,可以 std::nullptr_t 类型纯右值 (包括 nullptr) 初始化 bool 对象。结果为 false。然而不认为它是隐式转换。

5. C++ Keywords: bool

The Boolean data type is used to declare a variable whose value will be set as true (1) or false (0). To declare such a value, you use the bool keyword. The variable can then be initialized with the starting value. A Boolean constant is used to check the state of a variable, an expression, or a function, as true or false.
布尔数据类型用于声明一个变量,其值将设置为 true (1) or false (0)。要声明这样的值,请使用 bool 关键字。然后可以使用起始值初始化变量。布尔常量用于检查变量,表达式或函数的状态为 true 还是 false

You can declare a Boolean a variable as:
您可以将布尔变量声明为:

bool GotThePassingGrade = true;

Later in the program, for a student who got a failing grade, you can assign the other value, like this
在程序的后面,对于成绩不及格的学生,您可以分配其他值,如下所示

GotThePassingGrade = false;
//============================================================================
// Name        : std::map::bool
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
	bool MachineIsWorking = true;

	cout << "Since this machine is working, its value is " << MachineIsWorking << endl;

	MachineIsWorking = false;

	cout << "The machine has stopped operating. " << "Now its value is " << MachineIsWorking << endl;

	return 0;
}


Since this machine is working, its value is 1
The machine has stopped operating. Now its value is 0

6. Bool data type in C++

The ISO/ANSI C++ Standard has added certain new data types to the original C++ specifications.They are provided to provide better control in certain situations as well as for providing conveniences to C++ programmers.
ISO/ANSI C++ 标准在原始 C++ 规范中添加了某些新的数据类型,以在某些情况下提供更好的控制以及为 C++ 程序员提供便利。

One of the new data type is: bool
新数据类型之一是:bool

Syntax (句法):

bool b1 = true;      // declaring a boolean variable with true value   

In C++, the data type bool has been introduced to hold a boolean value, true or false. The values true or false have been added as keywords in the C++ language.
在 C++ 中,引入了数据类型 bool 来保存布尔值 truefalse。值 truefalse 已作为 C++ 语言中的关键字添加。

  • The default numeric value of true is 1 and false is 0.

  • We can use bool type variables or values true and false in mathematical expressions also. For instance,
    我们也可以在数学表达式中使用布尔型变量或 true and false 值。例如,

int x = false + true + 6;

is valid and the expression on right will evaluate to 7 as false has value 0 and true will have value 1.
是有效的,右边的表达式的计算结果为 7,因为 false 的值为 0,true 的值为 1。

It is also possible to convert implicitly the data type integers or floating point values to bool type. For example,
也可以将数据类型的整数或浮点值隐式转换为 bool 类型。例如,

bool x = 0;  // false 
bool y = 100;  // true 
bool z = 15.75;  // true
//============================================================================
// Name        : std::map::bool
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>

using namespace std;

int main()
{
	int x1 = 10, x2 = 20, m = 2;
	bool b1, b2;

	b1 = x1 == x2; // false
	b2 = x1 < x2; // true

	cout << "b1 is = " << b1 << "\n";
	cout << "b2 is = " << b2 << "\n";

	bool b3 = true;

	if (b3)
	{
		cout << "Yes" << "\n";
	}
	else
	{
		cout << "No" << "\n";
	}

	int x3 = false + 5 * m - b3;

	cout << x3;

	return 0;

}

b1 is = 0
b2 is = 1
Yes
9

7. C++ Booleans

In programming, you will need a data type that can only have one of two values, like:
在编程中,您将需要一个只能具有两个值之一的数据类型,例如:

YES / NO
ON / OFF
TRUE / FALSE

For this, C++ has a bool data type, which can take the values true (1) or false (0).
为此,C++ 具有布尔数据类型,该数据类型可以采用值 true (1) or false (0)。

A boolean variable is declared with the bool keyword and can only take the values true or false:
布尔变量使用 bool 关键字声明,并且只能采用 truefalse 值:

bool isCodingFun = true;
bool isFishTasty = false;
cout << isCodingFun;  // Outputs 1 (true)
cout << isFishTasty;  // Outputs 0 (false)

From the example above, you can read that a true value returns 1, and false returns 0.
从上面的示例中,您可以看到 true 值返回 1,false 则返回 0。

However, it is more common to return boolean values from boolean expressions (see next page).
但是,更常见的是从布尔表达式返回布尔值 (请参阅下一页)。

8. bool (C++)

C++ 新增 bool 类型 (布尔类型),一般占用 1 个字节长度。bool 类型只有两个取值,true 和 false (true 表示真,false 表示假)。在写程序时进行初始化比较保险,避免出问题找不到 bug。

  • bool 类型有 2 个值 true or false,true 默认值是 1,false 的默认值是 0,可以用于数值运算。
  • bool 类型变量的值只能是 true 或 false,非 0 即 true,0 即 false。bool 类型变量赋值会被自动转换成 true or false,任何非零值都被转换为 true,而零被转换为 false。

C++ 是在 C 语言的基础上发展来的,但是并不是 C++ 比 C 语言高级,两者的编程思想不一样,应用的领域也不一样。在各自的领域,谁也不能替代谁。微软为 Visual C++ 提供了很多用于显示 Windows 界面的库函数。Visual C++ 就是 C++ 加上 Windows 图形界面。

This keyword is a built-in type. A variable of this type can have values true and false. Conditional expressions have the type bool and so have values of type bool. For example, i!=0 now has TRUE or FALSE depending on the value of i.
此关键字是内置类型。这种类型的变量可以具有 truefalse 值。条件表达式的类型为 bool,值的类型也为 bool。例如,根据 i 的值,i!=0 现在具有 TRUE 或FALSE。

Visual Studio 2017 version 15.3 and later (available with /std:c++17): The operand of a postfix or prefix increment or decrement operator may not be of type bool. In other words, given a variable b of type bool, these expressions are no longer allowed.
Visual Studio 2017 version 15.3 and later (available with /std:c++17):后缀或前缀递增或递减运算符的操作数可能不是 bool 类型。换句话说,给定布尔类型 b 的变量 b,将不再允许这些表达式:

    b++;
    ++b;
    b--;
    --b;

The values TRUE and FALSE have the following relationship:

!false == true
!true == false
if (condexpr1) statement1;

If condexpr1 is TRUE, statement1 is always executed; if condexpr1 is FALSE, statement1 is never executed.

When a postfix or prefix ++ operator is applied to a variable of type bool, the variable is set to TRUE. Visual Studio 2017 version 15.3 and later: operator++ for bool was removed from the language and is no longer supported.
将后缀或前缀 ++ 运算符应用于 bool 类型的变量时,该变量将设置为 TRUE。Visual Studio 2017 version 15.3 and later:bool 的 operator++ 已从该语言中删除,不再受支持。

The postfix or prefix -- operator cannot be applied to a variable of this type.
后缀或前缀 -- 运算符不能应用于此类型的变量。

The bool type participates in integral promotions. An r-value of type bool can be converted to an r-value of type int, with FALSE becoming zero and TRUE becoming one. As a distinct type, bool participates in overload resolution.
布尔型参与整型提升。bool 类型的 r-value 可以转换为 int 类型的 r-value,其中 FALSE 变为 0,TRUE 变为 1。作为一种独特的类型,布尔参与了重载解析。

References

https://en.cppreference.com/w/cpp/keyword/bool
https://en.cppreference.com/w/cpp/language/bool_literal
http://www.functionx.com/cpp/keywords/bool.htm

发布了454 篇原创文章 · 获赞 1733 · 访问量 103万+

猜你喜欢

转载自blog.csdn.net/chengyq116/article/details/104376936
今日推荐