Skip to Content
C++程序设计语言(第4部分:标准库)(原书第4版)
book

C++程序设计语言(第4部分:标准库)(原书第4版)

by 本贾尼 斯特劳斯特鲁普
August 2021
Intermediate to advanced
450 pages
6h 57m
Chinese
Pearson
Content preview from C++程序设计语言(第4部分:标准库)(原书第4版)

34.2 “拟容器”

标准库中有一些容器不能很好地纳入STL框架(见31.4节、33.2节和33.1节),例如内置数组、array和string。我有时将它们称为“拟容器”(见31.4节),但这并不是很公平:它们保存元素,因此就是容器,只不过都有一些限制或包含额外组件,因而放在STL语境中显得有些尴尬。分开介绍这些容器也简化了STL的介绍:

为什么标准库会提供这么多容器呢?这是为了满足很多常见但又有差异(通常也有重叠)的需求。如果标准库不提供这些容器,很多人将不得不自己实现。例如:

·pair和tuple是异构的;所有其他容器都是同构的(元素都是相同类型)。

·array、vector和tuple连续保存元素;forward_list和map是链接结构。

·bitset和vector保存二进制位,通过代理对象访问这些二进制位;所有其他标准库容器都可以保存不同类型并直接访问元素。

·basic_string要求其元素为某种字符类型,它提供了字符串操作,如连接操作和区域敏感操作(见第39章),valarray要求其元素为数值类型,并提供数值运算。

所有这些容器都可视为提供了大规模程序员社区所需的特殊功能。没有任何单一容器能满足所有需求,因为有些需求是冲突的,例如,“增长的能力”与“保证在固定位置分配”,以及“添加元素不会导致其他元素移动”与“空间连续分配”。此外,非常通用的容器可能意味着不可接受的额外开销。

34.2.1 array

array定义在<array>中,是固定大小的给定类型的元素的序列,元素数目在编译时指定。因此,array连同其元素可以在栈中、对象内或静态存储中分配空间。array在哪个作用域中定义,元素就会在其中分配。理解array的最好方式是将其视为固定大小的内置数组,但不会隐式地、出乎意料地转换为指针类型,且提供了一些便利的函数。与内置数组相比,使用array不会带来(时间或空间上的)额外开销。array不遵循STL的“元素句柄”容器模型,而是直接包含其元素: ...

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

算法精讲视频课程:24篇系列讲座

算法精讲视频课程:24篇系列讲座

罗伯特·塞奇威克, 凯文·韦恩(Kevin Wayne)
可编程网络自动化

可编程网络自动化

Jason Edelman, Scott S. Lowe, Matt Oswalt
C语言核心技术(原书第2版)

C语言核心技术(原书第2版)

Peter Prinz, Tony Crawford
Go程序设计语言

Go程序设计语言

艾伦A. A.多诺万, 布莱恩W. 柯尼汉

Publisher Resources

ISBN: 9787111544395