1 / 104

CSC 262 Programming in C ++ II Sykes Day 18

CSC 262 Programming in C ++ II Sykes Day 18. 22.1   Introduction to the Standard Template Library (STL). We’ve repeatedly emphasized the importance of software reuse.

may
Download Presentation

CSC 262 Programming in C ++ II Sykes Day 18

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. CSC 262Programming in C++ IISykesDay 18

  2. 22.1  Introduction to the Standard Template Library (STL) • We’ve repeatedly emphasized the importance of software reuse. • Recognizing that many data structures and algorithms are commonly used, the C++ standard committee added the Standard Template Library (STL) to the C++ Standard Library. • The STL defines powerful, template-based, reusable components that implement many common data structures and algorithms used to process those data structures.

  3. 22.1  Introduction to the Standard Template Library (STL) (Cont.) • As you’ll see, the STL was conceived and designed for performance and flexibility. • This chapter introduces the STL and discusses its three key components—containers (popular templatized data structures), iterators and algorithms. • The STL containers are data structures capable of storing objects of almost any data type (there are some restrictions). • We’ll see that there are three styles of container classes—first-class containers, adapters and near containers.

  4. 22.1  Introduction to the Standard Template Library (STL) (Cont.) • STL iterators, which have properties similar to those of pointers, are used by programs to manipulate the STL-container elements. • In fact, standard arrays can be manipulated by STL algorithms, using standard pointers as iterators. • We’ll see that manipulating containers with iterators is convenient and provides tremendous expressive power when combined with STL algorithms—in some cases, reducing many lines of code to a single statement. • There are five categories of iterators, each of which we discuss in Section 22.1.2 and use throughout this chapter.

  5. 22.1  Introduction to the Standard Template Library (STL) (Cont.) • STL algorithms are functions that perform such common data manipulations as searching, sorting and comparing elements (or entire containers). • The STL provides approximately 70 algorithms. • Most of them use iterators to access container elements. • Each algorithm has minimum requirements for the types of iterators that can be used with it. • We’ll see that each first-class container supports specific iterator types, some more powerful than others. • A container’s supported iterator type determines whether the container can be used with a specific algorithm.

  6. 22.1  Introduction to the Standard Template Library (STL) (Cont.) • Iterators encapsulate the mechanism used to access container elements. • This encapsulation enables many of the STL algorithms to be applied to several containers without regard for the underlying container implementation. • As long as a container’s iterators support the minimum requirements of the algorithm, then the algorithm can process that container’s elements. • This also enables you to create new algorithms that can process the elements of multiple container types.

  7. 22.1  Introduction to the Standard Template Library (STL) (Cont.) • In Chapter 20, we studied data structures. • We built linked lists, queues, stacks and trees. • We carefully wove link objects together with pointers. • Pointer-based code is complex, and the slightest omission or oversight can lead to serious memory-access violations and memory-leak errors with no compiler complaints. • Implementing additional data structures, such as deques, priority queues, sets and maps, requires substantial extra work. • An advantage of the STL is that you can reuse the STL containers, iterators and algorithms to implement common data representations and manipulations.

  8. Iterators • Recall: generalization of a pointer • Typically even implemented with pointer! • "Abstraction" of iterators • Designed to hide details of implementation • Provide uniform interface across differentcontainer classes • Each container class has "own" iterator type • Similar to how each data type has ownpointer type

  9. Manipulating Iterators • Recall using overloaded operators: • ++, --, ==, != • * • So if p is iterator variable, *p gives access to datapointed to by p • Vector template class • Has all above overloads • Also has members begin() and end()c.begin(); //Returns iterator for 1st item in cc.end(); //Returns "test" value for end

  10. Cycling with Iterators • Recall cycling ability:for (p=c.begin();p!=c.end();p++) process *p //*p is current data item • Big picture so far… • Keep in mind: • Each container type in STL has own iterator types • Even though they’re all used similarly

  11. Display 19.1Iterators Used with a Vector (1 of 2) 1 //Program to demonstrate STL iterators. 2 #include <iostream> 3 #include <vector> 4 using std::cout; 5 using std::endl; 6 using std::vector; 7 int main( ) 8 { 9 vector<int> container; 10 for (int i = 1; i <= 4; i++) 11 container.push_back(i); 12 cout << "Here is what is in the container:\n"; 13 vector<int>::iterator p; 14 for (p = container.begin( ); p != container.end( ); p++) 15 cout << *p << " "; 16 cout << endl; 17 cout << "Setting entries to 0:\n"; 18 for (p = container.begin( ); p != container.end( ); p++) 19 *p = 0;

  12. Display 19.1Iterators Used with a Vector (2 of 2) 20 cout << "Container now contains:\n"; 21 for (p = container.begin( ); p != container.end( ); p++) 22 cout << *p << " "; 23 cout << endl; 24 return 0; 25 } Sample Dialogue Here is what is in the container: 1 2 3 4 Setting entries to 0: Container now contains: 0 000

  13. Vector Iterator Types • Iterators for vectors of ints are of type: std::vector<int>::iterator • Iterators for lists of ints are of type: std::list<int>::iterator • Vector is in std namespace, so need:using std::vector<int>::iterator;

  14. Kinds of Iterators • Different containers  different iterators • Vector iterators • Most "general" form • All operations work with vector iterators • Vector container great for iterator examples

  15. Random Access: Display 19.2 Bidirectional and Random-Access Iterator Use

  16. Iterator Classifications • Forward iterators: • ++ works on iterator • Bidirectional iterators: • Both ++ and – work on iterator(“--“) • Random-access iterators: • ++, --, and random access all work with iterator • These are "kinds" of iterators, not types!

  17. Constant and Mutable Iterators • Dereferencing operator’s behavior dictates • Constant iterator: • * produces read-only version of element • Can use *p to assign to variable or output,but cannot change element in container • E.g., *p = <anything>; is illegal • Mutable iterator: • *p can be assigned value • Changes corresponding element in container • i.e.: *p returns an lvalue

  18. Reverse Iterators • To cycle elements in reverse order • Requires container with bidirectional iterators • Might consider:iterator p;for (p=container.end();p!=container.begin(); p--) cout << *p << " " ; • But recall: end() is just "sentinel", begin() not! • Might work on some systems, but not most

  19. Reverse Iterators Correct • To correctly cycle elements in reverseorder:reverse_iterator p;for (rp=container.rbegin();rp!=container.rend(); rp++) cout << *rp << " " ; • rbegin() • Returns iterator at last element • rend() • Returns sentinel "end" marker

  20. Compiler Problems • Some compilers problematic with iteratordeclarations • Consider our usage:using std::vector<char>::iterator;…iterator p; • Alternatively:std::vector<char>::iterator p; • And others… • Try various forms if compiler problematic

  21. 22.1.2 Introduction to Iterators (Cont.) • STL first-class containers provide member functions begin and end. • Function begin returns an iterator pointing to the first element of the container. • Function end returns an iterator pointing to the first element past the end of the container (an element that doesn’t exist).

  22. 22.1.2 Introduction to Iterators (Cont.) • If iterator i points to a particular element, then ++i points to the “next” element and *i refers to the element pointed to by i. • The iterator resulting from end is typically used in an equality or inequality comparison to determine whether the “moving iterator” (i in this case) has reached the end of the container. • An object of type iterator refers to a container element that can be modified. • An object of type const_iterator refers to a container element that cannot be modified.

  23. 22.1.2 Introduction to Iterators (Cont.) • Figure 22.9 shows the predefined iterator typedefs that are found in the class definitions of the STL containers. • Not every typedef is defined for every container. • We use const versions of the iterators for traversing read-only containers. • We use reverse iterators to traverse containers in the reverse direction.

  24. 22.1.2 Introduction to Iterators (Cont.) • Figure 22.10 shows some operations that can be performed on each iterator type. • The operations for each iterator type include all operations preceding that type in the figure.

  25. 22.1.2 Introduction to Iterators (Cont.) • Figure 22.6 shows the categories of STL iterators. • Each category provides a specific set of functionality. • Figure 22.7 illustrates the hierarchy of iterator categories. • As you follow the hierarchy from top to bottom, each iterator category supports all the functionality of the categories above it in the figure. • Thus the “weakest” iterator types are at the top and the most powerful one is at the bottom. • Note that this is not an inheritance hierarchy.

  26. Containers • Container classes in STL • Different kinds of data structures • Like lists, queues, stacks • Each is template class with parameter for particular data type to be stored • e.g., Lists of ints, doubles or myClass types • Each has own iterators • One might have bidirectional, another might just have forward iterators • But all operators and members have same meaning

  27. 22.1.1 Introduction to Containers • The STL container types are shown in Fig. 22.1. • The containers are divided into three major categories—sequence containers, associative containers and container adapters.

  28. 22.1.1 Introduction to Containers (Cont.) • The sequence containers represent linear data structures, such as vectors and linked lists. • Associative containers are nonlinear containers that typically can locate elements stored in the containers quickly. • Such containers can store sets of values or key/value pairs. • The sequence containers and associative containers are collectively referred to as the first-class containers. • As we saw in Chapter 20, stacks and queues actually are constrained versions of sequential containers. • For this reason, STL implements stacks and queues as container adapters that enable a program to view a sequential container in a constrained manner.

  29. 22.1.1 Introduction to Containers (Cont.) • There are other container types that are considered “near containers”—C-like pointer-based arrays (discussed in Chapter 7), bitsets for maintaining sets of flag values and val-arrays for performing high-speed mathematical vector operations (this last class is optimized for computation performance and is not as flexible as the first-class containers). • These types are considered “near containers” because they exhibit capabilities similar to those of the first-class containers, but do not support all the first-class-container capabilities. • Type string (discussed in Chapter 18) supports the same functionality as a sequence container, but stores only character data.

  30. 22.1.2 Introduction to Iterators (Cont.) • The iterator category that each container supports determines whether that container can be used with specific algorithms in the STL. • Containers that support random-access iterators can be used with all algorithms in the STL. • As we’ll see, pointers into arrays can be used in place of iterators in most STL algorithms, including those that require random-access iterators. • Figure 22.8 shows the iterator category of each of the STL containers. • The first-class containers (vectors, deques, lists, sets, multisets, maps and multimaps), strings and arrays are all traversable with iterators.

  31. 22.1.1 Introduction to Containers (Cont.) • Most STL containers provide similar functionality. • Many generic operations, such as member function size, apply to all containers, and other operations apply to subsets of similar containers. • This encourages extensibility of the STL with new classes. • Figure 22.2 describes the functions common to all Standard Library containers. • [Note: Overloaded operators operator<, operator<=, operator>, operator>=, operator== and operator!= are not provided for priority_queues.]

More Related