?a???s?as? t?? PowerPoint - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

?a???s?as? t?? PowerPoint

Description:

return amount * pow(1 p / 100, n) ?? ?a??? p????a a de? e?? e???eta? ??t? ??a t? ?????. ... logic_error description('illegal parameter'); throw description; ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 21
Provided by: UserS58
Category:

less

Transcript and Presenter's Notes

Title: ?a???s?as? t?? PowerPoint


1
Templates Standard Template Library (STL) Excep
tions
?etapt???a?? ?????aµµa Sp??d??, ?µ?µa
?fa?µ?sµ???? ?????f??????
2
Templates
  • ?a p??t?pa (templates) e??a? µ?a ap? t?? p????
    ?s????? d??at?t?te? t?? C (Bjarne Stroustrup,
    1988)
  • ?a templates µa? ep?t??p??? ?a ?a????s??µe, µ?sa
    se ??a µ??ad??? tµ?µa ??d??a, ??a s?????
    s?s?et???µe??? s??a?t?se?? (template functions)
  • ?p????µe ??a pa??de??µa ?a ??????µe ??a µ??ad???
    p??t?p? s????t?s?? ??a µ?a s????t?s? ta????µ?s??
    e??? p??a?a ?a? st? s????e?a ?a af?s??µe st? C
    ?a pa???e? ?e????st?? s??a?t?se?? ??a t??
    ta????µ?s? e??? p??a?a a?e?a???, p??a?a a???µ??
    ????t?? ?p?d?ast????, p??a?a a?fa???µ?t???? ??p
  • ?p?p???? µp????µe ?a ???s??µe p??t?pa ???se??
    (class templates)
  • ???????a function templates ? (pa??????)
    template functions
  • class templates ? (pa??????) template
    classes

3
Templates
  • ?st? µ?a s????t?s? printArray(int T, int size)
  • ?? ?????µe ?a e?t?p?s??µe ??a? p??a?a µe
    st???e?a p?? e??a? a???µ?? ????t?? ?p?d?ast????,
    p??pe? ?a "?pe?f??t?s??µe" t? ???µa printArray
    printArray(double T, int size)
  • ?pe?f??t?µ??e? s??a?t?se?? ???s?µ?p?????ta? ??a
    t?? e?t??es? pa??µ???? ?e?t??????? se
    d?af??et????? t?p??? ded?µ????
  • ?? ?? ?e?t?????e? e??a? pa??µ???t?pe? ??a ???e
    t?p?, e??a? ?ata?????te?? ? ???s? p??t?p??
    s??a?t?se??
  • ? p????aµµat?st?? ???fe? ??a µ??ad??? p??t?p?
    s????t?s??
  • ??se? t?? t?p?? t?? ???sµ?t??, ? µeta???tt?st??
    pa???e? ?e????st?? s??a?t?se?? a?t??e?µe???
    ??d??a ??a t? ?e???sµ? ???e ???s??

4
Function Templates
  • ??a ta p??t?pa s??a?t?se?? ?e?????? µe t?
    desµe?µ??? ???? template a????????µe?? ap? µ?a
    ??sta pa?aµ?t??? µ?sa se lt gt
  • Se ???e pa??µet?? p??pe? ?a p????e?ta? e?te ?
    desµe?µ??? ???? class e?te ? typename p.?.

template ltclass Tgt T maximum( T value1, T value2,
T value3) T max value1 if(value2 gt max)
max value2 if(value3 gt max) max
value3 return max
5
Class Templates
  • St??ßa (stack) ??µ? ded?µ???? ?p?? ta ded?µ??a
    e?s????ta? p??t?te ?at? t?? ?d?a se??? ?a?
    a?a?t???ta? µe se??? LIFO
  • ? ???p???s? µ?a? st??ßa? e?a?t?ta? ap? t?? t?p?
    t?? ded?µ????
  • ?pa?a???s?µ?p???s? ???sµ?? t?? ?e????? ?????a?
    µ?a? st??ßa? µe p??t?pa ???se??

