310 likes | 572 Views
Chapter 19 STL Containers. §19.1 STL Basics §19.2 STL Iterators §19.3 Sequence Containers §19.4 Associative Containers §19.5 Container Adapters. §19.1 STL Basics. Standard Template Library ( STL ) 标准模板库 A software library included in the C++ Standard Library Purpose
E N D
Chapter 19 STL Containers §19.1 STL Basics §19.2 STL Iterators §19.3 Sequence Containers §19.4 Associative Containers §19.5 Container Adapters
§19.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 1980s • Accepted as part of C++ standard in Feb. 1994
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.
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 优先队列
Simple Demo Listing 19.1 gives a simple example that demonstrates how to create a vector, list, deque, set, multiset, stack, and queue. SimpleSTLDemo
§19.2 STL Iterators • Iterators are objects to facilitate traversing through the elements in a container • A iterator is like a built-inpointer that can manipulate the elements in a container IteratorDemo Pointers themselves are iterators. The array pointers can be treated as iterators.
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
Predefined Iterators • Iterators have been defined in every containers • Using “typedef” • The typedefs of iterators • iterator • const_iterator • reverse_iterator • const_reverse_iterator typedefint integer; integer value = 40; ConstIteratorDemo ReverseIteratorDemo
Iterator Operators • Using overloaded operators to manipulate the an iterator • Move its position • Access the element • Compare with other iterators
Iterator Operator Demo vector<int> 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<int>::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; IteratorOperatorDemo
I/O Steam Iterators • Iterators can also be used to manipulate I/O streams • input iterator and output iterator InputOutputStreamIteratorDemo
§19.3 Sequence Containers • Represent linear data structures
Sequence Container: vector VectorDemo An insert call may invalidate previously obtained iterators!
Sequence Container: deque DequeDemo An insert call may invalidate previously obtained iterators!
Sequence Container: list An insert call won’t change the previously obtained iterators! ListDemo
§19.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
Associative Containers: set and multiset • Mathematical set to store simple elements • set and multiset are identical except that • multiset allows duplicate keys SetDemo
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 MapDemo
§19.5 Container Adapters • Containers adapted from the sequence containers • For handling special cases • Programmer can choose an appropriate sequence container for a container adapter *: default one
Container Adapter: stack StackDemo
Container Adapter: queue QueueDemo
Container Adapter: priority_queue PriorityQueueDemo
A Summary • Concepts • STL, Container, Iterator, Algorithm • Three types of containers • Features of different containers • Syntax of declaring container objects • Main functions of containers