C9 templates

Click here to load reader

  • date post

    11-Apr-2017
  • Category

    Documents

  • view

    189
  • download

    1

Embed Size (px)

Transcript of C9 templates

  • K thut lp trnhPhn III: Lp trnh tng qut

    010101010101010110000101010101010101011000010101010101010101100001010101010010101010010101010101001010101001010101010100101010100101101001100011001001001010100110001100100100101010011000110010010010110010110010001000001011001011001000100000101100101100100010000010010101010101010110000101010101010101011000010101010101010101100001010101010010101010010101010101001010101001010101010100101010100101101001100011001001001010100110001100100100101010011000110010010010110010110010001000001011001011001000100000101100101100100010000010010101010101010110000101010101010101011000010101010101010101100001010101010010101010010101010101001010101001010101010100101010100101101001100011001001001010100110001100100100101010011000110010010010110010110010001000001011001011001000100000101100101100100010000010

    y = A*x + B*u;x = C*x + d*u;

    StateControllerstart()stop()

    LQGControllerstart()stop()

    Chng 9: Khun mu hm v khun mu lp

    12/25/2007

  • 2Chng 9: Khun mu hm v khun mu lp

    Ni dung chng 9

    9.1 Khun mu hm- Vai tr ca khun mu hm- nh ngha khun mu hm- S dng khun mu hm

    9.2 Khun mu lp- nh ngha khun mu lp- Dn xut khun mu lp- V d khun mu lp Vector

  • 3Chng 9: Khun mu hm v khun mu lp

    9.1 Khun mu hm (function template)

    Vn : Nhiu hm ch khc nhau v kiu d liu tham s p dng, khng khc nhau v thut tonV d: int max(int a, int b) {

    return (a > b)? a : b; }double max(double a, double b) {

    return (a > b)? a : b; }...

    Cc v d khc: cc hm swap, sort, find, select,...Bn cht ca vn ? Nm ngn ng lp trnh cn thp, cha gn vi t duy ca con ngi!Gii php: Tng qut ha cc hm ch khc nhau v kiu dliu p dng thnh khun mu hm.

  • 4Chng 9: Khun mu hm v khun mu lp

    nh ngha khun mu hm

    V d tng qut ha hm max c th p dng cho nhiu kiud liu khc nhau:template T max(T a, T b) {

    return (a > b)? a : b; }

    V d tng qut ha hm swap:template void (X& a, X& b) {

    X temp = a;a = b;b = temp;

    }

    Mt khun mu hm inline:template inline T max(T a, T b) { return (a > b)? a : b;}

    S dng t kha typenamehoc class khai bo thams khun mu

  • 5Chng 9: Khun mu hm v khun mu lp

    Khai bo v s dng khun mu hm

    V d s dng khun mu hm maxtemplate T max(T a, T b);template void swap(T&, T&);

    void main() {int N1 = 5, N2 = 7;double D1 = 5.0, D2 = 7.0;int N = max(N1,N2); // max(int,int)char c = max('c','a'); // max(char, char)double D = max(D1,D2); // max(double, double)swap(N1,N2); // swap(int&,int&)swap(D1,D2); // swap(double&,double&)D = max(D1,A1); // error: ambiguousN = max('c',A1); // error: ambiguousD = max(D1,A1);// OK: explicit qualificationN = max('c',A); // OK: explicit qualification

    }

    Khun mu hm

    Hm khun mu

  • 6Chng 9: Khun mu hm v khun mu lp

    Kh nng p dng khun mu hm

    Kh nng p dng mt khun mu hm l v tn, nhng khngphi p dng c cho tt c cc i s khun muV d: iu kin rng buc i vi kiu d liu c th p dngtrong khun mu hm max l phi c php so snh ln hn (>):template inline T max(T a, T b) { return (a > b)? a : b;}

    => i vi cc kiu d liu mi, mun p dng c th cn phinp chng ton t so snh >Tuy nhin, kh nng p dng c cha chc c nghaV d: Xc nh chui k t ng sau trong hai chui cho trctheo vn ABCchar city1[] = "Ha Noi", city2[] = "Hai Phong";char* city = max(city1,city2); // ???

    // max(char*,char*)

  • 7Chng 9: Khun mu hm v khun mu lp

    Np chng khun mu hmMt khun mu hm c th c np chng bng hm cng tn...char* max(char* a, char* b) { if (strcmp(a,b))... }void f() {

    char c = max('H','K'); // max(char,char)

    char city1[] = "Ha Noi", city2[] = "Hai Phong";

    char* city = max(city1,city2); // max(char*,char*)

    ...

    }

    ...hoc bng mt khun mu hm cng tn (khc s lng cctham s hoc kiu ca t nht mt tham s), v d:template T max(T a, T b, T c) {...}template T max(T* a, int n) {...}

    nhng khng c nh th ny:template X max(X a, X b) {...}

  • 8Chng 9: Khun mu hm v khun mu lp

    Tham s khun muTham s khun mu hm c th l mt kiu c bn hoc mtkiu dn xut, nhng khng th l mt bin hoc mt hng s:template max(T a, T b) { ... } // OKtemplate max(int* a) { ... } // error

    Mt khun mu hm c th c hn mt tham s kiu:template void swap(A& a, B& b) {

    A t = a;a = b; // valid as long as B is compatible to Ab = t; // valid as long as A is compatible to B

    }void f() {

    double a = 2.0;int b = 3;swap(a,b); // swap(double&,int&)swap(b,a); // swap

  • 9Chng 9: Khun mu hm v khun mu lp

    Thng thng, tham s khun mu xut hin t nht mt ln lkiu hoc kiu dn xut trc tip ca cc tham bin:template void f1(X a, int b) {...}template void f2(X* b) {...}template void f3(Y& a, X b) {...}

    Theo chun ANSI/ISO C++, tham s khun mu khng bt bucphi xut hin trong danh sch tham bin, nhng cn lu khis dng. V d#include template X* array_alloc(int nelem) {

    return (X*) malloc(nelem*sizeof(X));} void main() {

    double* p1 = array_alloc(5); // error!double* p2 = array_alloc(5); // OK!...free(p2);

    }

  • 10Chng 9: Khun mu hm v khun mu lp

    Khun mu hm v hm khun mu

    Khun mu hm ch a ra cch thc thc hin v s dng mtthut ton no mt cch tng qutTrong khi bin dch khun mu hm, compiler ch kim tra vc php, khng dch sang m chM hm khun mu c compiler to ra (da trn khun muhm) khi v ch khi khun mu hm c s dng vi kiu cthNu mt khun mu hm c s dng nhiu ln vi cc kiukhc nhau, nhiu hm khun mu s c to ra tng ngNu mt khun mu hm c s dng nhiu ln vi cc kiutng ng ging nhau, compiler ch to ra mt hm khun mu.

  • 11Chng 9: Khun mu hm v khun mu lp

    u im ca khun mu hm

    Tit kim c m ngun => d bao qut, d kim sot li, nngcao hiu qu lp trnhm bo c tnh cht ch v kim tra kiu mnh trong ngnng lp trnh (hn hn s dng macro trong C)Tnh m, nng cao gi tr s dng li ca phn mm: thut tonvit mt ln, s dng v s lnm bo hiu sut tng ng nh vit tch thnh tng hmring bitCho php xy dng cc th vin chun rt mnh (cc thut tonthng dng nh sao chp, tm kim, sp xp, la chn, ....)

  • 12Chng 9: Khun mu hm v khun mu lp

    Nhc im ca khun mu hmNu mun m bo tnh m hon ton th ngi s dng khunmu hm cng phi c m ngun thc thi M ngun thc thi cn c t trong header file Kh bo v cht xm

    Vic theo di, tm li bin dch nhiu khi gp kh khn Li nhiu khi nm m s dng, nhng li c bo trong m nh

    ngha khun mu hm V d: Compiler khng bo li dng lnh sau y, m bo li

    phn nh ngha hm max, ti php ton so snh ln hn khngc nh ngha cho kiu Complex:Complex a, b;

    ... Complex c = max(a,b);

    nh ngha v s dng khng ng cch c th dn ti gia tngln v m ch, bi s lng hm khun mu c th c to raqu nhiu khng cn thit.

  • 13Chng 9: Khun mu hm v khun mu lp

    V d: khun mu hm copy

    template

    void copy(const S * s, D* d, int n) {

    while (n--)

    *d++ = *s++;

    }

    void main() {

    int a[] = {1,2,3,4,5,6,7};

    double b[10];

    float c[5];

    copy(a,b,7); // copy(a,b,7)

    copy(b,c,5); // copy(b,c,5);

    ...

    }

  • 14Chng 9: Khun mu hm v khun mu lp

    9.2 Khun mu lp (class template)

    Nhiu cu trc d liu nh Point, Complex, Vector, List, Map,... trc kia vn phi c nh ngha ring cho tng kiu d liu phn t c th, v d DoubleComplex, FloatComplex, DoubleVector, IntVector, ComplexVector, DateList, MessageList, ...Cch thc hin mi cu trc thc ra ging nhau, ni chung khng ph thuc vo kiu phn t c thclass IntPoint { int x,y;

    public: IntPoint(int x0, int y0) : x(x0), y(y0) {}

    ...

    };

    class DoublePoint { double x,y;

    public: DoublePoint(double x0, double y0) : x(x0), y(y0) {}

    ...

    };

  • 15Chng 9: Khun mu hm v khun mu lp

    nh ngha khun mu lp// Point.htemplate class Point {

    T x, y;public:

    Point(): x(0), y(0) {} Point(T x0, T y0) : x(x0), y(y0) {}Point(const Point&);void move(T dx, T dy) { x += dx; y+= dy; }bool inRect(Point p1, Point p2);//...

    };template Point::Point(const Point& b) : x(b.x), y(b.y) {}

    template bool Point::inRect(Point p1, Point p2) {

    return (x >= p1.x && x = p2.x && x = p1.y && y = p2.y && x

  • 16Chng 9: Khun mu hm v khun mu lp

    S dng khun mu lp: Lp khun mu#include "Point.h"void main() {

    Point A1(5,5),A2(10,10);Point A3(A1);while (A3.inRect(A1,A2))

    A3.move(2,3);typedef Point FPoint;FPoint B1(5.0,5.0), B2(10.0,10.0);FPoint B3(B1);while (B3.inRect(B1,B2))

    B3.move(2,3);//...Point C1(B1); // errorif (A3.inRect(B1,B2)) // error

    ; //...}

  • 17Chng 9: Khun mu hm v khun mu lp

    Nhng kiu no c th p dng?Kh nng p dng ca kiu l v tn, tuy nhin khng c nghal p dng c cho tt c cc kiuMt kiu mun p dng c phi h tr cc php ton c sdng trong m thc thi khun mu lp.V d khun mu lp Point yu cu kiu ta phi p dngc cc php ton sau y: Chuyn i t s nguyn (trong hm to mc nh) Sao chp (trong hm to th hai v hm to bn sao) Ton t += (trong hm move) Cc php so snh >=,

  • 18Chng 9: Khun mu hm v khun mu lp

    Tham s khun mu: kiu hoc hng stemplate class Array {

    T data[N];public:

    Array(const T& x = T(0));int size() const { return N; }T operator[](int i) const { return data[i]; }T& operator[](int i) { return data[i]; }//...

    };template Array::Array(const T& x) {

    for (int i=0; i < N; ++ i) data[i] = x;}void main() {

    Array a;Array b; // same as aboveArray c; // same as above//...

    }

    Tham s mc nh

  • 19Chng 9: Khun mu hm v khun mu lp

    Dn xut t khun mu lptemplate class IOBuffer : public Array {public:

    IOBuffer(T x) : Array(x) {}//...

    };class DigitalIO : public IOBuffer {public:

    DigitalIO(bool x) : IOBuffer(x) {}//...

    };class AnalogIO : public IOBuffer {

    typedef IOBuffer BaseClass;public:

    AnalogIO(unsigned short x) : BaseClass(x) {}//...

    };void main() {

    IOBuffer delayBuf(0);DigitalIO di(false);AnalogIO ao(0