690 likes | 775 Views
C++ Programming: Program Design Including Data Structures, Second Edition. Chapter 22: Standard Template Library (STL). Objectives. In this chapter you will: Learn about the Standard Template Library (STL)
E N D
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
Objectives In this chapter you will: • Learn about the Standard Template Library (STL) • Become familiar with the basic components of the STL: containers, iterators, and algorithms • Explore how various containers are used to manipulate data in a program • Discover the use of iterators • Learn about various generic algorithms C++ Programming: Program Design Including Data Structures, Second Edition
Introduction • ANSI/ISO Standard C++ is equipped with a Standard Template Library (STL) • The STL provides class templates to process lists, stacks, and queues • This chapter discusses many important features of the STL and shows how to use its tools C++ Programming: Program Design Including Data Structures, Second Edition
Components of the STL • Components of the STL: • Containers • Iterators • Algorithms • Containers and iterators are class templates • Iterators are used to step through the elements of a container • Algorithms are used to manipulate data C++ Programming: Program Design Including Data Structures, Second Edition
Container Types • Containers are used to manage objects of a given type • Three categories: • Sequence (sequential) containers • Associative containers • Container adapters C++ Programming: Program Design Including Data Structures, Second Edition
Sequence Containers • Every object has a specific position • Three predefined sequence containers: • Vector • Deque • List C++ Programming: Program Design Including Data Structures, Second Edition
Sequence Container: Vector • A vector container stores and manages its objects in a dynamic array • To use a vector container in a program, the program must #include <vector> • The class vector contains several constructors, including the default constructor C++ Programming: Program Design Including Data Structures, Second Edition
Sequence Container: vector (continued) • Basic vector operations • Item insertion • Item deletion • Stepping through the elements C++ Programming: Program Design Including Data Structures, Second Edition
Sequence Container: vector (continued) • vector elements can be accessed using the operations below: C++ Programming: Program Design Including Data Structures, Second Edition
Declaring an Iterator to a Vector Container • vector contains a typedef iterator • For example, the statement vector<int>::iterator intVecIter; declares intVecIter to be an iterator into a vector container of the type int C++ Programming: Program Design Including Data Structures, Second Edition
Container and Functions begin and end • Every container contains the member function begin and end • begin returns the position of the first element • end returns the position of the last element • Both functions have no parameters C++ Programming: Program Design Including Data Structures, Second Edition
Operations of the vector Class • Some vector member functions: C++ Programming: Program Design Including Data Structures, Second Edition
The copy Algorithm • Function copy: convenient way to output the elements of a container • Can be used with any container type • Allows you to copy the elements from one place to another • Can output the elements of a vector • Can copy the elements of one vector into another C++ Programming: Program Design Including Data Structures, Second Edition
The ostream Iterator and the Function copy • One way to output the contents of a container is to use a for loop, along with begin (initialize) and end (loop limit) • copy can output a container; an iterator of the type ostream specifies the destination • When you create an iterator of the type ostream, specify the type of element that the iterator will output C++ Programming: Program Design Including Data Structures, Second Edition
Sequence Container: deque • deque stands for double ended queue • Implemented as dynamic arrays • Elements can be inserted at both ends • A deque can expand in either direction • Elements are also inserted in the middle C++ Programming: Program Design Including Data Structures, Second Edition
Sequence Container: list • Lists are implemented as doubly linked lists • Every element in a list points to both its immediate predecessor and its immediate successor (except the first and last element) • The list is not a random access data structure C++ Programming: Program Design Including Data Structures, Second Edition
Iterators • An iterator points to the elements of a container (sequence or associative) • Iterators provide access to each element • The most common operations on iterators are ++ (increment), and * (dereference) C++ Programming: Program Design Including Data Structures, Second Edition
Types of Iterators • Five types of iterators: • Input iterators • Output iterators • Forward iterators • Bidirectional iterators • Random access iterators C++ Programming: Program Design Including Data Structures, Second Edition
Input Iterators • Input iterators, with read access, step forward element-by-element return the values element-by-element • Input iterators are provided for reading data from an input stream C++ Programming: Program Design Including Data Structures, Second Edition
Output Iterators • Output iterators, with write access, step forward element-by-element • Output iterators are provided for writing data to an output stream C++ Programming: Program Design Including Data Structures, Second Edition
Forward Iterators • Forward iterators combine all of the functionality of input iterators and almost all of the functionality of output iterators C++ Programming: Program Design Including Data Structures, Second Edition
Bidirectional Iterators • Bidirectional iterators are forward iterators that can also iterate backward over the elements • The operations defined for forward iterators apply to bidirectional iterators • Use the decrement operator to step backward C++ Programming: Program Design Including Data Structures, Second Edition
Random Access Iterators • Random access iterators are bidirectional iterators that can randomly process the elements of a container • Can be used with containers of the types vector, deque, string, as well as arrays • Operations defined for bidirectional iterators apply to random access iterators C++ Programming: Program Design Including Data Structures, Second Edition
typedef iterator • Every container contains a typedef iterator • The statement vector<int>::iterator intVecIter; declares intVecIter to be an iterator into a vector container of the type int C++ Programming: Program Design Including Data Structures, Second Edition
typedef const_iterator • With the help of an iterator into a container and the dereference operator, *, you can modify the elements of the container • If the container is declared const, then we must prevent the iterator from modifying the elements • Every container contains typedef const_iterator to handle these situations C++ Programming: Program Design Including Data Structures, Second Edition
Stream Iterators • istream_iterator • Used to input data into a program from an input stream • ostream_iterator • Used to output data from a program into an output stream C++ Programming: Program Design Including Data Structures, Second Edition
Associative Containers • Elements in associative container are automatically sorted according to some ordering criteria • The predefined associative containers in the STL are: • Sets • Multisets • Maps • Multimaps C++ Programming: Program Design Including Data Structures, Second Edition
Associative Containers: set and multiset • Associative containers set and multiset automatically sort their elements • multiset allows duplicates, set does not • The default sorting criterion is the relational operator <(less than); that is, the elements are arranged in ascending order C++ Programming: Program Design Including Data Structures, Second Edition
Container Adapters • The STL provides containers to accommodate special situations called container adapters • The three container adapters are: • Stacks • Queues • Priority Queues • Container adapters do not support any type of iterator C++ Programming: Program Design Including Data Structures, Second Edition
Stack • The STL provides a stack class C++ Programming: Program Design Including Data Structures, Second Edition
Queue • The STL provides a queue class C++ Programming: Program Design Including Data Structures, Second Edition
Algorithms • Operations such as find, sort, and merge are common to all containers and are provided as generic algorithms • STL algorithms can be classified as follows • Nonmodifying algorithms • Modifying algorithms • Numeric algorithms • Heap algorithms C++ Programming: Program Design Including Data Structures, Second Edition
Nonmodifying Algorithms • Nonmodifying algorithms do not modify the elements of the container C++ Programming: Program Design Including Data Structures, Second Edition
Modifying Algorithms C++ Programming: Program Design Including Data Structures, Second Edition