180 likes | 402 Views
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!.
E N D
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!
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.
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.
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.
STL vector Class Header file: #include <vector> Declaration: template <class Type, class Allocator = allocator<Type>> class vector; Template instantiation: vector<string> myStringVector;
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.
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.
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
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.