Presentation is loading. Please wait.

Presentation is loading. Please wait.

Template is a declaration (similar to class declaration), which deals with generic types. Templates are evaluated to produce concrete classes when a template-base.

Similar presentations


Presentation on theme: "Template is a declaration (similar to class declaration), which deals with generic types. Templates are evaluated to produce concrete classes when a template-base."— Presentation transcript:

1 Template is a declaration (similar to class declaration), which deals with generic types. Templates are evaluated to produce concrete classes when a template-base class is instantiated using a concrete type. Templates are powerful because they allow producing generic classes! C++ Templates

2 Template class example: template class MyStorageTemplate { public: T Get(int index); // template method void Put(int index); // template method private: T Data[N]; // private data field }; Template function example: template void MyTemplateFuncion(T param); Validity of template definition is evaluated at compile time it depends on the concrete type used to instantiate the template. C++ Template Declaration

3 The Standard Template Library (STL) is a software library included in the C++ Standard Library. It provides containers, iterators, and algorithms. More specifically the C++ Standard Library is based on the STL Library published by SGI. Both include some features not found in the other. SGI's STL is rigidly specified as a set of headers, while ISO C++ does not specify header content, and allows implementation either in the headers, or in a true library. STL was architected by Alexander Stepanov in 1979. Standard Template Library

4 The quality of the C++ compiler has a large impact on usability of STL: Error messages involving templates are difficult to decipher. Excessive usage of STL templates leads to code bloat. Template instantiation tends to increase compilation time and memory usage (sometimes by as much as an order of magnitude). STL implementations non-standardized. STL Disadvantages

5 Header file: #include Declaration: template <class Type, class Allocator = allocator > class vector; Template instantiation: vector myStringVector; STL vector Class

6 vectorConstructor. assignErases a vector and copies the specified elements to the empty vector. atReturns a reference to the element at a specified location in the vector. backReturns a reference to the last element of the vector. beginReturns a random-access iterator to the first element in the container. capacityReturns the number of elements that the vector could contain without reallocating. clearErases the elements of the vector. emptyTests if the vector container is empty. endReturns a random-access iterator that points just beyond the end of the vector. eraseRemoves an element or a range of elements in a vector from specified positions. frontReturns a reference to the first element in a vector. insertInserts an element or a number of elements into the vector at a specified position. max_sizeReturns the maximum length of the vector. pop_backDeletes the element at the end of the vector. push_backAdd an element to the end of the vector. rbeginReturns an iterator to the first element in a reversed vector. rendReturns an iterator to the end of a reversed vector. resizeSpecifies a new size for a vector. reserveReserves a minimum length of storage for a vector object. sizeReturns the number of elements in the vector. swapExchanges the elements of two vectors. STL vector Members

7 vectorConstructor. assignErases a vector and copies the specified elements to the empty vector. atReturns a reference to the element at a specified location in the vector. backReturns a reference to the last element of the vector. beginReturns a random-access iterator to the first element in the container. capacityReturns the number of elements that the vector could contain without reallocating. clearErases the elements of the vector. emptyTests if the vector container is empty. endReturns a random-access iterator that points just beyond the end of the vector. eraseRemoves an element or a range of elements in a vector from specified positions. frontReturns a reference to the first element in a vector. insertInserts an element or a number of elements into the vector at a specified position. max_sizeReturns the maximum length of the vector. pop_backDeletes the element at the end of the vector. push_backAdd an element to the end of the vector. rbeginReturns an iterator to the first element in a reversed vector. rendReturns an iterator to the end of a reversed vector. resizeSpecifies a new size for a vector. reserveReserves a minimum length of storage for a vector object. sizeReturns the number of elements in the vector. swapExchanges the elements of two vectors. vector Members

8 Write a program that: - Initializes a vector based on user input by reading vector size and elements from cin -removes or reorganizes vector elements base on some criteria vector Exercise

9 template class Vector { public: Vector(int initialSize); enum Position {Start = 0, End = -1}; int GetSize() const; T& operator[](int index) const; void InsertAt(int index, const T& element); void RemoveAt(int index, const T& element); private: int Size; T* Data; void Reallocate(int newSize); }; Use memcpy for memory copy operations. Make the class verify parameters and throw exceptions. You can use either new or malloc to allocate / reallocate memory. Write Your Own Vector


Download ppt "Template is a declaration (similar to class declaration), which deals with generic types. Templates are evaluated to produce concrete classes when a template-base."

Similar presentations


Ads by Google