1 / 60

Data Structures Using C++ 2E

Data Structures Using C++ 2E. Chapter 4 Standard Template Library (STL) I. Objectives. Learn about the Standard Template Library (STL) Become familiar with the three basic components of the STL: containers, iterators , and algorithms (in Chapter 13)

kaseem-rios
Download Presentation

Data Structures Using C++ 2E

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. Data Structures Using C++ 2E Chapter 4 Standard Template Library (STL) I

  2. Objectives • Learn about the Standard Template Library (STL) • Become familiar with the three basic components of the STL: containers, iterators, and algorithms (in Chapter 13) • Explore how vector and deque containers are used to manipulate data in a program • Discover the use of iterators Data Structures Using C++ 2E

  3. Components of the STL • Program’s main objective is to manipulate data and generate results • Requires ability to store data, access data, and manipulate data • STL components • Containers • Iterators: step through container elements • Algorithms: manipulate data (e.g. sorting, search) • Containers and iterators • Class templates (types as class arguments) Data Structures Using C++ 2E

  4. Container Types • STL containers categories • Sequence containers (sequential containers) • Associative containers • Container adapters Data Structures Using C++ 2E

  5. Container Types • STL containers categories • Sequence containers (sequential containers) • vector, deque, list… • Associative containers • set, map, multiset, multimap… • Container adapters: adaptation of sequence or associative class containers by modifying and restricting its interface for some special purpose. • stack (LIFO), queue (FIFO), priority_queue… Data Structures Using C++ 2E

  6. TABLE 13-6 Containers, their associated header files, and the type of iterator supported by each container Containers, Associated Header Files,and Iterator Support Data Structures Using C++ 2E

  7. STL containers From http://cs.brown.edu/~jak/proglang/cpp/stltut/tut.html

  8. STL: Sequence containers • Contiguous blocks of objects; elements can be inserted at any point in the sequence; sequences are indexed by integers • Vector • Fast insertion at end, and allow random access. • List • Fast insertion anywhere, but provide only sequential access. • Deque • Fast insertion at either end, and allow random access. Data Structures Using C++ 2E

  9. STL: Associative containers • Associative containers are a generalization of sequences; can be indexed by any type (e.g., an ID or a name) • Implemented with a data structure called binary search tree, which will be covered later in this course. Data Structures Using C++ 2E

  10. STL: Associated containers • Sets: add, delete, search, and iterate through the set. • Multisets: Multisets are just like sets, except that duplicate copies of the same element are allowed. • Maps: Maps represent a mapping from one type (the key type) to another type (the value type). You can associate a value with a key, or find the value associated with a key, very efficiently; you can also iterate through all the keys. • Multimaps: are just like maps except that there is no limit on elemenets with the same key. Data Structures Using C++ 2E

  11. Sequence Containers • Every object has a specific position • Predefined sequence containers • vector , deque , list • Sequence container vector • Logically: same as arrays • Processed like arrays • All containers • Use same names (e.g. push_back()) for common operations • May have specific operations Data Structures Using C++ 2E

  12. Arrays • Static arrays int foo[100]; • Dynamic arrays Data Structures Using C++ 2E

  13. Sequence Container: vector • Vector container • Stores, manages objects in a dynamic array • Elements accessed randomly • Time-consuming item insertion: middle, beginning • Fast item insertion: end • Class implementing vector container • vector • Header file containing the class vector • vector Data Structures Using C++ 2E

  14. Sequence Container: vector (cont’d.) • Using a vector container in a program requires the following statement: • #include <vector> • Defining a vector container object • Specify object type • Example: vector<int> intlist; • Example: vector<string> stringList; Data Structures Using C++ 2E

  15. TABLE 4-1 Various ways to declare and initialize a vector container Sequence Container: vector (cont’d.) • Declaring vector objects Data Structures Using C++ 2E

  16. Declare and initialize a vector Data Structures Using C++ 2E

  17. Sequence Container: vector (cont’d.) • Manipulating data stored in a vector sequence container • Basic operations • Item insertion • Item deletion • Stepping through the elements of a vector array Data Structures Using C++ 2E

  18. Sequence Container: vector (cont’d.) Data Structures Using C++ 2E

  19. Access a vector Data Structures Using C++ 2E

  20. Sequence Container: vector (cont’d.) • class vector • Provides various operations to process vector container elements • Iterator • The argument position in STL terminology Data Structures Using C++ 2E

  21. TABLE 4-3 Various operations on a vector container Sequence Container: vector (cont’d.) Data Structures Using C++ 2E

  22. TABLE 4-3 Various operations on a vector container (cont’d.) Sequence Container: vector (cont’d.) Data Structures Using C++ 2E

  23. Vector: size vs. capacity • Size: the number of elements you have used. • Capacity: allocated space. In STL, it’s doubled each time if additional space is needed. Data Structures Using C++ 2E

  24. Sequence Container: vector (cont’d.) • Function push_back • Adds element to end of container • Used when declaring vector container • Specific size unknown Data Structures Using C++ 2E

  25. Iterators • Iterators provide a way of specifying a position in a container. • The code vector<int> v; // add some integers to v vector::iterator i1 = v.begin(); vector::iterator i2 = v.end(); will create two iterators like this: Data Structures Using C++ 2E

  26. Declaring an Iterator to a Vector Container • Process vector container like an array • Using array subscripting operator • Process vector container elements • Using an iterator • class vector: function insert • Insert element at a specific vector container position • Uses an iterator • class vector: function erase • Remove element • Uses an iterator Data Structures Using C++ 2E

  27. Declaring an Iterator to a Vector Container (cont’d.) • class vector contains iterator (a typedef inside the class vector) • Declared as a public member • Vector container iterator • Declared using typedef iterator • Example vector<int>::iterator intVecIter; Data Structures Using C++ 2E

  28. Declaring an Iterator to a Vector Container (cont’d.) • Requirements for using typedef iterator • Container name (vector) • Container element type • Scope resolution operator • ++intVecIter • Advances iterator intVecIter to next element into the container • *intVecIter • Returns element at current iterator position Data Structures Using C++ 2E

  29. Declaring an Iterator to a Vector Container (cont’d.) • Using an iterator into a vector container • Manipulating element type to be int Data Structures Using C++ 2E

  30. Containers and the Functions begin and end • begin() • Returns position of the first element into the container • end() • Returns position of the last element into the container • Functions have no parameters • class vector • Contains member functions used to find number of elements currently in the container Data Structures Using C++ 2E

  31. Example 4-4 Data Structures Using C++ 2E

  32. TABLE 4-4 Functions to determine the size of a vector container Containers and the Functions begin and end (cont’d.) Data Structures Using C++ 2E

  33. Member Functions Common to All Containers • Examples • Default constructor • Several constructors with parameters • Destructor • insert(), erase(), begin(), end(), etc… • Class encapsulates data, operations on that data • Into a single unit • Every container is a class • Several operations directly defined for a container • Provided as part of class definition Data Structures Using C++ 2E

  34. TABLE 4-5 Member functions common to all containers Data Structures Using C++ 2E

  35. TABLE 4-6 Member functions common to all sequence containers Member Functions Common to Sequence Containers Data Structures Using C++ 2E

  36. The copy Algorithm • Provides convenient way to output container elements • Generic STL algorithm • Usable with any container type and arrays • Does more than output container elements • Allows copying of elements from one place to another • Function template copy definition • Contained in header file algorithm Data Structures Using C++ 2E

  37. copy examples

  38. Sequence Container: deque • Deque: double-ended queue • Implemented as dynamic arrays • Can expand in either direction • Class name defining deque container • deque • Header file deque contains • Definition of the class deque • Functions to implement various operations on a deque object • Class deque contains several constructors Data Structures Using C++ 2E

  39. TABLE 4-7 Various ways to declare a deque object Sequence Container: deque (cont’d.) Data Structures Using C++ 2E

  40. TABLE 4-8 Various operations that can be performed on a deque object Sequence Container: deque (cont’d.) Data Structures Using C++ 2E

  41. Iterators • Work like pointers • Point to elements of a container (sequence or associative) • Allow successive access to each container element • Two most common operations on iterators • ++ (increment operator) • * (dereferencing operator) • Examples ++cntItr; *cntItr; Data Structures Using C++ 2E

  42. Types of Iterators • Input iterators • Output iterators • Forward iterators • Bidirectional iterators • Random access iterators Data Structures Using C++ 2E

  43. Operations supported by the iterators write access; step forward *outputIter = t; ++outputIter; Read access; step forward int t = *inputIter; ++inputIter; Both read/write; step forward Both read/write; step forward and backward. Supported in list, set, map, multiset, multimap Both read/write; jump forward and backward. rIterator = rIterator + n; Supported in vector deque, string, array

  44. Input Iterators • Read access • Step forward element-by-element • Return values element-by-element • Provided for reading data from an input stream Data Structures Using C++ 2E

  45. TABLE 4-9 Operations on an input iterator Input Iterators (cont’d.) Data Structures Using C++ 2E

  46. Output Iterators • Write access • Step forward element-by-element • Used for writing data to an output stream • Cannot be used to iterate over a range twice Data Structures Using C++ 2E

  47. TABLE 4-10 Operations on an output iterator Output Iterators (cont’d.) Data Structures Using C++ 2E

  48. Forward Iterators • Combination of • All of input iterators functionality and almost all output iterators functionality • Can refer to same element in same collection • Can process same element more than once Data Structures Using C++ 2E

  49. TABLE 4-11 Operations on a forward iterator Forward Iterators (cont’d.) Data Structures Using C++ 2E

More Related