Skip to Content
C++程序设计:原理与实践(基础篇)(原书第2版)
book

C++程序设计:原理与实践(基础篇)(原书第2版)

by 本贾尼 斯特劳斯特鲁普
August 2021
Intermediate to advanced
450 pages
9h 36m
Chinese
Pearson
Content preview from C++程序设计:原理与实践(基础篇)(原书第2版)

4.3.2 运算符

到目前为止,我们用的都是最简单的运算符,接下来你将会看到许多复杂运算符的使用方法。今后我们将在遇到运算符时加以详细描述,大多数运算符都很好理解。下表给出了常用的运算符:

上表中的lval表示左值,即它可以出现在赋值号左边,在附录A.5中有详细介绍。

逻辑运算符&&,||和!的例子可以分别在5.5.1节、7.7节、7.8.2节和10.4节中找到。

需要注意的是,表达式a<b<c表示(a<b)<c,a<b的结果是布尔值:true或false。因此,表达式a<b<c的值等于true<c或者false<c,而不是a<b<c表示“b的值是否介于a和c之间?”实际上,表达式a<b<c是没有用处的,在进行比较操作时,千万不要写出这样的表达式。如果在别人的代码中发现了这种表达式,这往往意味着一个错误。

增量表达式至少有三种形式:

哪种方式比较好?为什么呢?建议使用第一种方式++a,它直观地表示了增量的含义,显示了我们要做什么(对a加1),而不是怎么做(a加1,然后结果写到a)。通常,我们认为能够更直接地体现程序思想的编程方式更好一些,因为这种方式更准确,并且更容易被读者理解。假如使用a=a+1的话,读者可能会想,程序的原意真的是要对a加1吗?不会是要做a=b+1、a=a+2或者a=a-1但输入出错了吧!而使用++a方式就不会引起这样的疑问。需要注意的是,上述只讨论程序的正确性和逻辑性,与程序的效率无关。实际上,目前的编译器对a=a+1和++a的处理是一样的。同样,我们建议编程时使用a*=scale而不是a=a*scale。 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

C++语言导学(原书第2版)

C++语言导学(原书第2版)

本贾尼 斯特劳斯特鲁普
管理Kubernetes

管理Kubernetes

Brendan Burns, Craig Tracey
Python编程入门与实战

Python编程入门与实战

Posts & Telecom Press, Fabrizio Romano

Publisher Resources

ISBN: 9787111562252