js布尔类型+数字判断

In this article, we’ll take a look at the Bool datatype in C++.

在本文中,我们将介绍C ++中的Bool数据类型。

The bool data type is one that is there in C++ for quite sometime. This serves as a convenient and easy datatype for programmers to manage and write conditional statements using a boolean value, rather than an int.

布尔数据类型是相当一段时间以来在C ++中存在的一种。 这对于程序员使用布尔值而不是int来管理和编写条件语句而言,是一种便捷的数据类型。

However, remember that essentially, both bool and int are the same, with some small differences.

但是,请记住, boolint本质上是相同的,只是有一些小差异。

Let’s take a look at using this datatype, using a few examples!

让我们使用一些示例来看看如何使用此数据类型!



C ++中的bool数据类型 (The bool Datatype in C++)

We can assign a variable of this datatype to only two values:

我们可以将此数据类型的变量仅分配给两个值:

  • true -> Equivalent to the integer value 1true- >等于整数值1
  • false -> Equivalent to the integer value 0false- >等于整数值0

The following way shows how the variables are assigned.

以下方式显示了变量的分配方式。


bool var_1 = true; // Set var_1 to true
bool var_2 = false; // Set var_2 to false

Interestingly, if you try to print these variables using std::cout, you’ll only get their integer values.

有趣的是,如果尝试使用std::cout打印这些变量,则只会得到它们的整数值。


#include <iostream>int main() {bool var_1 = true;bool var_2 = false;std::cout << "var_1: " << var_1 << std::endl;std::cout << "var_2: " << var_2 << std::endl;return 0;
}

Output

输出量


var_1: 1
var_2: 0


在C ++中使用布尔数据类型 (Using bool data type in C++)

The most common use of the bool datatype is for conditional statements. We can compare conditions with a boolean, and also return them.

bool数据类型的最常见用途是用于条件语句。 我们可以将条件与布尔值进行比较,并返回它们。

The below snippet shows the comparison of a condition with a bool value.

下面的代码段显示了条件与bool值的比较。


#include <iostream>int main() {int x = 10, y = 20;if ((x > y) == true)// The condition evaluates to truestd::cout << "x > y\n";else// The condition evaluates to falsestd::cout << "x <= y\n";return 0;
}

Output

输出量


x <= y

We can also return conditional statements since they are also a bool. We can use this to directly return them in functions

我们也可以返回条件语句,因为它们也是一个bool 。 我们可以使用它直接将它们返回给函数

For example, if you declare a boolean function to check if the first argument is greater than the second, you can directly return a conditional statement (x > y).

例如,如果声明一个布尔函数以检查第一个参数是否大于第二个参数,则可以直接返回条件语句(x> y)。


bool check_greater(int x, int y) {return (x > y);
}

This will return true if x > y, and false otherwise.

如果x> y,则返回true ,否则返回false



转换为int / float / double (Conversion to int/float/double)

Since the default value of true is 1 (or 1.00), we can also convert them into an int/ float/ double by type casting.

由于true的默认值为1(或1.00),因此我们也可以通过类型转换将它们转换为int / float / double

Sometimes, even the compiler implicitly converts a bool to an int/ float, and vice-versa. So this is very convenient now!

有时,即使是编译器也将bool隐式转换为int / float ,反之亦然。 因此,这现在非常方便!

The below expression is valid in C++:

以下表达式在C ++中有效:


int x = 10;
float y = 2.5;
float z = x + y + true + false;

Here, since all the types can be converted to the left-hand side type float, z will be assigned a value of: 10 + 2.5 + 1 + 0 = 13.5

在这里,由于所有类型都可以转换为左侧类型float ,因此z的值将为:10 + 2.5 + 1 + 0 = 13.5

We can also do the reverse if the datatype of the LHS is bool.

如果LHS的数据类型为bool我们也可以相反。


int x = 10;
float y = 2.5;
bool z = x + y + true + false;

Here, z will be assigned true, since (bool)10 + (bool)2.5 + true + false = true + true + true + false = true!

在这里, z将被指定为true ,因为( bool )10 +( bool )2.5 + true + false = true + true + true + false = true



使用布尔作为模板参数 (Using bool as a template argument)

Since bool is a valid data type, we can pass it as a template argument too.

由于bool是有效的数据类型,因此我们也可以将其作为模板参数传递。

For example, we can pass it as an argument to std::vector<T>, to construct a vector of bools!

例如,我们可以将其作为参数传递给std::vector<T> ,以构造bool的向量 !


#include <iostream>
#include <vector>int main() {std::vector<bool> vec = {true, false, true, false};for (auto i: vec)std::cout << i << std::endl;return 0;
}

Output

输出量


1
0
1
0


结论 (Conclusion)