6
Class Templates
template ltclass Tgt class Stack public
Stack(int s) bool push(T) T
pop() private int size int top T
stackPtr bool isEmpty() return top -1
bool isFull() return top size-1
7
Class Templates
template ltclass Tgt Stacklt T gtStack(int s)
size s gt 0 ? s 10 top -1 stackPtr
new Tsize template ltclass Tgt bool
StackltTgtpush(T pushValue) if(!isFull())
stackPtrtop pushValue return
true else return false
8
Class Templates
template ltclass Tgt T StackltTgtpop() if(!isEmpt
y()) return stackPtrtop-- else cout ltlt
"Nothing to retrieve" ltlt endl
9
STL
  • ? C pe???aµß??e? t?? p??t?p? ß?ß??????? STL µe
    ????? s??p? ?a d?e??????e? t?? epa?a???s?µ?p???s?
    ??d??a
  • Alexander Stepanov, Meng Lee (Hewlett Packard
    generic programming a?e?a?t?s?a d?µ?? e??????
    ap? ?p??e?µe?a data)
  • ????? t?? p??t?p?? C
  • ????a s?stat???
  • ?p?d??e?? (containers)
  • epa?a??pte? (iterators)
  • a??????µ?? (algorithms)
  • ? ß?ß??????? STL e??a? p??? µe???? !!
  • ????t?? ? d?µ??????a p??t?p?? d?µ?? ded?µ????
    p?? ?a ?e?t??????? e????a µe ???a p?????µµata

10
STL
  • St?? C/C ? p??sßas? sta st???e?a µ?a? d?µ??
    (p.?. p??a?a) ???eta? µe t? ???s? pointers
  • St?? STL ? p??sßas? st??? ?p?d??e?? ???eta? µ?s?
    epa?a??pt??
  • ?d?a "a?s??s?" µe t??? de??te?
  • "e??p??te??" s?µpe??f???
  • ??? ?? ?p?d??e?? e?s?µat????? st???e??de??
    ?e?t?????e?, p?? e?e??t?µ???? a??????µ??
    ???p?????ta? ?e????st?
  • H STL ap?fe??e? t? ???s? t?? new ?a? delete a???
    ???s?µ?p??e? ?ata?eµ?t?? (allocators) µ??µ??

11
Containers
  • ?a?ade??µata
  • vector - ta?e?a p??s???? ?a? d?a??af? st? t????
  • - ?µes? p??ßas? se ???e st???e??
  • list - d?p?? s??dedeµ??? ??sta, ta?e?a p??s????
    ?a? d?a??af? ?p??d?p?te
  • set - ta?e?a e??es?, de? ep?t??p??ta? d?p???
    ?ata????se??
  • multiset - ta?e?a e??es?, ep?t??p??ta? d?p???
    ?ata????se??
  • stack - last-in-first-out (LIFO)
  • queue - first-in-first-out (FIFO)

12
Containers
  • ?p?????? p????? ?e?t?????e? p?? efa?µ????ta? se
    p?????? ?p?d??e??
  • empty - ep?st??fe? true a? de? ?p?????? st???e?a
  • size - ep?st??fe? t?? t?????ta a???µ? t??
    st???e???
  • swap - e?a??a?? t?? ??s?? d?? st???e???
  • begin - ep?st??fe? epa?a??pt? p??? t? p??t?
    st???e??
  • erase - d?a??af? e??? ? pe??ss?t???? st???e???
  • operator - ????es? e??? ?p?d???a se ??a? ????
  • operator lt - ?p?st??fe? true a? ??a? ?p?d???a?
    e??a? µ????te??? ap? ??a? ????

13
Iterators
  • ?a???? p?? ?? epa?a??pte? e??a? ???p???µ????
    a?????a µe t? d?µ? p??? t?? ?p??a "de??????",
    µ???????ta? ???sµ??e? ?????? ?e?t?????e?
  • - te?est?? ?µµes?? a?af????
  • - µeta????s? t?? epa?a??pt? st? ep?µe??
    st???e??
  • ???s?µ?p????µe ??a a?t??e?µe?? iterator ??a ?a
    a?afe????µe se st???e?a ta ?p??a µp????? ?a
    t??p?p???????.
  • ???s?µ?p????µe ??a a?t??e?µe?? const_iterator ??a
    ?a a?afe????µe se st???e?a ta ?p??a de? µp?????
    ?a t??p?p???????

