1 / 30

Chapter 19 STL Containers

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

avari
Download Presentation

Chapter 19 STL Containers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 19 STL Containers §19.1 STL Basics §19.2 STL Iterators §19.3 Sequence Containers §19.4 Associative Containers §19.5 Container Adapters

  2. §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

  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.

  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 优先队列

  5. Container Classes

  6. Common Functions to All Containers

  7. Common Functions to Sequence/Associative Containers

  8. Simple Demo Listing 19.1 gives a simple example that demonstrates how to create a vector, list, deque, set, multiset, stack, and queue. SimpleSTLDemo

  9. §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.

  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

  11. Iterator Types Supported by Containers

  12. 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

  13. Iterator Operators • Using overloaded operators to manipulate the an iterator • Move its position • Access the element • Compare with other iterators

  14. 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

  15. I/O Steam Iterators • Iterators can also be used to manipulate I/O streams • input iterator and output iterator InputOutputStreamIteratorDemo

  16. §19.3 Sequence Containers • Represent linear data structures

  17. Common Functions in Sequence Container

  18. Sequence Container: vector VectorDemo An insert call may invalidate previously obtained iterators!

  19. Sequence Container: deque DequeDemo An insert call may invalidate previously obtained iterators!

  20. Sequence Container: list An insert call won’t change the previously obtained iterators! ListDemo

  21. §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

  22. Common Functions in Associative Containers

  23. Associative Containers: set and multiset • Mathematical set to store simple elements • set and multiset are identical except that • multiset allows duplicate keys SetDemo

  24. 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

  25. §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

  26. Container Adapter: stack StackDemo

  27. Container Adapter: queue QueueDemo

  28. Container Adapter: priority_queue PriorityQueueDemo

  29. A Summary • Concepts • STL, Container, Iterator, Algorithm • Three types of containers • Features of different containers • Syntax of declaring container objects • Main functions of containers

More Related