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.3 list

标准库提供了一个名为list的双向链表:

如果希望在一个序列中添加、删除元素而无须移动其他元素,则应使用list。对电话簿应用而言,插入和删除操作可能很频繁,因此list可能适合保存电话簿。例如:

当我们使用一个链表时,通常并不是想要像使用向量那样使用它,即,不会用下标操作访问链表元素,而是想进行“在链表中搜索具有给定值的元素”这类操作。为了完成这样的操作,我们可以利用“list是序列”这样的事实(如第12章所述):

这段代码从链表头开始搜索s,直至找到s或到达phone_book的末尾。

我们有时需要在list中定位一个元素。例如,我们可能想删除这个元素或是在这个元素之前插入一个新元素。为此,我们需要使用迭代器(iterator):一个list迭代器指向list中的一个元素,可用来遍历(iterate through)list(因此得名)。每个标准库容器都提供begin()和end()函数,分别返回一个指向首元素的迭代器和一个指向尾后位置的迭代器(参见第12章)。我们可以改写函数get_number(),显式使用迭代器遍历list,这个版本显然不那么优雅: ...

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