STL Standard Template Library Good reference book The
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. – www. cs. rpi. edu/projects/STL/stl. html
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.
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)
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.
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.
Template Example ● ● ● template <class T> inline const T& my_max (const T& a, const T& b) { if(a < b) return b; else return a; }
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.
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.
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
My. Array class template – – – template <class T> class My. Array { public: My. Array(int = 10); ~My. Array( ); void Fill. Array(T=2); void Print. Array( ); private: T* Array. Ptr; int size; };
My. Array constructor template < class T> My. Array<T>: : My. Array(int s) { if(s < 0) s = 10; size = s; Array. Ptr = new T[s]; – – – – }
My. Array destructor – – – template < class T > My. Array<T>: : ~My. Array( ) { delete [ ] Array. Ptr; }
My. Array Fill. Array function – – – template < class T > void My. Array<T>: : Fill. Array(T value) { for(int i=0; i<size; i++) Array. Ptr[i]=value; }
My. Array Print. Array function – – – – template < class T > void My. Array<T>: : Print. Array( ) { for(int i = 0; i<size; i++) cout << Array. Ptr[i] << “ “; cout << endl; }
test My. Array – – – #include “My. Array. h” int main (void) { My. Array<int> fred(3); My. Array<double> suzie(5); My. Array<int> sam; fred. Fill. Array(-2); fred. Print. Array(); suzie. Fill. Array(3. 25); suzie. Print. Array(); sam. Fill. Array(); sam. Print. Array(); – – }
test output – – – -2 -2 -2 3. 25 2 2 2 2 2
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;
STL General Concepts cont. ● ● Header files In addition to the standard ones: – ● There is normally a special include for each use of a STL template. – – – ● #include <iostream> #include <string> #include <vector> #include <stack> (more specifics as we describe STL components)
● Containers – ● Manage collections of objects of a certain kind. Iterators – – ● STL Components 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
container iterator algorithm container iterator container
STL Sequence 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
STL Sequence Containers Vector Deque List
STL Associative 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
STL Associative Containers Set/Multiset Map/Multimap
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 - -
(cont. ) ● Operator == and != ● – Returns whether two iterators represent the same position. Operator = ● Assigns an iterator (the position of the element to which it refers)
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.
begin ( ) end ( )
- Slides: 28