List L=1; - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

List L=1;

Description:

. . - ++. 8 ... – PowerPoint PPT presentation

Number of Views:16
Avg rating:3.0/5.0
Slides: 11
Provided by: 11284
Category:
Tags: list

less

Transcript and Presenter's Notes

Title: List L=1;


1
????????-??????????????? ????????????????
?????? ?.?.
  • ?. ?????? 8

2
???????
  • ?????? ???????? ?????? ????????? ?????????? ????.
    ????????, ?????? ????? ?????. ?????????? ?????
    ????????? ???????? ???

class List int val List next public
void add(List e) void add(int n) void
print(void) List(int v0) val v0 next
NULL
void List add(int n) List t new
List(n) add(t) void Listadd(List e)
List t for(tthist-gtnexttt-gtnext)
t-gtnext e void Listprint(void)
for(List tthisttt-gtnext) printf("d ",
t-gtval)
?????? ?????? ?? ??????? List L1 L.add(List(2)
) List pl new List(3) L.add(pl) L.add(4) L.
print()
3
??????? ???????
  • ??????? ? ?????? ?.??????????? "Parameterized
    Types for C", 1988.
  • ??????????? ???????
  • lttemplategt lt?????? ??????????gt ??????????
  • ?????????? ????????? ??????? ??? ?????.
    ?????????? ????? ???? ?????? ??????????.
  • template ltclass Tgt class Vector
  • T v
  • int sz
  • public
  • Vector(int n) sz n v new Tsz
  • void sort(void)
  • T operator(int i) return vi
  • Vector ltintgt v1(20)
  • Vector ltComplexgt v2(10)
  • typedef Vector ltComplexgt CVector
  • ?????? ? ?????? ?????????? ?????? ??????? ??? ??,
    ??? ? ?????? ? ?????? ??????.

4
??????? ???????-??????
  • template ltclass Tgt TVectorltTgtoperator(int i)
    ...
  • template ltclass Tgt void VectorltTgtsort(void)
    ...
  • ??? ????????????? ????????? ??????? ?? ??????????
    ??????? ?????? ?? ??????????? ?? ?????????? ??
    ?????? ??????? ?? ????.
  • Vector ltComplexgt cv(100)
  • Vector ltintgt ci(100)
  • f(cv) //f(vectorltComplexgt)
  • f(ci) //f(vectorltintgt)

5
??????? ???????
  • template ltclass Tgt T max(T a, T b)
  • return agtb?ab
  • int a, b
  • char c, d
  • int m1 max(a,b) //max(int, int)
  • char m2 max(c,d) //max(char, char)
  • int m3 max(a,c) // - ?????? ??????
    ????????????? max(int, char)

6
???????????
  • ?????????, ??? ?????? ???????? ???????, ????????
    ? ?????? ??????????, ?????? ???? ??????????? ?
    ????? ?????????? ??????? ???????.
  • template ltclass Tgt T create() // ?????? (??)
  • template ltclass Tgt void f()
  • T a
  • // ?????? (??)
  • template ltclass Tgt class creator
  • static T create()
  • int creatorltintgtcreate()

7
??????
  • struct S
  • int a, b
  • template ltclass Tgt T create()
  • T a
  • a new T
  • return a
  • template ltclass Tgt class Ccreator
  • public
  • static T create()
  • int Ccreatorltintgtcreate()
  • int n
  • n new int
  • n 123
  • return n

void main(void) int t S s t
createltintgt() s createltSgt() t
Ccreatorltintgtcreate()
8
???????
  • template ltclass Tgt class TList
  • public
  • T val
  • TList next
  • void add(TList e)
  • void add(T n)
  • TList t new TList(n) add(t)
  • void print(void)
  • TList(T v0) val v0 next NULL
  • template ltclass Tgt void TListltTgtadd(TList e)
  • TList t
  • for(tthist-gtnexttt-gtnext)
  • t-gtnext e
  • template ltclass Tgt void TListltTgtprint(void)
  • for(TList tthisttt-gtnext)

template ltclass Tgt void print(T e) printf("d
", e.val) // ?????? ?? ??????? ?????? ???? //
??????? ?????????? ?????? ?? ????? // ? ????
????????? template ltclass Tgt void print() T
x printf("d ", 1) class C int n,
n2 public C(int k0) nk n2n-1
9
???????????
  • void main(void)
  • TListltintgt tl1
  • tl.add(TListltintgt(2))
  • TListltintgt tpl new TListltintgt(3)
  • tl.add(tpl)
  • tl.add(4)
  • tl.print()
  • typedef TListltchargt CList
  • CList cl1
  • cl.add(CList(2))
  • CList cpl new CList(3)
  • cl.add(cpl)
  • cl.add(4)
  • cl.print()
  • TList ltCgt ctpl C(31)
  • ctpl.add(32)
  • ctpl.print()
  • print(ctpl)

10
????????? ??????????
  • ?????????? ???????? ????? ???? ?????????
  • template ltclass A, class Bgt void print(A e, B x)
    printf("d d", e.val, x)
  • template ltclass T1, class T2gt class L12
  • T1 t1
  • T2 t2
  • ...
  • print(ctpl, 1)
  • L12ltint, intgt s
Write a Comment
User Comments (0)
About PowerShow.com