1 / 28

STL Standard Template Library

STL Standard Template Library. Good reference book: The C++ Standard Library A Tutorial and Reference by Nicolai M. Josuttis 1999 – Addison Wesley Longman, Inc. ISBN: 0201379260 My presentation will come from both books plus a small amount from other references.

timmyl
Download Presentation

STL Standard Template Library

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. STLStandard Template Library • Good reference book: • The C++ Standard Library • A Tutorial and Reference • by Nicolai M. Josuttis • 1999 – Addison Wesley Longman, Inc. • ISBN: 0201379260 • My presentation will come from both books plus a small amount from other references. • www.cs.rpi.edu/projects/STL/stl/stl.html

  2. History • 1989 – Start of standardization of C++ • 1997 – Finished, published Sept. 1998 • 750 pages, published by the International Standards Organization (ISO). • Title: “Information Technology – Programming Languages – C++” • “..it defines the exact contents and behavior of C++, ...” • Part of the standard is a standard library.

  3. History: C++ Standard Library • core components for • I/O • strings, • containers (data structures) • algorithms (such as sort, search and merge) • support for numeric computation • support for internationalization (such as different character sets)

  4. History: STL added • 1994 – The STL was added to the C++ standard library. • STL defines powerful, template-based, reusable components that implement many common data structures and algorithms used to process those data structures. • Makes heavy use of both function and class templates.

  5. Templates • Templates are functions or classes that are written for one or more types not yet specified. • When you use a template, you pass the types as arguments, explicitly or implicitly.

  6. Template Example • template <class T> • inline const T& my_max (const T& a, const T& b) • { • if(a < b) return b; • else return a; • }

  7. exmaple : details • First line defines T as an arbitrary data type that is specified by the caller when the caller calls the function. The type is classified by class, although it does not have to be a class. You can use any data type as long as it provides the operations that the template uses. • inline is a hint to the compiler to put this function in-line since it is so short.

  8. example: details • const before the type reference allows the function to use constant reference parameters. • T& is the type of the function. (A reference to the type T) • my_max is the name of the function being defined as a template. • const T& a (and const T& b) define two reference parameters whose value cannot be changed.

  9. example: details • The code for the function itself is simply to return the maximum of the two parameter values as the value of the function. • Instantiated by its use (since it is a function and not a class). • Use: • int a(10), b(12); • cout << my_max(a,b) << endl; • Output: 12

  10. MyArray class template • template <class T> • class MyArray { • public: • MyArray(int = 10); • ~MyArray( ); • void FillArray(T=2); • void PrintArray( ); • private: • T* ArrayPtr; • int size; • };

  11. MyArray constructor • template < class T> • MyArray<T>::MyArray(int s) • { • if(s < 0) s = 10; • size = s; • ArrayPtr = new T[s]; • }

  12. MyArray destructor • template < class T > • MyArray<T>::~MyArray( ) • { • delete [ ] ArrayPtr; • }

  13. MyArray FillArray function • template < class T > • void MyArray<T>::FillArray(T value) • { • for(int i=0; i<size; i++) • ArrayPtr[i]=value; • }

  14. MyArray PrintArray function • template < class T > • void MyArray<T>::PrintArray( ) • { • for(int i = 0; i<size; i++) • cout << ArrayPtr[i] << “ “; • cout << endl; • }

  15. test MyArray • #include “MyArray.h” • int main (void) • { • MyArray<int> fred(3); • MyArray<double> suzie(5); • MyArray<int> sam; • fred.FillArray(-2); fred.PrintArray(); • suzie.FillArray(3.25); suzie.PrintArray(); • sam.FillArray(); sam.PrintArray(); • }

  16. test output • -2 -2 -2 • 3.25 3.25 3.25 3.25 3.25 • 2 2 2 2 2 2 2 2 2 2

  17. STL General Concepts • Namespace std • (covered before) • std::cout << std::hex << 127 << std::endl; • or • using std::cout; • using std::endl; • cout << std::hex << 127 << endl; • or • using namespace std; • cout << hex << 127 << endl;

  18. STL General Concepts cont. • Header files • In addition to the standard ones: • #include <iostream> • There is normally a special include for each use of a STL template. • #include <string> • #include <vector> • #include <stack> • (more specifics as we describe STL components)

  19. STLComponents • Containers • Manage collections of objects of a certain kind. • Iterators • Used to step through the elements of collections of objects. • Small but common interface for an arbitrary container type. • Algorithms • Used to process elements of collections

  20. iterator container iterator algorithm container iterator container

  21. STLSequence Containers • Ordered Collections. • Every element has a certain position. • Position independent of the value of the element. • Position depends on order and place of insertion. • Vector • Deque • List

  22. STLSequence Containers Vector Deque List

  23. STLAssociative Containers • Sorted Collection. • Actual position of an element depends on its value due to certain sorting criterion. • Order of insertion doesn't matter. • Set • Multiset • Map • Multimap

  24. STL Associative Containers Set/Multiset Map/Multimap

  25. STL Iterators • An object that can “iterate” over elements. May be all or part of a STL container. • An iterator represents a certain position in a container. • Operator * • Returns the element of the actual position. • Operator ++ • Lets the iterator step forward to the next element. • Most iterators also allow stepping backwards by using operator - -

  26. (cont.) • Operator == and != • Returns whether two iterators represent the same position. • Operator = • Assigns an iterator (the position of the element to which it refers)

  27. Iterator related member functions • begin( ) • returns an interator that represents the beginning of the elements in the container. • end( ) • returns an iterator that represents the end of the elements in the container. • The end position is behind the last element.

  28. end ( ) begin ( )

More Related