《Beginning C++20 From Novice to Professional》第四章 Making Decisions

It means altering the sequence of execution depending on the result of a comparison

书上对做决策解释得挺清楚的,指的是根据比较的结果更改执行顺序的这个过程;这也是将计算机与计算器区别开的一个重要特征,计算机能够做决策

不过根本还是在人的代码写得怎么样

Comparing Data Values 比较数据值大小

主要有大小比较和相等比较;注意==与=区分

用于比较的变量有一个专用类型是bool,只有true和false两种类型,这俩词也是关键字

bool isValid {true}; // Define and initialize a logical variable to true
bool correct {}; // Define and initialize a logical variable to false

{}空初始化应用0初始化,0对应false这点也很好理解

Applying the Comparison Operators 

给了一段示例代码,注意上一章提到<>优先级是比<<>>低的,使用流输出的时候注意加括号

还有输出的时候我们并没有看到true和false这两个字面量,而是输出了0和1

From the output you can see that the value true is displayed as 1, and the value false is displayed as 0. These are the default representations for true and false.

书里也解释道,true的默认表示就是1,我们也可以使用整数给bool赋值

当我们想要看到字面量的时候,需要更改流的状态,使用std::boolalpha

std::cout << std::boolalpha;

std::format() outputs true and false for Booleans by default, and is free from operator precedence issues.

 所以流输出的形式被很多人诟病,不直观而且有运算符优先级的问题,我们再使用format看一下

Comparing Floating-Point Values 浮点数比较

The comparison operators are all of lower precedence than the arithmetic operators

比较运算符的优先级是比算术运算符低的,很多时候不需要加括号,但是避免歧义还是要加

浮点数大多数情况下结果是符合预期的,有一个例外就是溢出的时候

One peculiarity about floating-point comparisons is that not-a-number values are neither less than, greater than, nor equal to any other number. 

NAN不是一个数,不可以和任何数进行比较和相等判断

The Spaceship Operator 太空飞船?运算符

<=>这个玩意一般叫做三元比较运算符吧,spaceship可能是不正式的名字

这个运算符可能写起来判断逻辑比较简单,但是结果的类型是需要额外记忆的,处理逻辑就长一点了

上面的代码换成二元比较就是这样的↑

Default comparisons (since C++20) - cppreference.com

这个运算符目前看来最大的作用就是在复杂对象的比较时我们可以让编译器生成很多比较运算符,可以看上面这个网页的示例代码

并且由于这个运算符引入了强弱排序、强弱相等的概念

记住他的结果是枚举类型

Comparison Categories 比较的类型

比较本身也是有类型的,这个体系是C++20引入的,分为强排序、偏排序和弱排序

The type it evaluates to depends on the type of its operands.

这个运算符的结果和他的操作数有关

Default comparisons (since C++20) - cppreference.com

对应上面书上的图,当整数和指针比较属于强排序,浮点数属于偏排序,自定义操作属于弱排序

Named Comparison Functions 命名的比较函数

Strong_ordering values implicitly convert to either weak_ or partial_ordering, and Weak_ordering values implicitly convert to partial_ordering.

These conversions have the obvious results.

Partial_ordering values, however, cannot be converted to either of the other types

用命名函数注意结果的类型,强弱都可以转换为偏排序,而偏排序不可以转换 

The if Statement

if语句不想讲了,是个编程语言都讲烂了,可以参考《Primer》文章

第五章第五章 语句-CSDN博客第五章

Character Classification and Conversion

这些是C语言就有的对字符归类的一些函数,都在<cctype>里

Standard library header <cctype> - cppreference.com

这里面的函数不多,还有两个转换大小写的函数

书上也提到字符处理都是基于locale设置的,不同的地区大小写、字符集都不同

这点不在此处细讲

The if-else Statement

不多说,看《Primer》文章

第五章 语句-CSDN博客

Logical Operators 逻辑运算符

这里就不讲具体语法了,只需要知道C/C++里有短路原则

  • 逻辑与第一个条件为假则第二个条件不会进行判断
  • 逻辑或第一个条件为真则第二个条件不会进行判断

举个使用的例子

# include <numbers>
# include <iostream>
# include <format>
# include <cmath>
using namespace std;

int main() {
    int age{};     // Age of the prospective borrower
    int income{};  // Income of the prospective borrower
    int balance{}; // Current bank balance
    // Get the basic data for assessing the loan
    std::cout << "Please enter your age in years: ";
    std::cin >> age;
    std::cout << "Please enter your annual income in dollars: ";
    std::cin >> income;
    std::cout << "What is your current account balance in dollars: ";
    std::cin >> balance;
    // We only lend to people who are at least 21 years of age,
    // who make over $25,000 per year,
    // or have over $100,000 in their account, or both.
    if (age >= 21 && (income > 25'000 || balance > 100'000)) {
        // OK, you are good for the loan - but how much?
        // This will be the lesser of twice income and half balance
        int loan{}; // Stores maximum loan amount
        if (2 * income < balance / 2) { loan = 2 * income; }
        else { loan = balance / 2; }
        std::cout << "\nYou can borrow up to $" << loan << std::endl;
    }
    else // No loan for you...
    {
        std::cout << "\nUnfortunately, you don't qualify for a loan." << std::endl;
    }
}

The Conditional Operator 三元条件运算符

这也没什么好讲的

Statement Blocks and Variable Scope

这里想强调的一点是,C++很重视作用域的问题,如果是block中才会用到的变量,就不要声明到块外污染名字空间

基于这个原则,if语句中也引入了初始化的位置:

switch、while都是类似的,开头都可以进行临时变量的定义

EXERCISES

4-1

判断两个整数是否相等的小程序

4-2

不接受非正数;然后判断是否一个数是另外一个数的倍数

4-3

使用嵌套if判断数字是否在1-100的范围里,以及和50的大小关系

剩下的题大家可以看书做了,有点琐碎意义不大

  • 29
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

+xiaowenhao+

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值