7 - Templates - STL
7 - Templates - STL
INTRODUCTION TO
PROGRAMMING
CSC 1102
MD.NAZMUL HOSSAIN
(UNDERGRADUATE)
LECTURER, CS, AIUB
Slide-2
Templates
Templates are the foundation of generic programming, which involves writing code in
a way that is independent of any particular type.
A template is a blueprint or formula for creating a generic class or a function. The
library containers like iterators and algorithms are examples of generic programming
and have been developed using template concept.
There is a single definition of each container, such as vector, but we can define many
different kinds of vectors for example, vector
MMH
Slide-3
Templates
#include <iostream> T getmax ()
using namespace std; {
T retval;
template <class T> retval = a>b? a : b;
return retval;
class mypair }
{ };
T a, b;
public: int main ()
mypair (T first, T second) {
{ //generic datatype T
a=first; mypair <int> myobject (100, 75);
b=second; cout << myobject.getmax();
} return 0;
}
MMH
Slide-4
STL is a set of general purpose templatized classes (data structures) & functions
that could be used as a standard for storing & processing of data.
STL has three components:
1. Containers: used to store data.
2. Algorithms: procedures/functions used to process data stored in container.
3. Iterators: objects like pointers used to point any element of a container or to
iterate through a container.
MMH
Slide-5
Container
Containers are of three types
Sequence container: Vector, list, deque.
Associative container: map, multimap, set, multiset.
Derived container: priority-queue, stack, queue.
General Theory of Operation
Functions provided by sequence container: insert, erase, push_back,
pop_back, begin, end. push_front,pop_front these two can done only by list &
deque.
Functions provided by associative container: insert, erase, begin, end, find.
MMH
Slide-6
Vector
Vectors are sequence containers representing arrays that can change in size.
Just like arrays, vectors use contiguous storage locations for their elements, which
means that their elements can also be accessed using offsets on regular pointers to
its elements, and just as efficiently as in arrays. But unlike arrays, their size can change
dynamically, with their storage being handled automatically by the container.
MMH
Slide-7
MMH
Slide-8
MMH
Slide-9
Vector (insert & delete operation)
#include<iostream>
p = v.begin() + 2;
#include<vector>
v.insert(p,4,-1);
using namespace std;
for(p=v.begin();p!=v.end();p++)
int main()
cout<<*p<<" “ <<endl;
{
int n;
p = v.begin() + 2;
vector<int> v(5);
v.erase(p,p+4);
vector<int>::iterator p;
for(p=v.begin();p!=v.end();p++)
cout<<"Insert 5 numbers:" <<endl;
cout<<*p<<" “ <<endl;
for(p=v.begin();p!=v.end();p++)
return 0;
{
}
1 2 3 4 5
cin>>n;
*p=n; 1 2 -1 -1 -1 -1 3 4 5
}
1 2 3 4 5
MMH
Slide-10
Storing Class Object in Vector
#include<iostream> int main(){
#include<vector> vector<A> v(3);
using namespace std; int i,n;
cout<< "Enter Three Number:"<<endl;
class A { for(i=0;i<v.size();i++) {
int a; cin>>n;
public: v[i]=A(n); //Parameterized Constructor
A(int a=0) }
{ cout << "Outputs are:";
this ->a=a; for(i=0;i<v.size();i++)
} cout<<v[i].get_a()<<" ";
int get_a() return 0;
{ } input output
return a; 10 10 20 30
} 20
}; 30
MMH
Slide - 20
REFERENCES