Embedded Linux C + + language (ix)--templates

Source: Internet
Author: User

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&gt::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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.