1 / 95

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

C++ Programming: Program Design Including Data Structures, Fifth Edition. Chapter 22: Standard Template Library (STL). Objectives. In this chapter, you will: Learn about the Standard Template Library (STL)

viho
Download Presentation

C++ Programming: Program Design Including Data Structures, Fifth 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 IncludingData Structures, Fifth 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, Fifth 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 • Shows how to use its tools C++ Programming: Program Design Including Data Structures, Fifth 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, Fifth Edition

  5. Container Types • Manage objects of a given type • Three categories: • Sequence (sequential) containers • Associative containers • Container adapters C++ Programming: Program Design Including Data Structures, Fifth Edition

  6. Sequence Containers • Every object has a specific position • Three predefined sequence containers: • vector • deque • list C++ Programming: Program Design Including Data Structures, Fifth Edition

  7. Sequence Container: vector • Stores and manages its objects in a dynamic array • Must have: #include <vector> • To define an object of type vector, specify the type of the object • Examples: vector<int> intList; vector<string> stringList; • vector contains several constructors C++ Programming: Program Design Including Data Structures, Fifth Edition

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

  9. Sequence Container: vector (cont'd.) C++ Programming: Program Design Including Data Structures, Fifth Edition

  10. Sequence Container: vector (cont'd.) C++ Programming: Program Design Including Data Structures, Fifth Edition

  11. Sequence Container: vector (cont'd.) C++ Programming: Program Design Including Data Structures, Fifth Edition

  12. Declaring an Iterator to a Vector Container • A vector contains a typedefiterator • For example, the statement vector<int>::iteratorintVecIter; declares intVecIter to be an iterator into a vector container of type int • ++intVecIter • Advances the iterator • *intVecIter • Returns element at current iterator position C++ Programming: Program Design Including Data Structures, Fifth Edition

  13. Containers and the 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, Fifth Edition

  14. Member Functions Common to All Containers C++ Programming: Program Design Including Data Structures, Fifth Edition

  15. Member Functions Common to All Containers (cont'd.) C++ Programming: Program Design Including Data Structures, Fifth Edition

  16. Member Functions Common to All Containers (cont'd.) C++ Programming: Program Design Including Data Structures, Fifth Edition

  17. Member Functions Common to Sequence Containers C++ Programming: Program Design Including Data Structures, Fifth Edition

  18. Member Functions Common to Sequence Containers (cont'd.) C++ Programming: Program Design Including Data Structures, Fifth Edition

  19. The copy Algorithm • Function copy: convenient way to output the elements of a container • Copies elements from one place to another • Can output the elements of a vector • Prototype: copies elements within range first1...last-1 • Must have: #include <algorithm> C++ Programming: Program Design Including Data Structures, Fifth Edition

  20. The copy Algorithm (cont'd.) • Example: int intArray[] = {5, 6, 8, 3, 40, 36, 98, 29, 75}; vector<int> vecList(9); copy(intArray, intArray + 9, vecList.begin()); • After the previous statement executes: vecList = {5, 6, 8, 3, 40, 36, 98, 29, 75} C++ Programming: Program Design Including Data Structures, Fifth Edition

  21. The copy Algorithm (cont'd.) • Consider the statement : copy(intArray + 1, intArray + 9, intArray); • After the previous statement executes: intArray = {6, 8, 3, 40, 36, 98, 29, 75, 75} • Now, consider the statement: copy(vecList.rbegin() + 2, vecList.rend(), vecList.rbegin()); • After the previous statement executes: vecList = {5, 6, 5, 6, 8, 3, 40, 36, 98} C++ Programming: Program Design Including Data Structures, Fifth Edition

  22. 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 destination • When creating an iterator of the type ostream: • Specify the type of element that the iterator will output C++ Programming: Program Design Including Data Structures, Fifth Edition

  23. The ostream Iterator and the Function copy (cont'd.) • Example: ostream_iterator<int> screen(cout, " "); copy(intArray, intArray + 9, screen); copy(vecList.begin(), vecList.end(), screen); • The last statement is equivalent to: copy(vecList.begin(), vecList.end(), ostream_iterator<int>(cout, " ")); • Another example: copy(vecList.begin(), vecList.end(), ostream_iterator<int>(cout, ", ")); C++ Programming: Program Design Including Data Structures, Fifth Edition

  24. 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, Fifth Edition

  25. Sequence Container: deque(cont’d.) C++ Programming: Program Design Including Data Structures, Fifth Edition

  26. Sequence Container: deque(cont’d.) C++ Programming: Program Design Including Data Structures, Fifth Edition

  27. 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, Fifth Edition

  28. Sequence Container: list (cont'd.) C++ Programming: Program Design Including Data Structures, Fifth Edition

  29. Sequence Container: list (cont'd.) C++ Programming: Program Design Including Data Structures, Fifth Edition

  30. Sequence Container: list (cont'd.) C++ Programming: Program Design Including Data Structures, Fifth Edition

  31. Sequence Container: list (cont'd.) C++ Programming: Program Design Including Data Structures, Fifth Edition

  32. Sequence Container: list (cont'd.) C++ Programming: Program Design Including Data Structures, Fifth Edition

  33. Sequence Container: list (cont'd.) C++ Programming: Program Design Including Data Structures, Fifth Edition

  34. Iterators • An iterator points to the elements of a container (sequence or associative) • Iterators provide access to each element • Most common operations on iterators • ++ (increment) • * (dereference) C++ Programming: Program Design Including Data Structures, Fifth Edition

  35. Types of Iterators • Input iterators: have read access; step forward element-by-element • Output iterators: have write access; step forward element-by-element • Forward iterators: have all functionality of input and almost all of output iterators • Bidirectional iterators: can go backward • Random access iterators: bidirectional iterators that can randomly process the elements of a container C++ Programming: Program Design Including Data Structures, Fifth Edition

  36. Input Iterators C++ Programming: Program Design Including Data Structures, Fifth Edition

  37. Output Iterators • Output iterators cannot be used to iterate over a range twice • If we write data at same position, there is no guarantee that new value will replace old one C++ Programming: Program Design Including Data Structures, Fifth Edition

  38. Forward Iterators C++ Programming: Program Design Including Data Structures, Fifth Edition

  39. Bidirectional Iterators • 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, Fifth Edition

  40. Random Access Iterators • 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, Fifth Edition

  41. Types of Iterators (cont'd.) C++ Programming: Program Design Including Data Structures, Fifth Edition

  42. 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, Fifth Edition

  43. 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 typedefconst_iterator to handle these situations C++ Programming: Program Design Including Data Structures, Fifth Edition

  44. typedef reverse_iterator • Every container also contains the typedef reverse_iterator • An iterator of this type is used to iterate through the elements of a container in reverse C++ Programming: Program Design Including Data Structures, Fifth Edition

  45. typedef const_reverse_iterator • Read-only iterator • Used to iterate through the elements of a container in reverse • Required if: • The container is declared as const • Need to iterate through the elements of the container in reverse C++ Programming: Program Design Including Data Structures, Fifth Edition

  46. Other typedefs Common to AllContainers C++ Programming: Program Design Including Data Structures, Fifth Edition

  47. Stream Iterators • istream_iterator • Used to input data into a program from an input stream • ostream_iterator • Used to output data into an output stream C++ Programming: Program Design Including Data Structures, Fifth Edition

  48. Associative Containers • Elements in an associative container are automatically sorted according to some ordering criteria • Predefined associative containers in the STL: • Sets • Multisets • Maps • Multimaps C++ Programming: Program Design Including Data Structures, Fifth Edition

  49. 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) • Ascending order • Must #include <set> C++ Programming: Program Design Including Data Structures, Fifth Edition

  50. Declaring set or multiset Associative Containers C++ Programming: Program Design Including Data Structures, Fifth Edition

More Related