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 特殊容器

标准库还提供几种不能完美契合STL框架(参见第11章、第12章)的容器,如内置数组、array和string。我有时候会将这些容器称为“拟容器”,但这么说有点儿不太公平:它们的确保存元素,所以就是容器,只不过它们各自都有一些约束或增加了一些额外特性因而在STL的语境中显得有些异类。因此我选择单独介绍这些容器,也简化了STL的介绍。

标准库为什么要提供这么多容器呢?因为它们能满足各种常见且各不相同(时常会有交叠)的需求。如果标准库没有提供这些容器,很多人就不得不自己设计并实现它们。例如:

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

·array、vector和tuple的元素是连续分配的;而forward_list和map是链接的结构。

·bitset和vector存放的是位,通过代理对象来访问;其他所有标准库容器可存放任意类型的元素,并且可以直接访问元素。

·basic_string要求它的元素是某种类型的字符并且提供很多字符串操作,比如连接操作和区域敏感操作等。

·valarray要求它的元素是数值并且提供数值操作。

所有这些容器都可看作为庞大的程序员群体的需求提供专门服务。没有任何单一容器能满足所有需求,因为有些需求本身就是矛盾的。例如,“可扩充性”和“确保分配在固定位置”是矛盾的;“添加元素时不移动元素”和“连续分配”也是矛盾的。

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