14
Vectors
  • ??a d?µ? vector e??a? ??a? "e??p??te???" p??a?a?
    (s??e?e?? ??se?? µ??µ??)
  • t? µ??e??? µp??e? ?a a????e? d??aµ???
  • ??a vector µp??e? ?a a?ate?e? se ??a ????
  • ? p??s???? st???e??? st? t???? e??a? ap?d?t???
  • ? p??s???? st???e??? st? µ?s? e??a? ?p?????st???
    dapa????
  • ?a st???e?a µp????? ?a p??spe?ast??? µ?s? t??
    ?pe?f??t?µ???? te?est?

15
Algorithms
  • ????? t?? STL ?? ß?ß??????e? ???se?? µeta??
    d?af???? ?atas?e?ast?? ?ta? as?µßate?
  • ?????? ???s?µ?p????sa? ???????µ???t?ta ?a?
    p???µ??f?sµ? µe t? s?et??? ??st?? ta??t?ta? t??
    virtual s??a?t?se??
  • ?? a??????µ?? ?ta? e?s?µat?µ???? st?? d?µ??
  • St?? STL ?p?d??e?? ?a? a??????µ?? d?a???????ta?
  • ?a??de??µa ???s? ßas???? a??????µ?? ta????µ?s??

16
?e???sµ?? ??a???se??
  • ?st? ?t? ??a? p????aµµat?st?? ???e? ??p???
    sf??µa, ?p?? t? ?a ??t?se? ??a st???e?? µet? t?
    t???? e??? d?a??sµat?? se µ?a ???s?
  • ? s????af?a? t?? ???s?? ??e? t?? e???
    d??at?t?te?
  • ?a µ?? e????e? t? s?????? t?? sf??µat?? ?a? ?a
    e?te??se? t?? ??d??a (p??a??? µe s?ßa???
    s???pe?e?)
  • ?a e????e? t? s?????? t?? sf??µat?? ?a? ?a µ??
    ???e? t?p?ta a? s?µße?
  • ?a e????e? t? s?????? t?? sf??µat?? ?a? ?a
    e??µe??se? t? ?a??? p????aµµa (µ???d?) a? s?µße?
  • double future_val(double amount, double p, int n)
  • return amount pow(1 p / 100, n)

?? ????e? ?? future_val(1000, -100, -1) ??
17
?e???sµ?? ??a???se??
  • ???a? d??at?? ?a ???p????e? ??d??a? e??????

double future_val(double amount, double p, int
n) if (p lt 0 n lt 0) return 0 return
amount pow(1 p / 100, n)
  • ?? ?a??? p????aµµa de? e??µe???eta? ??t? ??a t?
    ?????. ?? e???ße? t? 0 ?? ?a?????? t?µ? ?a?
    s??e??se? µp??e? ?a p????????? pe?a?t???
    p??ß??µata

18
?e???sµ?? ??a???se??
  • ? C d?a??te? ??a µ??a??sµ? e?d?p???s??, p??
    ?a?e?ta? ?e???sµ?? e?a???se?? (exception
    handling)
  • ?ta? µ?a s????t?s? e?t?p??e? ??a sf??µa,
    p???a?e? µ?a e?a??es? (throws an exception)

double future_val(double amount, double p, int
n) if (p lt 0 n lt 0) logic_error
description("illegal parameter") throw
description return amount pow(1 p /
100, n)
19
?e???sµ?? ??a???se??
  • ? desµe?µ??? ???? throw ?p?d????e? t?? ???d? ap?
    t? s????t?s?
  • Ost?s?, ? s????t?s? de? ep?st??fe? ?p???e?t???
    st?? ?a???sa
  • ?????e? t?? ?a???sa s????t?s?, a?t? p?? t??
    ???ese ?.?.?. µ????? ?t?? ß?e?e? ??a? ?at???????
    ?e???st?? (handler)

20
?e???sµ?? ??a???se??
  • ?a???ete ??a? ?e???st? e?a???se?? µe t??
    a??????? ??d??a

try code catch (logic_error e)
handler
Write a Comment
User Comments (0)
About PowerShow.com