1 / 9

C++ Templates

C++ Templates. 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!.

Download Presentation

C++ Templates

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. C++ Templates 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!

  2. C++ Template Declaration Template class example: template<class T, int N> 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<class T> void MyTemplateFuncion(T param); Validity of template definition is evaluated at compile time it depends on the concrete type used to instantiate the template.

  3. Standard Template Library 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.

  4. STL Disadvantages 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.

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

  6. STL vector Members vector Constructor. assign Erases a vector and copies the specified elements to the empty vector. at Returns a reference to the element at a specified location in the vector. backReturns a reference to the last element of the vector. begin Returns a random-access iterator to the first element in the container. capacity Returns the number of elements that the vector could contain without reallocating. clear Erases the elements of the vector. empty Tests if the vector container is empty. end Returns a random-access iterator that points just beyond the end of the vector. erase Removes an element or a range of elements in a vector from specified positions. front Returns a reference to the first element in a vector. insert Inserts an element or a number of elements into the vector at a specified position. max_size Returns the maximum length of the vector. pop_back Deletes the element at the end of the vector. push_back Add an element to the end of the vector. rbegin Returns an iterator to the first element in a reversed vector. rend Returns an iterator to the end of a reversed vector. resize Specifies a new size for a vector. reserve Reserves a minimum length of storage for a vector object. size Returns the number of elements in the vector. swap Exchanges the elements of two vectors.

  7. vector Members vector Constructor. assign Erases a vector and copies the specified elements to the empty vector. at Returns a reference to the element at a specified location in the vector. backReturns a reference to the last element of the vector. begin Returns a random-access iterator to the first element in the container. capacity Returns the number of elements that the vector could contain without reallocating. clear Erases the elements of the vector. empty Tests if the vector container is empty. end Returns a random-access iterator that points just beyond the end of the vector. erase Removes an element or a range of elements in a vector from specified positions. front Returns a reference to the first element in a vector. insert Inserts an element or a number of elements into the vector at a specified position. max_size Returns the maximum length of the vector. pop_back Deletes the element at the end of the vector. push_back Add an element to the end of the vector. rbegin Returns an iterator to the first element in a reversed vector. rend Returns an iterator to the end of a reversed vector. resize Specifies a new size for a vector. reserve Reserves a minimum length of storage for a vector object. size Returns the number of elements in the vector. swap Exchanges the elements of two vectors.

  8. vector Exercise 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

  9. Write Your Own Vector template <class T> 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.

More Related