In this article, we learned about the bool data type in C++, and how we can use it in different ways, to make our lives easier!

在本文中,我们了解了C ++中的bool数据类型,以及如何以不同的方式使用它,以使我们的生活更轻松!



参考资料 (References)

  • cppreference.com section on Bool data type有关布尔数据类型的cppreference.com部分


翻译自: https://www.journaldev.com/37791/bool-datatype-in-c-plus-plus

js布尔类型+数字判断

js布尔类型+数字判断_C ++中的布尔数据类型相关推荐

  1. strcmp返回值布尔类型的判断(分析常见错误)

    strcmp返回值布尔类型的判断 strcmp: 用于比较两个字符串,原型如下: int strcmp ( char const *s1, char const *s2): 如果s1小于s2,strc ...

  2. 【strcmp】strcmp返回值布尔类型的判断

    strcmp: 用于比较两个字符串,原型如下: int strcmp ( char const *s1, char const *s2): 如果s1小于s2,strcmp函数返回一个小于零的值.如果s ...

  3. strcmp返回值布尔类型的判断

    strcmp: 用于比较两个字符串,原型如下: int strcmp ( char const *s1, char const *s2): 如果s1小于s2,strcmp函数返回一个小于零的值.如果s ...

  4. 布尔类型的值包括( )和( )_布尔类型

    2.4.3布尔类型 布尔类型主要用来表示真值或假值.在Python中,标识符True和False被解释为布尔值.另外,Python中的布尔值可以转化为数值,True 表示1, False 表示0. 说 ...

  5. C++基础之布尔类型,什么是C++的布尔类型

    在C语言中,关系运算和逻辑运算的结果有两种,真和假:0 表示假,非 0 表示真.例如: #include <stdio.h>int main(){int a, b, flag;scanf( ...

  6. java 基本类型的引用_Java中的基本数据类型与引用数据类型

    一.基本数据类型 byte.short.int.long(整数类型) float.double(浮点数类型) char(字符型) boolean(布尔类型 ) Java数据大多数存放在堆栈中.栈区:存 ...

  7. javascript 布尔_JavaScript布尔说明-如何在JavaScript中使用布尔

    javascript 布尔 布尔型 (Boolean) Booleans are a primitive datatype commonly used in computer programming ...

  8. 布尔逻辑_了解Go中的布尔逻辑

    布尔逻辑 The Boolean data type (bool) can be one of two values, either true or false. Booleans are used ...

  9. java布尔类型定义_Java如何正确定义布尔类型变量的命名

    布尔类型变量命名success VS isSuccess success & isSuccess 皆可,命名都不存在歧义性,但是建议不加is 理由: 1.防止部分框架解析会引起序列化错误 反例 ...

最新文章

  1. 你知道吗?Workspot属于二级VDI平台?
  2. C#去掉字符串中的汉字
  3. [原创] debian 9.3 搭建Jira+Confluence+Bitbucket项目管理工具(四) -- 安装bitbucket 5.7.0
  4. 矩阵乘法 算法训练 试题_蓝桥杯习题集_ 算法训练 矩阵乘法
  5. [转载]We Recommend a Singular Value Decomposition
  6. JNI----Native本地方法接口
  7. icewm使用心得[转]
  8. 可运行的c语言程序的扩展名为什么?
  9. ASP.NET AJAX Debugging and Tracing
  10. ai系统架构_人工智能中的模糊逻辑系统架构
  11. 关于resolve非泛型方法不能与类型实参一起使用
  12. html5 扩展属性,HTML5属性的介绍和扩展.doc
  13. Conversion Operators in OpenCascade
  14. ICCV 2019 中国论文数量超美国,商汤57篇论文入选!
  15. jQuery选择器整理+知识总结
  16. 使用Pytorch的LSTM文本分类
  17. OperationException: CLIENT: CLIENT_ERROR cannot increment or decrement non-numeric value
  18. esp32开发快速入门 8 : MQTT 的快速入门,基于esp32实现MQTT通信
  19. 如果长颈鹿哭了,它会不会要哽咽好久
  20. 2021-07-03 DTS 驱动 rock1126 添加硬件ucam 驱动

热门文章

  1. ajax data参数
  2. AE “每用户订阅上的所有人SID 不存在”
  3. php输出带尖括号的内容
  4. (转载)mysql书籍
  5. [转载] 使用Python+OpenCV实现在视频中某对象后添加图像
  6. [转载] 深度测评Python的3种“字符串格式化”方法,看看你喜欢哪一种?
  7. [转载] Python 主成分分析PCA
  8. 设计模式PHP篇(三)————适配器模式
  9. 允许远程访问MySQL的设置
  10. [转] python提取计算结果的最大最小值及其坐标