Download
c programming program design including data structures third edition n.
Skip this Video
Loading SlideShow in 5 Seconds..
C++ Programming: Program Design Including Data Structures, Third Edition PowerPoint Presentation
Download Presentation
C++ Programming: Program Design Including Data Structures, Third Edition

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

149 Views Download Presentation
Download Presentation

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

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. C++ Programming: Program Design Including Data Structures, Third 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

  3. Introduction (continued) • 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

  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

  5. Container Types • Containers are used to manage objects of a given type • Three categories: • Sequence (sequential) containers • Associative containers • Container adapters

  6. Sequence Containers • Every object has a specific position • Three predefined sequence containers: • vector • deque • list

  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> • To define an object of type vector, we must specify the type of the object because the class vector is a class template. For example, the statement vector<int> intList;declares intList to be a vector and the component type to be int. • The statement vector<string> stringList; declares stringList to be a vector container and the component type to be string.

  8. The classvector contains several constructors, including the default constructor

  9. Sequence Container: Vector (continued) • Basic vector operations • Item insertion • Item deletion • Stepping through the elements

  10. Vector elements can be accessed using the operations below:

  11. The class vector also contains member functions that can be used to find the number of elements currently in the container, the maximum number of elements that can be inserted into a container, and so on.

  12. The classvector also contains member functions that can be used to manipulate the data, as well as insert and delete items, in a vector container.

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

  14. The expression ++intVecIter advances the iterator intVecIter to the next element in the container • The expression *intVecIter returns the element at the current iterator position.

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

  16. The statement in Line 1 declares intList to be a vector container and the element type is int. The statement in Line 2 declares intVecIter to be an iterator in a vector container whose element type is int. • After the following statement executes: intVecIter = intList.begin(); the iterator intVecIter points to the first element in the container intList. • The following for loop outputs the elements of intList to the standard output device: for (intVecIter = intList.begin(); intVecIter != intList.end(); ++intVecList) cout << *intVecList << " ";

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

  18. The prototype of the function templatecopy is • template <class inputIterator, class outputIterator> • outputItr copy(inputIterator first1, • inputIterator last, • outputIterator first2); • The parameter first1 specifies the position from which to begin copying the elements; the parameter last specifies the end position. The parameter first2 specifies where to copy the elements. Therefore, the parameters first1 and last specify the source; parameter first2 specifies the destination. • Note that the elements within the range first1...last-1 are copied. • The definition of the function templatecopy is contained in the header file algorithm. Thus, to use the function copy, the program must include the statement • #include <algorithm>

  19. The function copy works as follows. Consider the following statement: int intArray[] = {5, 6, 8, 3, 40, 36, 98, 29, 75}; This statement creates an array intArray of nine components. The statement: vector<int> vecList(9); creates an empty container of nine components of type vector and the element type int. Now consider the statement copy(intArray, intArray + 9, vecList.begin()); This statement copies the elements starting at the location intArray until intArray + 9 - 1 into the container vecList. After the previous statement executes, vecList = {5, 6, 8, 3, 40, 36, 98, 29, 75}

  20. Consider the statement copy(intArray + 1, intArray + 9, intArray); Here first1 is intArray + 1 and last is intArray + 9. Also, first2 is intArray. After the preceding statement executes, intArray = {6, 8, 3, 40, 36, 98, 29, 75, 75} Clearly, the elements of the array intArray are shifted to the left by one position.

  21. Now consider the statement copy(vecList.rbegin() + 2, vecList.rend(), vecList.rbegin()); Recall that the function rbegin (reverse begin) returns a pointer to the last element into a container; it is used to process the elements of a container in reverse. Therefore, vecList.rbegin() + 2 returns a pointer to the third-to-last element into the container vecList. Similarly, the function rend (reverse end) returns a pointer to the first element into a container. The previous statement shifts the elements of the container vecList to the right by two positions. After the previous statement executes, the container vecList is vecList = {5, 6, 5, 6, 8, 3, 40, 36, 98}

  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 the destination • When you create an iterator of the type ostream, specify the type of element that the iterator will output

  23. ostream_iterator<int> screen(cout, " "); • This statement creates screen to be an ostream iterator with the element type int. The iterator screen has two arguments: the object cout and a space. • The iterator screen is initialized using the object cout. When this iterator outputs elements, they are separated by a space. • The statement copy(intArray, intArray + 9, screen); outputs the elements of intArray on the screen. • The statement copy(vecList.begin(), vecList.end(), screen); outputs the elements of the container vecList on the screen.

  24. The statement copy(vecList.begin(), vecList.end(), screen); is equivalent to the statement copy(vecList.begin(), vecList.end(), ostream_iterator<int>(cout, " ")); • The statement copy(vecList.begin(), vecList.end(), stream_iterator<int>(cout, ", ")); outputs the elements of vecList with a comma and space between them.

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

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

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

  28. Types of Iterators • Five types of iterators: • Input iterators • Output iterators • Forward iterators • Bidirectional iterators • Random access iterators

  29. Input Iterators • Input iterators, with read access, step forward element-by-element; consequently, they return the values element-by-element. • These iterators are provided for reading data from an input stream.

  30. Output Iterators • Output iterators, with write access, step forward element-by-element • Output iterators are provided for writing data to an output stream

  31. Forward Iterators • Forward iterators combine all of the functionality of input iterators and almost all of the functionality of output iterators