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版)

11.6 容器概述

标准库提供了一些最通用也最有用的容器类型,使得程序员能够根据应用需求选择最适合的容器。

无序容器针对关键字(通常是一个字符串)搜索进行了优化,这是通过使用哈希表来实现的。

容器都定义在名字空间std中,通过<vector>、<list>、<map>等头文件提供(参见8.3节)。此外,标准库还提供了容器适配器queue<T>,stack<T>和priority_queue<T>。如果你需要使用这些特性,请查阅相关资料。标准库还提供了一些更特殊化的类容器的类型,如定长数组array<T,N>(参见13.4.1节)和bitset<N>(参见13.4.2节)。

从符号表示角度,不同标准库容器及其基本操作的设计是相似的。而且,同名操作对不同容器的含义是相同的。只要有意义且能高效实现,基本操作就会应用于每种容器:

这种符号表示和语义上的一致性使得程序员可以设计出与标准库容器在使用方式上非常相似的新的容器类型,范围检查向量Vector(参见3.5.2节和第4章)就是一个例子。容器接口的一致性还使得我们可以设计与容器类型个体无关的算法。但是,凡事都有两面性,有优点就会有缺点。例如,下标操作和遍历vector的操作很高效也很简单。但另一方面,当我们在vector中插入或删除元素时,就需要移动元素,效率不佳,而list则恰好具有相反的特性。请注意,当序列较短,元素大小较小时,vector通常比list更为高效(即便是insert()和erase()操作也是如此)。我推荐将标准库vector作为存储元素序列的默认类型:你除非有充分的理由,否则不要选择其他容器。 ...

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