Download presentation
Presentation is loading. Please wait.
Published byLeo Hubbard Modified over 9 years ago
1
Chapter 22 STL Containers §22.1 STL Basics §22.2 STL Iterators §22.3 Sequence Containers §22.4 Associative Containers §22.5 Container Adapters
2
§22.1 STL Basics Standard Template Library (STL) 标准模板库 A software library included in the C++ Standard Library Purpose To standardize commonly used components History Originally developed by Alexander Stepanov in 1980sAlexander Stepanov Accepted as part of C++ standard in Feb. 1994 2
3
Three Components of STL Containers (容器) A container object, such as vector, used to store a collection of data, often referred to as elements Iterators (迭代器) Objects that facilitate traversing through the elements in a container Like built-in pointers that provide a convenient way to access and manipulate the elements in a container Algorithms (算法) Functions to manipulate data such as sorting, searching, and comparing elements. Most of them use iterators to access the elements in the container. 3
4
Three Types of Containers Sequence/sequential containers, 序列容器 Represent linear data structures Three sequence containers vector 向量, list 表, and deque 双端队列 Associative containers, 关联容器 / 联合容器 Non-linear containers that can locate elements stored in the container quickly Four associative containers set 集合, multiset 多重集合, map 映射, and multimap 多重映射 Container adapters ,容器适配器 Constrained versions of sequence containers stack 栈, queue 队列, and priority_queue 优先队列 4
5
Container Classes 5
6
Common Functions to All Containers 6
7
Common Functions to Sequence/Associative Containers 7
8
Simple Demo Listing 22.1 gives a simple example that demonstrates how to create a vector, list, deque, set, multiset, stack, and queue. 8 SimpleSTLDemo
9
§22.2 STL Iterators Iterators are objects to facilitate traversing through the elements in a container A iterator is like a built-in pointer that can manipulate the elements in a container 9 IteratorDemo Pointers themselves are iterators. The array pointers can be treated as iterators.
10
Type of Iterators Different containers may have different types of iterators Five types: Input (Output) iterator For reading/writing from/to container Moving only in forward direction Forward iterator Combination of input/output iterator Bidirectional iterator A forward iterator that can move backward Random access iterator A bidirectional iterator that can jump 10
11
Iterator Types Supported by Containers 11
12
Predefined Iterators Iterators have been defined in every containers Using “typedef” The typedefs of iterators iterator const_iterator reverse_iterator const_reverse_iterator 12 typedef int integer; integer value = 40; ReverseIteratorDemo ConstIteratorDemo
13
Iterator Operators Using overloaded operators to manipulate the an iterator Move its position Access the element Compare with other iterators 13
14
14
15
Iterator Operator Demo 15 IteratorOperatorDemo vector intVector; intVector.push_back(10); intVector.push_back(20); intVector.push_back(30); intVector.push_back(40); intVector.push_back(50); intVector.push_back(60); vector ::iterator p1 = intVector.begin(); for (; p1 != intVector.end(); p1++){ cout << *p1 << " "; } cout << endl << *(--p1) << endl; cout << *(p1 - 3) << endl; cout << p1[-3] << endl; *p1 = 1234; cout << *p1 << endl;
16
I/O Steam Iterators Iterators can also be used to manipulate I/O streams input iterator and output iterator 16 InputOutputStreamIteratorDemo
17
§22.3 Sequence Containers Represent linear data structures 17 Header file Strong pointsWeak pointsImpl. struct. vector Random access Inserting and deleting at the end Inserting/Deleting in the middle or front array deque Random access Inserting and deleting at the front and end Inserting/Deleting in the middle array list Inserting and deleting anywhere Random accesslinkedlist
18
Common Functions in Sequence Container 18
19
Sequence Container: vector 19 VectorDemo An insert call may invalidate previously obtained iterators!
20
Sequence Container: deque 20 DequeDemo An insert call may invalidate previously obtained iterators!
21
Sequence Container: list 21 ListDemo An insert call won’t change the previously obtained iterators!
22
§22.4 Associative Containers Represent non-linear data structures Elements in an associative container are sorted according to some sorting criterion (“<” by default) For fast storage and quick retrieval using keys 22 Header file Strong pointsWeak pointsImpl. struct. set mutlset For fast storage and quick retrieval using keys Inserting/DeletingBST map multimap For fast storage and quick retrieval using keys Inserting/DeletingBST
23
Common Functions in Associative Containers 23
24
Associative Containers: set and multiset Mathematical set to store simple elements set and multiset are identical except that multiset allows duplicate keys 24 SetDemo
25
Associative Containers: map and multimap Storage of mapping from one data item (a key) to another (a value). map and multimap are identical except that multimap allows duplicate keys 25 MapDemo
26
§22.5 Container Adapters Containers adapted from the sequence containers For handling special cases Programmer can choose an appropriate sequence container for a container adapter 26 Header file FeaturesImpl. struct. stack Last-In-First-Outdeque*, list, vector queue First-In-First-Outdeque*, list priority_queue Largest-In-First-Outvector*, deque *: default one
27
Container Adapter: stack 27 StackDemo
28
Container Adapter: queue 28 QueueDemo
29
Container Adapter: priority_queue 29 PriorityQueueDemo
30
A Summary Concepts STL, Container, Iterator, Algorithm Three types of containers Features of different containers Syntax of declaring container objects Main functions of containers 30
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.