0% found this document useful (0 votes)
43 views11 pages

7 - Templates - STL

Templates allow defining classes and functions that can work with different data types. A template is a blueprint for creating generic classes and functions. The Standard Template Library (STL) provides common data structures like vectors and lists as well as associated algorithms using templates. STL has containers like vectors that store data, algorithms that process the data, and iterators that traverse container elements.

Uploaded by

Abdullah Rayed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views11 pages

7 - Templates - STL

Templates allow defining classes and functions that can work with different data types. A template is a blueprint for creating generic classes and functions. The Standard Template Library (STL) provides common data structures like vectors and lists as well as associated algorithms using templates. STL has containers like vectors that store data, algorithms that process the data, and iterators that traverse container elements.

Uploaded by

Abdullah Rayed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

CHAPTER 7

TEMPLATES & STL


COURSE NAME

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

<int> or vector <string>


 You can use templates to define functions as well as classes

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

Standard Template Library (STL)

 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

Vector (without iterator)


#include <iostream> v.push_back(40);
#include<vector> v.push_back(50);
using namespace std;
int main() for(int i=0;i<v.size();i++)
{ cout <<v[i] <<endl;
int n;
vector<int> v(3); // 3 input return 0; input output
for(int i=0;i<3;i++) }
{ 10 10
cin>>n;
20 20
v[i]=n;
} 30 30
40
50

MMH
Slide-8

Vector (with iterator)


#include<iostream> v.push_back(40);
#include<list> v.push_front(50);
Using namespace std;
int main() for(p=v.begin();p!=v.end();p++)
{ cout<<*p<<endl;
int n;
list<int> v(3); // 3 input return 0;
list<int>::iterator p; } input Output
1 50
for(p=v.begin();p!=v.end();p++)
{ 2 1
cin>>n; 3 2
*p=n; 3
} 40

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

 Tech Yourself C++, Herbert Schildt.


 C++ How To Program, Paul Deitel, Harvey Deitel.
 The C++ Complete Reference, Herbert Schildt.
 Thinking in C++, Volume One, Bruce Eckel.

You might also like