Presentation is loading. Please wait.

Presentation is loading. Please wait.

STL Standard Template Library ● Good reference book: – The C++ Standard Library ● A Tutorial and Reference ● by Nicolai M. Josuttis ● 1999 – Addison Wesley.

Similar presentations


Presentation on theme: "STL Standard Template Library ● Good reference book: – The C++ Standard Library ● A Tutorial and Reference ● by Nicolai M. Josuttis ● 1999 – Addison Wesley."— Presentation transcript:

1 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/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 ● 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 MyArray { – public: – MyArray(int = 10); – ~MyArray( ); – void FillArray(T=2); – void PrintArray( ); – private: – T* ArrayPtr; – int size; – };

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

12 MyArray destructor – template – MyArray ::~MyArray( ) – { – delete [ ] ArrayPtr; – }

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

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

15 test MyArray – #include “MyArray.h” – int main (void) – { – MyArray fred(3); – MyArray suzie(5); – MyArray 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 ● There is normally a special include for each use of a STL template. – #include ● (more specifics as we describe STL components)

19 STL Components ● 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 container algorithm iterator

21 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

22 STL Sequence Containers Vector Deque List

23 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

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 begin ( ) end ( )


Download ppt "STL Standard Template Library ● Good reference book: – The C++ Standard Library ● A Tutorial and Reference ● by Nicolai M. Josuttis ● 1999 – Addison Wesley."

Similar presentations


Ads by Google