Professional Documents
Culture Documents
Templates: Anas P.S 21-Feb-2011
Templates: Anas P.S 21-Feb-2011
Anas P.S
21-Feb-2011
Definition
Templates are mechanisms for generating
functions and classes based on type
parameters
Also called “type parameterization”
◦ Types are given as parameters to a template
◦ Like variables are given as a function’s parameters
Two types
Function Templates And
Class Templates
Function Templates
Syntax:
template<class type> function_declaration;
-Or-
template<typename type>
function_declaration;
Templates of Functions (Example)
// Precondition: x[] is an array of length n, of
// a generic type. n is a positive integer.
// Postcondition: the output is the minimum in x[]
template<typename T> T min(T x[], int n){
T m = x[0]; // M is the minimum so far
for (int i=1;i<n;i++)
if (x[i]<m)
m=x[i];
return m;
}
4
How to Call a Function Template
functionName<an-actual-type>(parameter-list);
}
Output is:
void main() Stack<char> c1;
{ c1.push('a');
Stack<int> c; c1.push('b');
c.push(1); c1.push('c');
c.push(2); c1.pop();
c.push(3); c1.pop();
c.pop(); c1.pop();
c.pop();
c.pop();
}
Index: 0 1 Index: 0 a
Index: 1 2 Index: 1 b
Stack Full Stack Full
Poped Element is : 2 Poped Element is : b
Poped Element is : 1 Poped Element is : a
Nothing to pop Nothing to pop
Class templates Continues……
The declarations and definitions need to be in the same header file
Iterator
Algorithm
Iterator
Algorithm
Iterator
Iterator
Algorithm
Container Types
The STL provides several basic kinds of
containers
◦ <vector> : one-dimensional array
◦ <list> : double linked list
◦ <deque> : double-ended queue
◦ <queue> : queue
◦ <stack> : stack
◦ <set> : set
◦ <map> : associative array
Container Categories
Sequence Containers
Organize collection of objects,into a strictly linear
arrangement
◦ Egs: vector, deque, list
Sorted Associative Containers
Provide fast retrieval of objects from the collection based
on keys
◦ Egs: set, multiset, map, multimap
Adapters
Inherit from the two first-class containers and modify their
interfaces.
◦ Three major types:- stack, queue, priority queues
Vector
A vector is a Sequence that supports random access to elements,
constant time insertion and removal of elements at the end, and
linear time insertion and removal of elements at the beginning or in
the middle.
The number of elements in a vector may vary dynamically;
memory management is automatic.
Vector is the simplest of the STL container classes, and in many
cases the most efficient.
#include <vector>
Vector Continues….
Syntax: vector<of what>
For example : vector<int> - vector of integers.
vector<string> - vector of strings.
vector<int *> - vector of pointers to integers.
vector<CMyClass*> - vector of pointers to CMyClass
objects.
Construction: A vector can be initialized by specifying its size
and
a prototype element or by another vector
vector<Date> v1(1000); // creates vector of size 1000,
// requires default constructor for
Example
void example()
{
const int nSize = 10;
vector<int> vecEgs(nSize );
// initialize vec.
vecEgs[0] = 1;
int nEgArr [nSize ];