Download presentation
Presentation is loading. Please wait.
1
Template
2
Contents 1 What is Template? 2 Function Templates 3 Template Functions
Class Templates 4 Template Classes 5
3
What is a Template(模板) T max(T x, T y) { return (x>y)? x:y; }
#define max(x, y) ((x>y)? x:y) int max(int x, int y) { return (x>y) ? x : y; } char max(char x, char y) double max(double x, double y) 宏替换只是简单的符号替换 int main() { int i = 5, j = 3; cout << max(++i, j); return 0; } Result: 7 Problem: Macro presents the possibility for serious side effects: no type checking, repetitive operations and so on. T max(T x, T y) { return (x>y)? x:y; } Result: 6
4
Template(模板) Template mechanism allows a type to be a parameter in the definition of a class or a function. (在定义函数和类时,模板机制允许类型是以一个参数形式个给出) Templates provide a simple way to represent a wide range of generic concepts and simple ways to combine them.(提供一种表示更广泛的通用概念的方式) Templates are one of C++’s capabilities for software reusability. (是C++软件可重用的方法之一) Templates provide direct support for generic programming, that is, programming using generic types.(支持泛型程序设计,即使用通用类型的程序设计)
5
Function Templates(函数模板)
For example, int max(int x, int y) { return (x>y) ? x : y; } char max(char x, char y) double max(double x, double y) template <typename T> T max(T x, T y) { return (x>y)? x:y; } template <class T> T max(T x, T y) { return (x>y)? x:y; } Keyword template Keyword typename type identifier template < typename identifier>, or template < class identifier> function_declaration(or function_definition) ;
6
Function Templates(函数模板)
A function template is a special functions that can operate with generic types. (能处理通用类型的特殊函数) This allows us to create a function template whose functionality can be adapted to more than one type without repeating the entire code for each type.(允许我们创建函数模板,其功能可适用更多的类型,而对于每种类型不必要重复整个代码)
7
Function Template Instantiation
To use function templates, we use the following format for the function call: Function template instantiation (函数模板实例化) template <typename T> T max(T x, T y); //declaration int main() { cout << max(3, 4) << endl; cout << max(3.4, 67.8) << endl; cout << max('s', 'x') << endl; return 0; } template <typename T>//definition T max(T x, T y) return (x > y) ? x : y; cout<<max<int>(3,4); cout<<max<double>(3.4, 67.8); cout<<max<char>('s', 'x') function_name <type> (parameters);
8
Function Templates with different Parameter Types
template <typename T1, typename T2> T1 getMin(T1 x, T2 y) { return x<y ? x : (T1)y; } int main() { int i = 9, j = 6; double a = 8.3, b = 7.8; cout << getMin(i, a); return 0; } cout<<getMin<int, double>(i, a);
9
Function Templates and Function Overloading
Both of they make codes more compact and convenient. Function overloading is normally used to perform similar or identical operations on different types of data.(函数重载是针对不同的类型执行相似的或相同的操作) Function templates are used to perform identical operations on different types of data.(函数模板是针对不同类型执行相同的操作)
10
Function Templates Overloading
template <typename T> void Swap(T& x, T& y); void Swap(int& x, int& y); int main() { int i = 9, j = 6; double a = 8.3, b = 7.8; char s = '4', t = '5'; Swap(i, j); Swap(a, b); Swap(s, t); return 0; } template <typename T> void Swap(T& x, T& y) { T temp; temp = x; x = y; y = temp; cout << "Call of template\n"; } void Swap(int& x, int& y) { int temp; temp = x; x = y; y = x; cout << "Call of function\n"; } Result: Call of function Call of template
11
Function Templates Overloading
We can declare several function templates with the same name and even declare a combination of function templates and ordinary functions with the same name. (可以同时声明几个同名的函数模板,也可以声明函数模版与一般的同名函数的组合) The compiler always matches first function prototypes (exactly matching), and then function templates. (编译器总是先匹配原型,即精确匹配,然后是函数模板) Template <typename T> T getMin(T p1, T p2); T getMin(T p1, T p2, T p3);
12
Function Templates Overloading
template <typename T> T getMax(T x, T y) { cout<<"calling template\n"; return (x>y)?x:y; } int getMax(int i, int j) { cout<<"calling a function with int type\n"; return (i>j)?i:j; } int getMax(double d, int i) with a double type\n"; return (d>i)? (int)d:i; } const int n = 7; int main( ) { int i = 5, j = 6; double a =8.3, b = 7.8; char c1 = 'a', c2 = 'b'; cout<<getMax(i,j)<<endl; cout<<getMax(c1,c2)<<endl; cout<<getMax(a, b)<<endl; cout<<getMax(n, i)<<endl; cout<<getMax('a',1)<<endl; cout<<getMax(2.7, 4)<<endl; return 0; } 1 2 2 1 3 1 2 2 3
13
Function Templates Result: calling a function with int type 6
calling template b 8.3 7 97 calling a function with a double type 4
14
Class Templates Stack is a kind of data structure with LIFO (last in, first out) It has two principal operations: push and pop. Stack + Stack() - IsEmpty(): bool const + peek(): int + push(int&): void + pop(): int + getSize(): int const - element : int - top: int
15
Class Template(类模板) Stack::Stack() :top(0){}
bool Stack::IsEmpty() const { return top == 0; } int Stack::peek() { return element[top - 1]; } void Stack::push(int& i) { if (top < 10) element[top++] = i; else cout << "The stack is full\n"; } int Stack::pop() if (IsEmpty()) cout << "The stack is empty \n"; return element[--top]; int Stack::getSize() const { return top; } const int Size = 10; class Stack{ public: Stack(); bool IsEmpty() const; int peek(); void push(int& i); int pop(); int getSize() const; private: int element[Size]; int top; };
16
Class Template(类模板) const int Size = 10; template <typename T> class Stack{ public: Stack(); bool IsEmpty() const; T peek(); void push(T& i); T pop(); int getSize() const; private: T element[Size]; int top; }; Define a class as a template. This class is called class template. template <class T> Generic type (type parameter) The data members and the parameters and returning values of member functions in the class can be taken in a parameter.
17
Class Template(类模板) template <typename T>
T Stack<T>::pop() { if (IsEmpty()) cout << "The stack is empty \n"; return element[--top]; } Class Template(类模板) template <typename T> Stack<T>::Stack() :top(0){} bool Stack<T>::IsEmpty() const { return top == 0; } T Stack<T>::peek() { return element[top - 1]; } void Stack<T>::push(T& i) { if (top < 10) element[top++] = i; else cout << "The stack is full\n"; } int Stack<T>::getSize() const { return top;}
18
Class Template Instantiation
The process of generating a class declaration from a template class and a template argument is often called template instantiation.(由模板类和模板参数生成的类声明的过程被称为模板实例化) int main() { srand(time(0)); Stack<int> st; for (int i = 0; i < 6; i++) int a = rand() % 10; st.push(a); } cout << st.getSize() << endl; cout << st.peek() << endl; cout << st.pop() <<endl; return 0; The template is instantiated. st is a template class object(模板类的对象)
19
Example template <typename T> class A{ public: A(T);
A<T>& operator+(A<T>&); friend ostream& operator<<(ostream& out, A<T>& obj) // { out << obj.a << endl; return out; } friend istream& operator>>(istream& in, A<T>& obj) //输入流 { return in >> obj.a; }// void print(); private: T a; }; Example int main() { A<int> intA1(10), intA2(20); (intA1 + intA2).print(); cout << intA2; A<char> charA(' '), charB(' '); cin >> charA; cin >> charB; cout << charA + charB; return 0; } template <typename T> A<T>::A(T type) { a = type; } A<T>& A<T>::operator+(A<T>& obj) { a = a + obj.a; return *this; } void A<T>::print() { cout << a << endl; }
20
Summary Be able to understand generic programming
Be able to define function templates and template functions Be able to define class templates and template classes
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.