Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2009 – Curt Hill Standard Template Library An Introduction.

Similar presentations


Presentation on theme: "Copyright © 2009 – Curt Hill Standard Template Library An Introduction."— Presentation transcript:

1 Copyright © 2009 – Curt Hill Standard Template Library An Introduction

2 Generics The template feature of C++ allows the separation of logic from data type –Sorting an array is always the same –The only thing that changes is type and location of the key The STL implements a number of well known abstract data types as groups of template classes Originally developed at HP Copyright © 2009 – Curt Hill

3 Components of the STL Containers –Our main focus Algorithms Iterators Function templates Adapters Copyright © 2009 – Curt Hill

4 Containers Containers are typically one or more template classes that store other items The items they store may be classes or primitives Each class has its own requirements for the characteristics of the stored class or primitive –May be as little as suitable for assignment Every object can have a pointer reference it, which is suitable for assignment but not comparison –May be as complicated suitable for equality or less comparison Copyright © 2009 – Curt Hill

5 Example Containers Vector List Deque (a form of queue) Sets and bags (multisets) Maps and multimaps (trees or dictionaries) We will consider all of these Copyright © 2009 – Curt Hill

6 Algorithms Common algorithms for manipulating a container –Sort –Search –Comparison These algorithms require that the contained class be suitable for comparison –This will often obligate us to overload comparison operators Copyright © 2009 – Curt Hill

7 Iterators The mechanism to traverse an entire container Also used to input/output data to/from the container Iterators are generally classes made to look like pointers Often get one or need one for most data access Copyright © 2009 – Curt Hill

8 Function templates Many algorithms accept a function template as a parameter that modifies how the algorithm works Usually there is some place in the algorithm where an operation is needed This allows the user to write a template function that handles it These functions may do arithmetic or comparison operation when we have not overloaded other operators Copyright © 2009 – Curt Hill

9 Adapters An adaptor modifies the interface of other components This allows us to glue components together to get larger more usable items Container adapter –Transforms a container to function with specified characteristics –There is no stack container but a stack adapter that can make a list into a stack Iterator adapters give more options than our regular iterators Function adaptors change the characteristics of functions Copyright © 2009 – Curt Hill

10 A problem The STL causes some issues Complicated actions are being done to your objects This is in an internal environment you know next to nothing about Since you do not know what it is doing, it is a good idea to code classes following some recommendations Copyright © 2009 – Curt Hill

11 Class Recommendations Code a default constructor –Either make it private or code it for real –This prevents the compiler from generating a phony one –If it is private then nobody can use it without an error Code a copy constructor –This will be used by the STL –Makes sure that it works properly Copyright © 2009 – Curt Hill

12 More Recommendations Code a destructor –It should deallocate pointers (carefully) –It should close files Overload the assignment operator –This will often just invoke the copy constructor –Make it private if something is not copyable –If not copyable it should not be in an STL container class Copyright © 2009 – Curt Hill

13 Comparison operators Overload equality and less than They will usually require code These are the most likely to be used by STL All the rest can be constructed from those two If no comparison is possible then make a body with an assert in it Keeps from accidental use by member code Make private – prevents accidental use by non-member code Copyright © 2009 – Curt Hill

14 Recommendation Summary Every primitive and most good components have these already These are also good recommendations for any classes containing files or pointers Consider the ClassMaker program on the downloads page Copyright © 2009 – Curt Hill


Download ppt "Copyright © 2009 – Curt Hill Standard Template Library An Introduction."

Similar presentations


Ads by Google