1 / 69

C++ Programming: Program Design Including Data Structures, Second Edition

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)

Download Presentation

C++ Programming: Program Design Including Data Structures, Second Edition

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. C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)

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

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

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

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

  6. Sequence Containers • Every object has a specific position • Three predefined sequence containers: • Vector • Deque • List C++ Programming: Program Design Including Data Structures, Second Edition

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

  8. Sequence Container: vector (continued) • Basic vector operations • Item insertion • Item deletion • Stepping through the elements C++ Programming: Program Design Including Data Structures, Second Edition

  9. Sequence Container: vector (continued) • vector elements can be accessed using the operations below: C++ Programming: Program Design Including Data Structures, Second Edition

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

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

  12. Operations of the vector Class • Some vector member functions: C++ Programming: Program Design Including Data Structures, Second Edition

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  30. Stack • The STL provides a stack class C++ Programming: Program Design Including Data Structures, Second Edition

  31. Queue • The STL provides a queue class C++ Programming: Program Design Including Data Structures, Second Edition

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

  33. Nonmodifying Algorithms • Nonmodifying algorithms do not modify the elements of the container C++ Programming: Program Design Including Data Structures, Second Edition

  34. Modifying Algorithms C++ Programming: Program Design Including Data Structures, Second Edition

More Related