Skip to Content
C++语言导学(原书第2版)
book

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

by 本贾尼 斯特劳斯特鲁普
August 2021
Intermediate to advanced
450 pages
5h 35m
Chinese
Pearson
Content preview from C++语言导学(原书第2版)

13.4.2 bitset

系统的很多属性(例如输入数据流的状态)都可以表示为一组表示二元状态的标记,像

好/坏、真/假、开/关等。C++通过整数上的位运算(参见1.4节)为这样一种小规模标记的概念提供了高效支持。类bitset<N>提供了N位序列[0:N)上的操作,从而泛化了这一概念,其中N在编译时就已知。对于无法放入一个long long int中的位集合,使用bitset比直接使用整数方便得多。即使对于较小的位集合,bitset通常也进行了优化。如果你为每一位命名而不是对它们编号,则应该使用set(参见11.4节)或者枚举(参见2.5节)。

我们可以用整数或者字符串来初始化bitset:

常用位运算符(参见1.4节)以及左移和右移运算符(<<和>>)都能用在bitset上:

其中,移位操作(此处是<<)会“移进”0。

函数to_ullong()和to_string()提供了与构造函数相反的操作。例如,我们可以输出一个int值的二进制表示:

这段代码将二进制表示的1和0从左至右地打印出来,最高位在最左边,则实参123的输出结果是: ...

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

软件开发实践:项目驱动式的Java开发指南

软件开发实践:项目驱动式的Java开发指南

Raoul-Gabriel Urma, Richard Warburton
管理Kubernetes

管理Kubernetes

Brendan Burns, Craig Tracey
Python机器学习案例精解

Python机器学习案例精解

Posts & Telecom Press, Yuxi (Hayden) Liu

Publisher Resources

ISBN: 9787111633280