Embedded linux C + + language (ix)--Templates
generic type (Generic programming) Meaning that can be manipulated on a variety of data types. representative works of generic programming STL is an efficient, generic, interoperable software component.
generic programming was first born in In C + + , the goal is to implement STL (Standard Template Library) for C + +. The language support mechanism is the template (Templates). The spirit of the template is actually very simple: parameterized type. In other words, the type information in an algorithm or class that is originally specific to a type is pumped out and made into a template parameter T.
First,
function Templates1.
generics implemented by function overloading
#include <iostream>using namespace std;void Swap (INT&NBSP;&A,&NBSP;INT&&NBSP;B) { int t = a; a = b; b = t;} Void swap (double &a,double b) { double t = a; a = b; b = t;} Int main () { int ia = 10; int ib = 20; swap (IA,IB); cout<<ia<<ib<<endl; double da = 10, db = 20; swap (DA,DB); cout<<da<<db<<endl; return 0;}
2.
function Templates
Syntax format
Template<typename/class type parameter table >
return type function template Name ( function parameter list )
{
function template definition Body
}
3.
#include <iostream>using namespace std;template <typename t>void swap (t& a,t &b ) { T T = a; a = b; b = t;} Int main () { int ia = 10; int ib = 20; swap (Ia,ib); //swap<int> (IA,IB); cout<<ia<<ib <<endl; double da = 10, db = 20; swap (da,db); //swap<double> (da,db); cout<<da<<db<< endl; string sa = "China"; string sb = "America"; swap (SA,SB); cout<<sa<<sb<<endl; return 0;}
A function template that applies only to functions with the same number of arguments and different types, and with the same function body. If the number is different, you cannot use a function template.
second, class template
1. Definition of class template
Definition of a class template
Template<typename t>
Class Stack
{ }
Define member functions within a class
Template<typename t>
Class Stack
{
Public
Stack (int size)
{
Space = new T[size];
top = 0;
}
}
Out-of-class definition functions
Template<typename t>
void Stack<t>::p ush (T data)
{
space[top++] = data;
}
Class template is instantiated as a template class
stack<double> s (100);
A class template is an abstraction of a class that is an instance of a class template.
2.
class Template Instance
#include <iostream> #include <stdlib.h> #include <stdio.h> #include < string.h>using namespace std;template<typename t>class stack{public: stack (int size) { space = new T[size]; top = 0; } ~stack (); bool isempty (); bool isfull (); void push (T data); t Pop ();p rivate: t* space; int top;}; Template<typename t>stack<t>::~stack () { delete []space;} Template<typename t>bool stack<t>::isempty () { return top == 0;} Template<typename t>bool stack<t>::isfull () { return top == 1024;} Template<typename t>void stack<t>::p ush (t data) { space[top++ ] = data;} Template<typename t>t stack<t>::p op () { return space[--top];} Int main () { stack<double> s (+); //stack<string> s (100 ); if (!s.isfull ()) s.push (10.3); if (!s.isfull ()) s.push (; ) if (!s.isfull ()) s.push (); if (! S.isfull ()) s.push (+), if (!s.isFull ()) s.push ( while) (!s.isEmpty ()) cout<<s.pop () <<endl; return 0;}
This article from "Endless life, Struggle not only" blog, reprint please contact the author!
Embedded Linux C + + language (ix)--templates