Beruflich Dokumente
Kultur Dokumente
Template
&
Standard Template Library
Computer Programming II
Objectives
Function Template
Class Template
Standard Template Library (STL)
STLs vector class
STLs set class
STLs map class
Computer Programming II
Introduction
Template allows us to write generic functions
or classes (called function template, class
template) that accept any data type as
parameters or attributes.
During compilation, the compiler will produce
a separate definition for every data type that
uses the template.
Computer Programming II
Function Template
Recall the function to swap integers:
void Swap (int& m, int& n) {
int temp = m;
m = n;
n = temp;
}
Computer Programming II
These 2 functions
do the same thing,
only different in the
data type of objects
they swap.
We can avoid
writing the
redundant code by
replacing both
functions with a
function template.
Function Template
Template version of Swap() function:
template <typename T>
void Swap(T& x, T& y) {
T temp = x;
x = y;
y = temp;
}
int main() {
int m = 22, n = 66;
Swap(m, n);
// integers
cout << "m = " << m << endl
<< "n = " << n << endl;
string s1 = "Michael", s2 = "Kelly";
Swap(s1, s2); // strings
cout << "s1 = " << s1 << endl
<< "s2 = " << s2 << endl;
return 0;
}
Computer Programming II
The symbol T is
called type
parameter. It is
replaced by an
actual type when the
function is invoked.
Compiler produces 2
definitions of
Swap() functions:
one for int, one for
string
Output:
m = 66
n = 22
s1 = Kelly
s2 = Michael
Computer Programming II
Computer Programming II
Class Template
Define a class template
Pair that have 2 attributes
of the same type.
template <typename T>
class Pair {
T a;
T b;
public:
void set (T a, T b) {
this->a = a;
this->b = b;
}
void print() const {
cout << "(" << a << ","
<< b << ")\n";
}
};
Computer Programming II
int main() {
Pair<int> p1;
p1.set (3,5);
p1.print();
Pair<string> p2;
p2.set ("Peter",
"Jackson");
p2.print();
Pair<char> p3;
p3.set ('P','J');
p3.print();
return 0;
}
Output:
(3,5)
(Peter,Jackson)
(P,J)
Class Template
Class template allows us to
write one class that works
for many data types
template <typename T>
class Pair {
T a;
T b;
public:
void set (T a, T b) {
this->a = a;
this->b = b;
}
void print() const {
cout << "(" << a << ","
<< b << ")\n";
}
};
Computer Programming II
int main() {
Pair<int> p1;
p1.set (3,5);
p1.print();
Pair<string> p2;
p2.set ("Peter",
"Jackson");
p2.print();
Pair<char> p3;
p3.set ('P','J');
p3.print();
return 0;
}
Output:
(3,5)
(Peter,Jackson)
(P,J)
Computer Programming II
int main() {
Pair<int,char> p1;
p1.set (1, 'A');
p1.print();
Pair<char,string> p2;
p2.set ('B',"Boy");
p2.print();
Pair<int,int> p3;
p3.set (99,99);
p3.print();
return 0;
}
Output:
(1,A)
(B,Boy)
(99,99)
10
Computer Programming II
11
Type
Sequential
Description
Bidirectional linked list of elements
vector * Sequential
deque
Sequential
set *
Associative
multiset Associative
map *
Associative
multimap Associative
12
pop_back
size
clear
Computer Programming II
13
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v;
v.push_back(4);
v.push_back(2);
v.push_back(1);
v.push_back(6);
v.push_back(8);
v.push_back(3);
for (int i=0; i < v.size(); i++)
cout << v[i] << " ";
}
Computer Programming II
Output:
4 2 1 6 8 3
Push the item to the
end of the vector
The items are accessed
just as if they are array
elements
14
With vector, we do
not need to worry
about memory
allocating for it
} while (n!=-1);
Output:
=> 1
=> 5
=> 3
=> 2
=> -1
1 5 3 2
Computer Programming II
15
Computer Programming II
16
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> nums;
nums.insert (nums.begin(), -99); // -99
nums.insert (nums.begin(), 14); // 14 -99
nums.insert (nums.end(), 57); // 14 -99 57
nums.insert (nums.begin()+1, 22);
// 14 22 -99 57
for (int i = 0; i < nums.size(); i++)
cout << nums[i] << " ";
cout << endl;
nums.erase (nums.begin()+2); // 14 22 57
nums.erase (nums.begin()); // 22 57
for (int i = 0; i < nums.size(); i++)
cout << nums[i] << endl;
return 0;
}
Computer Programming II
Insert -99 to
the beginning
of the vector
Output:
14 22 -99 57
22 57
Insert 22 to as
the 2nd element
of the vector
Erase the 3rd
element in the
vector
17
Computer Programming II
18
Computer Programming II
19
STL Iterators
Iterator: An STL iterator is a mechanism (pointer) for
accessing the elements in a container one at a time.
The type of iterator must match the type of container
in order for the iterator to work.
set<int> s;
for (set<int>::iterator it = s.begin();
it != s.end();
it++)
cout << *it << " ";
20
Computer Programming II
Output1:
-999
18
321
Enter an integer:
5
5 is not in set.
Use iterator to
iterate the set.
Output2:
-999
18
321
Enter an integer:
321
321 is IN set.
21
Computer Programming II
22
Computer Programming II
char key;
cout << "Enter a char: ";
cin >> key;
it = m.find (key);
if (it == m.end())
cout << key
<< " is NOT in map.";
else
cout << key << " is IN map.";
return 0;
}
Output 1:
Apple Boy Cat
A Apple
B Boy
C Cat
Enter a char: Z
Z is NOT in map
Output 2:
Apple Boy Cat
A Apple
B Boy
C Cat
Enter a char: C
C is IN map
23