c training datascope lawrence d antonio n.
Skip this Video
Loading SlideShow in 5 Seconds..
C++ Training Datascope Lawrence D’Antonio PowerPoint Presentation
Download Presentation
C++ Training Datascope Lawrence D’Antonio

C++ Training Datascope Lawrence D’Antonio

73 Views Download Presentation
Download Presentation

C++ Training Datascope Lawrence D’Antonio

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

  1. C++ TrainingDatascopeLawrence D’Antonio Lecture 10 An Overview of C++: The Standard Template Library

  2. Why Should You Learn STL? • STL is generic. It involves data structures and algorithms that are designed to work on as wide a range of data types as possible. • STL is useful. It provides a variety of basic tools that are easy to learn and to use. STL has a well-designed architecture that provides a variety of algorithms that can manipulate a number of data structures.

  3. Why Should You Learn STL? • STL is efficient. STL algorithms come with performance guarantees. For example, insertion into a map data structure is guaranteed to be at most logarithmic. • STL is extensible.The components of STL may be put together to create powerful generic libraries. For example, there exists an MTL (Matrix Template Library), a GTL (Graph Template Library), and Regex++, a regular expression library based on STL.

  4. What is STL? • The Standard Template Library (STL) is a powerful collection of generic data structures and algorithms that is part of the standard library in C++. The algorithms in STL are orthogonal to the data structures, in the sense that algorithms are designed to work without having to know details of the data structures to which they are being applied.

  5. What is STL? 2 • In order for this process to work, data containers are required to define the concept of an iterator (i.e., objects that define an addressing mechanism for the data structure). These iterators are used in turn by the STL algorithms to access the data structure.

  6. What is STL? 3 • Different algorithms require different types of access. • For example, the generic sorting algorithms in the STL require random access. This restricts the data structures that sorting algorithms can be used with to ones that provide random access (in the standard library this includes arrays, strings, vectors, and deques).

  7. What is STL? 4 • Alexander Stepanov, the primary developer of the STL, identifies four principles that underlie the design of STL. • Generic programming • Abstractness without loss of efficiency • Von Neumann computational model • Value semantics

  8. Generic programming • Generic programming can be thought of as writing software components that can be used for as wide a range of types as possible. • Stepanov states that generic programming attempts to find the most abstract form of an efficient algorithm.

  9. Generic programming 2 • The central abstraction in STL is that of the concept. A concept is a set of requirements for data types. • For example, a Sequence is a concept describing data containers that store elements in a linearly ordered range and allow for insertion or deletion at any point in that range. • Any data structure fitting these requirements is a model for the Sequence concept. STL defines three sequence types; vectors, lists, and deques.

  10. Generic programming 3 • Concepts can be classified and organized in hierarchies. For example, one of the primary concepts of the STL is that of the Iterator. Iteration allows for movement from one address location in a data structure to another. • There are various categories of Iterators.

  11. Generic programming 4 • A ForwardIterator only allows movement in one direction. There are two Iterator concepts that the ForwardIterator refines, the InputIterator and the OutputIterator. • The InputIterator can only read from the current location and allows movement forward. The OutputIterator can only write to the current location and allows movement forward.

  12. Generic programming 5 • The algorithms that form the STL are themselves organized around concepts. • A generic algorithm consists of an implementation and the set of requirements that its arguments must satisfy. • For example, the STL algorithm reverse() will reverse the elements of any container that is a model of the Sequence concept.

  13. Generic programming 6 • Here is a non-generic gcd function. int gcd(int x, int y) { while (y != 0) { int t = x % y; x = y; y = t; } return x < 0 ? -x : x; }

  14. Generic programming 7 • This algorithm is fine as it goes, but a generic version should be valid for other algebraic quantities besides integers (in fact, for any principal ideal domain). For example, we would want to extend the algorithm to polynomials and Gaussian integers.

  15. Generic programming 8 • One problem arises here, the return statement, which is assuming that the gcd returns a positive value, will only be valid for types that satisfy the requirement of being totally ordered.

  16. Generic programming 9 • Here is a generic gcd function. template <class T> T gcd(T x, T y) { while (y != 0) { T t = x % y; x = y; y = t; } return x; }

  17. Generic programming 10 • In this case, the 'greatest' divisor that we are returning is only unique up to multiplication by a unit. Each type will have its own concept of greatest (greatest degree for polynomials, greatest modulus for Gaussian integers).

  18. Generic programming 11 • To summarize (and perhaps oversimplify). • object-oriented programming = data abstraction + object types + type inheritance • generic programming = data abstraction + generic types + concept refinement

  19. Abstraction • STL makes efficiency an important part of the design of the library. • Complexity guarantees are part of interface of each STL component. • For example, there are three sequence containers, vector<Type>, list<Type>, deque<Type>. • The C++ standard gives the following time guarantees for element access, element insert/delete, and range insert/delete.

  20. Abstraction 2

  21. Abstraction 3 • STL also provides associative containers that, in general, store elements of the form (key, value), in some sort of tree structure (usually a red-black tree). There are four types of associative containers: • Sets (which are collections of unique keys without corresponding values). • Multisets (which allow for multiple occurrences of a key). • Maps (which store key, value pairs with keys being unique). • Multisets (which store key, value pairs with multiple keys possible).

  22. Abstraction 4 • The basic operations for associative containers are, finding an element with a given key, inserting an element, erasing an element, erasing all elements pointed to by a key. • In the following table, N represents the number of elements in the container, K represents the number of values corresponding to a particular key.

  23. Abstraction 5

  24. Von Neumann Model • Stepanov notes the significance of addressing in the design of STL. • The STL containers provide services for adding or deleting addresses to a data structure. Iterators can be seen as moving from one address to another in a container.

  25. Value Semantics • In the STL containers own their elements. When a container is copied, all elements are copied. When a container is destroyed, all objects it contains are in turn destroyed. • All copies in STL are deep copies. • These value semantics imply that STL containers can never overlap.

  26. Value Semantics 2 • Of course, for the sake of efficiency, one can store pointers to larger structures in a container. • But this itself presents problems. One has to adapt the comparison functions that several STL containers and algorithms require to work with pointers.

  27. Value Semantics 3 • A more important consideration is the use of STL with polymorphic objects. • Because of the use of value semantics in STL, polymorphic objects are either sliced (i.e., only their base parts are copied or assigned) or, if pointers are used, there is a problem of ownership of the object (e.g., what should happen if the object is removed from the container).

  28. Value Semantics 4 • One solution to this problem is the use a type of smart pointer. • For example, one can define a pointer object containing a reference to a polymorphic object. • When copying or assigning the smart pointer, a new copy of the referenced object is made (with the correct dynamic type).

  29. A Short History of STL • The first explorations of the concept of generic programming go back to work of Alexander Stepanov in the late '70s. • Later work at General Electric Research and Development in collaboration with David Musser led to the development of a list processing library in Ada, the first language to support generic programming. • This work, in 1987, was followed by additional work in Scheme

  30. A Short History of STL 2 • Stepanov, in work at Bell Laboratories and then later at Hewlett-Packard Research Laboratories, turned his attention to formulating generic algorithms in C++. • He was particularly attracted to the C++ feature of templates, which allow for parametrized types. • Bjarne Stroustrup, the developer of C++, introduced templates in 1991, in the cfront Release 3.0

  31. A Short History of STL 3 • While Stepanov was at Hewlett-Packard, Meng Lee joined his project and became a major contributor to the library that was eventually called STL. • Andrew Koenig of Bell Laboratories, a member of the X3J16 ANSI/ISO committee for C++ standardization, invited Stepanov to present the basic ideas of the library at the November 1993 X3J16 meeting. • This led to request from the committee for Stepanov and Lee to write a draft proposal for STL at the March 1994 committee meeting.

  32. A Short History of STL 4 • Further revisions and extensions were proposed to the STL at this point. • The major extension, associative containers, was implemented by David Musser. • After this work, the STL proposal was accepted by the ANSI committee at their July 1994 meeting. • The document produced by Stepanov and Lee was accepted into the X3J16 draft standard.

  33. A Short History of STL 5 • The dissemination of the STL became widespread with the decision by HP, in August 1994, to make the library freely available on the Internet. • This implementation, due primarily to Stepanov, Lee, and Musser, is the basis for all future versions of the STL.

  34. A Short History of STL 6 • In 1996 Stepanov joined SGI and together with Matthew Austern and Hans Boehm, worked on an implementation of the STL. • This implementation includes various extensions of the STL, such as thread-safe memory allocation and hash tables. • It should be noted that the extensions in the SGI version of the library have not yet been accepted into the C++ standard. • This implementation may be downloaded, together with extensive documentation on the library, at

  35. STL by example • First, to see the power of STL and how it functions let us look at a simple series of examples, adapted from David Harvey's online STL tutorial:

  36. STL by example 2 • Word Copy Example • The task is to write a function that reads in strings from an input stream, sorts them, eliminates duplicates, and then prints the results to an output stream, one word per line.

  37. STL by example 3 • Version 1 (Non-STL) • This version is given in pseudo-code to spare the reader the gory details (which they can presumably fill in for themselves).

  38. STL by example 4 ostream& wordcopy(istream &in, ostream &out) { string s; //string to be read //Also need a container to store the strings while( !in.eof() && in >> s ) { //store s in container } //Sort the container in alphabetical order  //Remove duplicates, this gets ugly if you used an array  //Print each string to output stream, one per line return out; }

  39. STL by example 5 • Version 2 (STL using vector container) ostream& wordcopy(istream &in, ostream&out) { string s; //string to be read vector<string> v; //STL vector container for the strings while( !in.eof() && in >> s ) v.push_back(s); //inserts s at end of container

  40. STL by example 6 //STL sort algorithm sort(v.begin(), v.end()); //Use the STL unique() to 'eliminate‘ duplicates //unique 'removes' consecutive duplicates vector<string>::iterator p = unique(v.begin(), v.end()); //Now send results to output for(vector<string>::iterator i = v.begin(); i != p; i++) out << *i << '\n'; return out; }

  41. STL by example 7 • A couple of comments on this version. • The use of iterator is an essential part of STL. Iterators are used to traverse containers, and to add or remove items from containers. • Each container-type has a corresponding iterator-type. Vectors use random-access iterators.

  42. STL by example 8 • The unique algorithm has a quirk that one must get used to. • Since it is a generic algorithm, and hence knows nothing about the underlying organization of the data it is being applied to, it cannot actually remove duplicates from the container. • What it in fact does is to place any duplicates at the end of the container. After the algorithm is completed, there will be a range of non-duplicates, say in positions [first,last). The function will return position last.

  43. unique() algorithm template<class FwdIt> FwdIt unique(FwdIt F, FwdIt L) { FwdIt X = F; //X will step through duplicates FwdIt Fb = F; //Fb will store position of first duplicate for( *X++ = *Fb; ++F != L; ) { if( !(*Fb == *F) ) { Fb = F; *X++ = *Fb; } } return X; }

  44. unique() algorithm 2 main() { int A[ ] = {1,1,1,2,3,3}; int *p = std::unique(&A[0],&A[6]); for(int *q = &A[0]; q != p; q++) std::cout << *q << ' '; std::cout << '\n'; return 0; }

  45. unique() algorithm 3 template<class FwdIt, class BinPred> FwdIt unique(FwdIt F, FwdIt L, BinPred P) { FwdIt X = F; FwdIt Fb = F; for( *X++ = *Fb; ++F != L; ) { if( !P(*Fb,*F) ) { Fb = F; *X++ = *Fb; } } return X; }

  46. unique() algorithm 4 • Consider the following predicate class P { public: bool operator()(int x, int y) { return ((x % 2) != (y%2)); } };

  47. unique() algorithm 5 main() { int A[ ] = {1,1,1,2,3,3}; int *p = unique(&A[0],&A[6],P()); for(int *q = &A[0]; q != p; q++) std::cout << *q << ' '; std::cout << '\n'; return 0; }

  48. STL by example 9 • Version 3 (STL using set container) • A student studying STL quickly learns about the benefits of the STL associative containers. • For the task at hand, the set container is particularly useful. Data in a set is kept in sorted order, and no duplicates are allowed. This is exactly what we want.

  49. STL by example 10 ostream& wordcopy(istream &in, ostream &out) { string s; //string to be read set<string> words; //STL set container for the strings while( !in.eof() && in >> s ) words.insert(s); //Insert in sorted order

  50. STL by example 11 //Now send results to output for(set<string>::iterator i = words.begin(); i != words.end(); i++) out << *i << '\n'; return out; }