C+ + In- De pth C (Bja rne Stroustrup), I have made this letter longer than usual, because I lack the time to make it short. ISO/ANSI C++ C++., C++. C++,, C++ In-Depth.,..,., The C++ Programming Language. C++,. C++,, C + + In- De pth..
2 Ess ential C+ + Accelerated C++ : C++ - Accelerated C++: Practical Programming by Example Exceptional C++ : C++ 47 - Exceptional C++: 47 Engineering Puzzles, Programming Problems, and Solutions C++ Network Programming Vol.1 : ACE - C++ Network Programming, Vol.1: Mastering Complexity with ACE and Patterns Modern C++ Design - Modern C++ Design: Generic Programming and Design Patterns Applied C++ Network Programming Vol 2 : ACE - C++ Network Programming, Volume 2: Systematic Reuse with ACE and Frameworks C++ - Essential C++ Applied C++ - - Applied C++: Practical Design and Implementation Techniques More Exceptional C++: 40 Engineering Puzzles, Programming Problems, and Solutions The Boost Graph Library User Guide and Reference Manual
C.?..?? C++ 10. ',. ' 1998, ANSI C++. C++..., C++., (Bjarne Stroustrup) (generic programming),. C++ C++, '., C++, '. C + + In De pth C++ C++.,,,
4 Ess ential C+ +,. C++,, C++,. Essential C++ (Stanley Lippman) C++ Primer.,, C++ ( ). C++ Primer C++, 3/E(1999 ). STL. OOP,, C++ 1000,. AT&T C++ cpront C++, ( ) C++ ( C++ )., C++ Primer Distilled Essential C++. C++ Primer. C++ Accelerated C++,.,., (^^;) C++ Primer 3/E. (, ).
(kwak101@hitel.net / http://www.kwak101.pe.kr),.,. / C++(More Effective C++ ), STL(Effective STL ), (Web Development with JavaServer Pages ), UML (Teach Yourself UML in 24 hours ).
C C++ Primer. (Bjarne Stroustrup) The C++ Programming Language C++. C++ Primer (Stanley Lippman) 1000 C++ Essential C++.,., C Java, Perl C++. C++. C++,...,.. (a r t i e@a r t i e. pe. kr) (ht t p :/ / a r t i e. pe. kr / book)..,..,. Accelerated C++ : C++. [1], [2].
8 Ess ential C+ +.,,..,...,.,.....,.,... Work! Work! Work! :) (artie@artie.pe.kr / http://artie.pe.kr), ( ) 2 :. ( ),.,,. < >.
C!. C++ Primer, 1237. 360... (Disney Feature Animation).,....,.,..,. 2000(Fantasia 2000) (Firebird). (raw Disney camera information). C++,... (Jinko Chyuan ) Perl., TD Perl Tcl (TD, (Technical Director). TD, TD[Mira] TD[Tim], [Mike, Steve, Tonya] ). Perl (Paul, Gaetan) (Dave) (Peter).,...
20 Ess ential C+ +. C++., Perl...,.., (plug-in)., 48. Perl.,. (Randal Schwartz) Learning Perl.,., Perl. C++ Primer 3 C++. C++ Primer., C++ Primer., (Josée Lajoie) 3. C++. Essential C++. Perl C++.. Learning Perl, C++..? 1. (Level of detail) (detail)., 5,...,. C++ Primer (Josée ). 46, 2.
21 2. (Core language) C++ Report..,... (nested type).,.,..,. 3. C++ Primer 6., C++ Primer. A. (Deborah Lafferty).. 7 2. 1., string,, iostream. string C. 2, C++. (inline), (overloading), (template),. 3 (STL). (vector) (list), (set), (map) sort (), copy ( ), me r ge ( ) (generic algorithms). B.
22 Ess ential C+ + C++. 4 C++., (Dreamworks Animation) 4. 5., 8, (hierarchy) (dynamic binding). 6..,. (buffer). (binary tree)., 7 C++,. A, B. Addison Wesley Longman (www. awl. com/ c se ng/ t i t l es/ 0-201- 48518-4) (www.obj ect wr i t e. com). Visual C++ 5.0 Visual C++ 6.0.. (s l i ppma n@obj ect wr i t e. com) (, ). C++ Primer 3 (Josée Lajoie).., (Dave Slayton). (Steve Vinoski).
23 Addison-Wesley. (Deborah Lafferty), (Betsy Hardinger), (John Fuller).. Essential C++. (Colin Lipworth), (Edwin Leonard), (Kenneth Meyer)., C++ (Lippman) (Lajoie) C++ Primer 3 (Stroustrup) The C++ Programming Languag e 3. ( C++ Primer The C++ Programming Language ). [LIPPMAN98] Lippman, Stanley, and Josée Lajoie, C++ Primer, 3rd Edition, Addison Wesley Longman, Inc., Reading, MA (1998) ISBN 0-201-82470-1. [LIPPMAN96a] Lippman, Stanley, Inside the C++ Object Model, Addison Wesley Longman, Inc., Reading, MA (1996) ISBN 0-201-83454-5. [LIPPMAN96b] Lippman, Stanley, Editor, C++ Gems, a SIGS Books imprint, Cambridge University Press, Cambridge, England (1996) ISBN 0-13570581-9. [STROUSTRUP97] Stroustrup, Bjarne, The C++ Programming Language, 3rd Edition, Addison Wesley Longman, Inc., Reading, MA (1997) ISBN 0-201-88954-4. [SUTTER99] Sutter, Herb, Exceptional C++, Addison Wesley Longman, Inc., Reading, MA (2000) ISBN 0-201-61562-2.
Cha pte r 1 C++. C++. 1. : (boolean),,, 2.,,., (assignment),, (compound assignment). 3. (if ) (while ) 4. (array) (compound type).,. 5. (string) (vector) (standard library)
26 Ess ential C+ + 1.1 C+ +.,.? C++. mai n( ). (function). mai n( ) i nt mai n( ) { / /. } int C++ (keyword).,. int (built-in) ( ).. (return type),, (parameter), (body)... mai n( ). mai n( )., mai n( ) 0, 0.,., mi n( ) so r t ( ). f ( ) g( ).,. mai n C++. C++ mai n( )..
Chapter 1 C+ + 27,. mai n( ). (,), ( (call, invoke) )., mi n( ). mi n( ). i nt mi n( i nt val 1, i nt val 2 ) { / /... } ({}).. (//).,... C++. (object-oriented class hierarchy), C++, C++ (standard library). (class).. ( ). C++ (boolean),,,.,.,..,....
28 Ess ential C+ + 7. 6 2 x, y, z.., 7. (type abstraction) (layer)., Point3d., Point3d., 7. 7... (header file).. C++ iostream.. iostream. #i ncl ude <i ost r eam> cout (see out ). (<<) cout. cout << "Pl eas e e nt e r your f i r s t name : " ; C++ (statement) C++.. (;). ( ).,.. Pl ea se e nt e r your f i r st name :..
Chapter 1 C+ + 29. i nt. i nt., string( ). st r i ng use r_ name ; use r_ name string. (declaration statement). string. string. #i ncl ude <st r i ng> ci n(see in ). (>>) ci n. ci n >> us e r_name ; ( ). Pl ea se e nt e r your f i r st name : a nna.. Hel l o, a nna... a nd goodbye!,... cout (newline) (literal). cout << ' \ n ' ;.. (' a ', ' A' ), (' ; ', ' - ' ) (' \ n ' ) (' \ t ' )..
30 Ess ential C+ +, Hel l o. cout << "Hel l o, " ;. use r_ name.,?. cout << us e r_name ;, ( ). cout << "... a nd goodbye!\ n" ;,.,.. cout << "3 + 4 = " ; cout << 3 + 4 ; cout << ' \ n ' ;. 3 + 4 = 7 (instance)[1] ( 4 )..,. cout << ' \ n ' << "Hel l o, " << use r_ name << "... and goodbye!\ n" ;, mai n( ) return. r et ur n 0 ;
Chapter 1 C+ + 31 r et ur n C++. return, 0. mai n( ) 0. 1) C++. #i ncl ude <i ost r eam> #i ncl ude <st r i ng> us i ng name s pace s t d ; / /... i nt mai n( ) { st r i ng use r_ name ; cout << " Pl ea se e nt e r your f i r st name : " ; ci n >> us e r_name ; cout << ' \ n ' << "Hel l o, " << use r_ name << "... and goodbye!\ n" ; r et ur n 0 ; } ( ). Pl ea se e nt e r your f i r st name : a nna Hel l o, a nna... a nd goodbye!. us i ng name s pace s t d ; ( ). usi ng names pace C++. std (namespace)[2]. (string iostream cout ci n ) std.,. (name clash) (. 1) mai n( ) r et ur n 0 r et ur n r et ur n. r et ur n.
32 Essential C+ +. )., string iostream ci n cout string iostream, s t d. us i ng name s pace st d ; us i ng ( [LIPPMAN98] 8.5 [STROUSTRUP97] 8.2 ). 1.1 mai n( ).,... 1.2 string. / / #i ncl ude <s t r i ng>.? string. / / us i ng name s pace st d ;? 1.3 mai n() my_mai n().?
Chapter 1 C+ + 33 1.4 (2).. (1). 1.2 (d a ta o bje c t) (d e finition),... The val ues 2, 3 f o rm t wo cons ec ut i ve el ement s of a nume r i cal s eque nce. What i s t he next val ue? (Fibonacci) (1, 1, 2, 3, 5, 8, 13,...). 1. (2 ). 5..... string,,. (data object)[3]., (_).., us e r_name, Us e r_name, user_name, use r_name.., name_1 1_name., C++ (keyword). del et e (string del et e ( ) e r as e ( ) ).
34 Essential C+ +... use r_ name.. #i ncl ude <st r i ng> st r i ng use r_ name ;. C++ (boolean), (integer), (floating point), (character) (built-in data type)..,. i nt us r_val ; i nt us r_val.. 0.. i nt num_t r i e s = 0 ; i nt num_ r i ght = 0 ;,. i nt num_t r i e s = 0, num_ r i ght = 0 ;,. us r_val.. (constructor syntax). i nt num_t r i e s ( 0 ) ;??.
Chapter 1 C+ + 35 (assignment) C., string. st r i ng s eque nce_ name = " Fi bonacci " ;. (complex).. #i ncl ude <compl ex> compl ex<doubl e> pur ei ( 0, 7 ) ; complex compl ex (template)... (member).,.,? C++. f l oat (single precision), doubl e 2 (double precision), l ong precision). doubl e (extended.,. doubl e...,.,.. doubl e. doubl e us r_s co r e = 0. 0 ;
36 Essential C+ +, yes/no.. cha r us r_mor e ; cout << "Try anot he r sequence? Y/ N? " ; ci n >> us r_mor e ; cha r. ' a ', ' 7 ', ' ; ' (literal). ( (escape sequences) ). ' \ n ' ( newl i ne) ' \ t ' ( t ab) ' \ 0 ' ( nul l ) ' \ ' ' ' \ " ' ' \ \ ' (bac ks l a s h),. cout << ' \ n ' << ' \ t ' << us e r_name ;,. cout << "\ n\ t " << use r_ name ;,.,. " F:\ \ e s s ent i al \ \ pr ogr ams\ \ chapt e r 1\ \ ch1_mai n. c pp" ; C++ (true) (false) (boolean).. bool go_f o r_i t = t r ue ;
Chapter 1 C+ + 37. bool. t r ue f al se., go_f or_i t f al s e, us e_ scor e.. (pi)..? cons t. cons t i nt max_t r i es = 3 ; cons t doubl e pi = 3. 14159 ; (const).. max_t r i e s = 42 ; / / : 1.3 (Exp re ssio n),,,.. / / + a + b - a - b * a * b / a / b % a % b.. %. 5 / 3 1, 5 % 3 2. 5 / 4 1, 5 % 4 1. 5 / 5 1, 5 % 5 0.
38 Essential C+ +? 8. 8. 8.. cons t i nt l i ne_s i ze = 8 ; i nt cnt = 1 ; / /. / / a_ st r i ng, / / c nt 1. cout << a_ st r i ng << ( c nt % l i ne_s i ze? ' ' : ' \ n ' ) ; (? :). 0, 0.. cnt % l i ne_s i ze c nt l i ne_ si ze 0, 0.. expr? expr t r ue : expr f al se ; expr true, expr false (:).. 0 false, 0 true. cnt 8 0 true,. (compound assignment operator)., cnt = cnt + 2 ;
Chapter 1 C+ + 39. cnt += 2 ; / / c nt 2 +=, - =, *=, / =, %=. 1. cnt ++; / / cnt 1 cnt - - ; / / c nt 1 (prefix) (postfix). 1. i nt t r i e s = 0 ; cout << "Ar e you r eady f or t ry # " << ++t r i es << "?\ n" ; t r i es 1., 1. i nt t r i e s = 1 ; cout << "Ar e you r eady f or t ry # " << t r i es ++ << "?\ n" ; t r i es. 1. (relational operator) true false.. == a == b!= a!= b < a < b > a > b <= a <= b >= a >= b
40 Essential C+ + (==). bool us r_mor e = t r ue ; cha r us r_ r s p ; i f / /. / / us r_ r s p. i f ( us r_ r s p == ' N' ) us r_mo r e = f al se ; true. usr_rsp N' us r_mor e false. us r_ r s p ' N'. (!=). i f ( us r_ r s p!= ' Y' ) us r_mo r e = f al se ; ' N' us r_ r s p ' n '.. else. i f ( us r_ r s p == ' N' ) us r_mo r e = f al se ; el se i f ( us r_ r s p == ' n ' ) us r_mo r e = f al se ; us r_ r s p ' N' us r_mor e false,. ' N' el s e. us r_ r s p ' n ' us r_mor e false. us r_ r s p us r_mo r e. == =. / / us r_ r s p ' N'. / / t r ue. i f ( us r_ r s p = ' N' ) / /... OR ( ).
Chapter 1 C+ + 41 i f ( us r_ r s p == ' N' us r_ r s p == ' n ' ) us r_mo r e = f al s e ; true OR true., true. us r_ r s p ' N' us r_ r s p ' n '. AND (&&) true true. i f ( pa s swo r d && val i dat e ( pa s swo r d ) && ( acct = r et r i eve_acct _i nf o ( pas swor d) ) ) / /.... false AND false.,. password, password. NOT (!) false true. i f ( us r_mo r e == f al s e ) cout << "Your s co r e f or t hi s s es si on i s " << us r_ scor e << " Bye!\ n" ;. i f (! us r_mo r e )... (Precedence).., 5+2*10 70 25.., 5 2 2 10.., ( 5+2) *10 70.
42 Essential C+ +.... NOT ( *, /, % ) ( +, - ) ( <, >, <=, >= ) ( ==,!= ) AND OR i val.! i val % 2 / /. i val 0, NOT true. 0, NOT false.. i val 0 false. NOT i val. i val 0 false, true.,. false 0, true 1. i val 0 0%2.,.... C++..! ( i val % 2 ) / / ok
Chapter 1 C+ + 43 C++.,.. [LIPPMAN98] 4 [STROUSTRUP97] 6. 1.4 (loop) mai n( ). i f. i f true false. else.. (pseudocode) (#1 #2) i f (#5), i f - el s e (#4), swi t c h (#3). / / : whi l e. { #1. whi l e and. { #2.. i f. { #3. got _i t t r ue. } el se {.. / / #4.. i f no. / / # 5 go_f o r_i t ( ) f al se. } } }
44 Essential C+ + i f. true i f. / / # 5 i f ( us r_ r s p == ' N' us r_ r s p == ' n ' ) go_f or_i t = f al s e ; if ({}) ( (statement block) ). / / #3 i f ( us r_gue s s == next _el em ) { / / num_ r i ght ++; got _i t = t r ue ; } / /,.. / /. / / num_co r ++ i f / / got _i t = t r ue ; i f ( us r_gue s s == next _el em ) num_co r ++; got _i t = t r ue ; got _i t,. num_cor i f us r_gues s next _el em. got _i t. i f true.. got _i t us r_gues s i f el s e. el s e false.. i f ( us r_gue s s == next _el em ) { / / }
Chapter 1 C+ + 45 el se { } / / el se i f... i f. i f ( num_t r i e s == 1 ) cout << "Oops! Ni ce gue s s but not qui t e i t. \ n" ; i f ( num_t r i e s == 2 ) cout << "Hmm. Sor ry. Wr ong a s econd t i me.\ n" ; i f ( num_t r i e s == 3 ) cout << "Ah, t hi s i s ha r de r t han i t l ooks, i s n ' t i t?\ n" ; true if true false. el s e- i f i f. i f ( num_t r i e s == 1 ) cout << "Oops! Ni ce gue s s but not qui t e i t. \ n" ; el se i f ( num_t r i e s == 2 ) cout << "Hmm. So r r y. Wr ong agai n. \ n" ; el se i f ( num_t r i e s == 3 ) cout << "Ah, t hi s i s ha r de r t ha n i t l ooks, i s n ' t i t?\ n" ; el se cout << "I t must be get t i ng pr et t y f r ust r at i ng by now!\ n" ;, i f. true, el se - i f. i f false i f true. num_t r i es 3 false else.
46 Essential C+ + i f - el se., i f - el s e.. i f ( us r_gue s s == next _el em ) { / / } el se i f ( num_t r i es == 1 ) / /... el se i f ( num_t r i es == 2 ) / /... el se i f ( num_t r i es == 3 ) / /... el se / /... / /,. / /. / /? el s e- i f el s e.,.. i f ( us r_gue s s == next _el em ) { / / } el se { / / i f ( num_t r i es == 1 ) / /... el se i f ( num_t r i es == 2 ) / /... el se i f ( num_t r i es == 3 ) / /...
Chapter 1 C+ + 47 el se/ /... } cout << "Wa nt t o t r y agai n? (Y/ N) " ; cha r us r_ r s p ; ci n >> us r_ r s p ; i f ( us r_ r s p == ' N' us r_ r s p == ' n ' ) go_f or_i t = f al s e ; i f - el s e- i f swi t c h. / / i f - el s e- i f. swi t ch ( num_t r i e s ) { ca se 1 : cout << "Oops! Ni ce gue s s but not qui t e i t.\ n" ; br ea k ; ca se 2 : cout << "Hmm. Sor ry. Wr ong agai n. \ n" ; br ea k ; ca se 3 : cout << "Ah, t hi s i s ha r de r t han i t l ooks, i s n ' t i t?\ n" ; br ea k ; def a ul t : cout << "I t mus t be get t i ng pr et t y f r ust r at i ng by now!\ n" ; br ea k ; } swi t ch ( ).. swi t c h cas e. ca se ca se. def aul t def a ul t. def aul t. br ea k? cas e. ca se. ca se ca se. swi t c h ca se. num_t r i es 2 br eak.
48 Essential C+ + / / num_t r i es == 2, br eak / /. Hmm. Sor ry. Wr ong agai n. Ah, t hi s i s ha r de r t han i t l ooks, i s n t i t? I t mus t be get t i ng pr et t y f r ust r at i ng by now! br ea k, ca se ca se. br ea k. swi t ch,. swi t ch ( next _cha r ) { ca se ' a ' : ca se ' A' : ca se ' e ' : ca se ' E' : ca se ' i ' : ca se ' I ' : ca se ' o ' : ca se ' O' : ca se ' u ' : ca se ' U' : ++vowel _c nt ; br eak ; / /... } true.. whi l e. {. whi l e and. } C++ whi l e. bool next _ seq = t r ue ; / /? bool go_f o r_i t = t r ue ; / /? bool got _i t = f al se ; / /? i nt num_t r i e s = 0 ; / / i nt num_ r i ght = 0 ; / /
Chapter 1 C+ + 49 whi l e ( next _ seq == t r ue ) { / /. whi l e ( ( got _i t == f al s e ) && ( go_f or_i t == t r ue ) ) { i nt us r_gues s ; ci n >> us r_gue s s ; num_t r i e s++; i f ( us r_gue s s == next _el em ) { got _i t = t r ue ; num_ r i ght ++ ; } el se { / / / /. / /. i f ( us r_ r s p == ' N' us r_ r s p == ' n ' ) go_f o r_i t = f al se ; } } / / whi l e cout << "Wa nt t o t r y a not he r s eque nce? (Y/ N) " cha r t ry_agai n ; ci n >> t r y_agai n ; i f ( t r y_agai n == ' N' t ry_agai n == ' n ' ) next _s eq = f al se ; whi l e } / / whi l e ( next _ seq == t r ue ). true whi l e.. false., false. false (infinite loop). whi l e.
50 Essential C+ + next _s eq bool next _ seq = t r ue ; whi l e ( next _s eq == t r ue ) { / /... i f ( t ry_agai n == ' N' t ry_agai n == ' n ' ) next _ seq = f al se ; } false. whi l e. br ea k., whi l e t r i es_c nt max_t r i e s. br ea k. i nt max_t r i e s = 3 ; i nt t r i e s_cnt = 0 ; whi l e ( t r i e s_cnt < max_t r i es ) { / /. i f ( us r_gue s s == next _el em ) br eak ; / /. } t r i e s_cnt ++; / / cont i nue. br eak,. 4. st r i ng wor d ; cons t i nt mi n_ si ze = 4 ; whi l e ( ci n >> wo r d ) { i f ( wor d. si ze ( ) < mi n_ si ze ) / /. cont i nue ; } / / wor d mi n_ si ze / /. pr oces s_t ext ( wo r d ) ;
Chapter 1 C+ + 51 wo r d mi n_s i ze cont i nue. cont i nue, whi l e ( proce s s_t ext ( )). wo r d. wor d mi n_ si ze whi l e. 4. 1.5 (Arra y) (Ve c to r) 6 8 (element). ( Fi bonacci ) : 1, 1, 2, 3, 5, 8, 13, 21 ( Luca s ) : 1, 3, 4, 7, 11, 18, 29, 47 ( Pel l ) : 1, 2, 5, 12, 29, 70, 169, 408 (Tr i a ngul a r ) : 1, 3, 6, 10, 15, 21, 28, 36 ( Squa r e ) : 1, 4, 9, 16, 25, 36, 49, 64 ( Pe nt agonal ) : 1, 5, 12, 22, 35, 51, 70, 92,..?..,..,...,. (container type)., ( ). 6 18.,. 3 6.
52 Essential C+ + C++ (built-in array) (vector class).,,.,. (dimension)...,. pel l _s eq 18. cons t i nt seq_ si ze = 18 ; i nt pel l _s eq [ seq_ si ze ] ;, (header file). (template) vector <>,. ( ),. pel l _ seq 18., 0. #i ncl ude <vect or > vect or <i nt > pel l _s eq ( seq_ si ze ) ;. (subscript operator) []. 1 0. 1. pel l _ seq 1 18, 0 17 ( off-by-one Pell. ). pel l _s eq [ 0 ] = 1 ; / / 1 pel l _s eq [ 1 ] = 2 ; / / 2 Pell 10. f or. C++.. f o r ( i nt i x = 2 ; i x < s eq_s i ze ; ++i x ) pel l _s eq [ i x ] = pel l _s eq [ i x- 2 ] + 2*pel l _s eq [ i x- 1 ] ;
Chapter 1 C+ + 53 f o r. f o r ( ; ; ). i x 2... true. false. i x s eq_s i ze..,,. false. i x 1.. cout << "The f i r s t " << seq_ si ze << " el ement s of t he Pel l Se r i es :\ n\ t " ; f o r ( i nt i x = 0 ; i x < seq_ si ze ; ++i x ) cout << pel l _ seq [ i x ] << ' ' ; cout << ' \ n ' ;,., f o r. i nt i x = 0 ; / /... f o r ( ; i x < s eq_s i ze ; ++i x ) / /... (;). 2, 3, 4.?,..
54 Essential C+ + i nt el em_s eq [ seq_ si ze ] = { 1, 2, 3, / / ( Fi bonacci ) 3, 4, 7, / / ( Luca s ) 2, 5, 12, / / ( Pel l ) 3, 6, 10, / / (Tr i a ngul a r ) 4, 9, 16, / / ( Squa r e ) 5, 12, 22 / / ( Pe nt agonal ) } ;. 0.. / / 18 i nt el em_s eq [] = { 1, 2, 3, 3, 4, 7, 2, 5, 12, 3, 6, 10, 4, 9, 16, 5, 12, 22 } ;.. vect or <i nt > el em_s eq ( seq_ si ze ) ; el em_s eq [ 0 ] =1 ; el em_s eq [ 1 ] =2 ; / /... el em_s eq [ 17 ] =22 ;,. i nt el em_val s [ s eq_s i ze ] = { 1, 2, 3, 3, 4, 7, 2, 5, 12, 3, 6, 10, 4, 9, 16, 5, 12, 22 } ; / / el em_val s el em_ seq vect or <i nt > el em_s eq ( el em_val s, el em_val s+seq_ si ze ) ; el em_s eq... el em_val s 18 el em_s eq. 3.
Chapter 1 C+ + 55, el em_s eq.,. f o r. / / el em_ seq. si ze ( ) el em_ seq / /. cout << "The f i r s t " << el em_s eq. s i ze ( ) << " el ement s of t he Pel l Se r i es :\ n\ t " ; f o r ( i nt i x = 0 ; i x < el em_s eq. s i ze ( ) ; ++i x ) cout << pel l _ seq [ i x ] << ' ' ; c ur_t upl e. 0. cur_t upl e 3. c ur_t upl e. i nt cur_t upl e = 0 ; whi l e ( next _ seq == t r ue && cur_t upl e < s eq_s i ze ) { cout << "The f i r s t t wo el eme nt s of t he sequence a r e : " << el em_ seq [ c ur_t upl e ] << ", " << el em_ seq [ c ur_t upl e+1 ] << "\ nwhat i s t he next el ement? " ; / /... i f ( us r_gues s == el em_ seq [ cur_t upl e+2 ] ) / /! } / /... i f ( us r_ r s p == ' N' us r_ r s p == ' n ' ) next _s eq = f al se ; el se c ur_t upl e += 3 ;,.. cons t i nt max_s eq = 6 ; st r i ng s eq_names [ max_s eq ] = { " Fi bonacci ",
56 Essential C+ + } ; "Lucas ", "Pel l ", "Tr i angul a r ", "Squa r e ", "Pent agonal " eq_names. i f ( us r_gue s s == el em_ seq [ c ur_t upl e+2 ] ) { ++num_co r ; cout << "Ve r y good. Yes, " << el em_ seq [ c ur_t upl e+2 ] << " i s t he next el ement i n t he " << s eq_names [ cur_t upl e/ 3 ] << "s eque nce. \ n" ; } cur_t upl e/ 3 0, 1, 2, 3, 4, 5.. 1.6 (Pointe r) (Flexibility). 6,..?...... 3. (pointer)..,.
Chapter 1 C+ + 57...,... i val 1024. i nt i val = 1024 ;. (*). pi i nt i nt *pi ; / / pi i nt. i val? (, 1024). i val ; / / i val. (address-of operator). &i val ; / / i val. pi i val. i nt *pi = &i val ; (dereference).,. *. / / pi i f ( *pi!= 1024 ) / / *pi = 1024 ; / /
58 Essential C+ +.,.,. pi ; / / pi.. *pi ; / / pi. pi.., *pi! pi pi, pi pi..? 0 ( (null) ). 0. / / i nt *pi = 0 ; doubl e *pd = 0 ; st r i ng *ps = 0 ; 0.. i f ( pi && *pi!= 1024 ) *pi = 1024 ;. i f ( pi &&... ) pi 0 true. pi false AND. null NOT. i f (! pi ) / / pi 0 t r ue
Chapter 1 C+ + 59. vect or <i nt > f i bonacci, l uca s, pel l, t r i angul a r, squa r e, pe nt agonal ;?,. _ _ * _ _ vect or <i nt >. pv, 0. vect or <i nt > *pv = 0 ; pv 6., pv. pv = &f i bonacci ; / /... pv = &l ucas ;.,.. cons t i nt s eq_c nt = 6 ; / / vect o r<i nt > / / vect or <i nt > *s eq_addr s [ s eq_c nt ] = { &f i bonacci, &l ucas, &pel l, &t r i angul a r, &squa r e, &pe nt agonal } ; seq_addr s vect o r<i nt >*. seq_addr s [0] f i bonacci, s eq_addr s [1] l uca s.. vect or <i nt > *c ur r e nt _vec = 0 ; / /... f o r ( i nt i x = 0 ; i x < seq_cnt ; ++i x ) { cur r ent _vec = s eq_addr s [ i x ] ;
60 Essential C+ + } / / / / c ur r e nt _vec..,,.. C r and( ) s r and( ). #i ncl ude <cs t dl i b> s r and( s eq_c nt ) ; seq_i ndex = r a nd ( ) % seq_cnt ; cur r ent _vec = seq_addr s [ seq_i ndex ] ; r a nd ( ) s r a nd ( ) (random number). s r and( ) r a nd ( ) i nt seed. r and( ) 0. seq_addr s 0 5. (%) 0 5..., f i bonacci 1. i f (! f i bonacci. empt y ( ) && ( f i bonacci [0] == 1 ) ) pv? f i bonacci empt y ( ) (.) (member selection operator).. (->).! pv- >empt y ( ) empt y ( ), pv 0. pv &&! pv- >empt y ( )
Chapter 1 C+ + 61, pv ( pv ). i f ( pv &&! pv- >empt y ( ) && ( ( *pv ) [1] == 1 ) ) 3 6 (Standard Template Library). [LIPPMAN98] 3.3. 1.7. (1) (session). (2).. f s t r eam. #i ncl ude <f s t r eam> (output file stream) ofstream.. / / s eq_dat a. t xt. of st r eam out f i l e ( " seq_dat a. t xt " ) ; out f i l e?, ( ).,.. f st r eam i os_bas e : :a pp ( ). / / s eq_dat a. t xt. / /. of st r eam out f i l e ( " seq_dat a. t xt ", i os_bas e : :a pp ) ;
62 Essential C+ +.,.. / / out f i l e f al se / /. i f (! out f i l e ) of st r eam false. (standard error) ce r r. cout ce r r., ce r r (buffer).,. i f (! out f i l e ) / /... ce r r << "Oops! Unabl e t o save se s s i on dat a!\ n" ; el se / / ok : out f i l e.,. out f i l e << us r_name << ' ' << num_t r i es << ' ' << num_ r i ght << e ndl ; ostream cout ce r r., out f i l e. endl iostream (manipulator). iostream. endl,.. hex 16. oct 8. set preci si on(n) n ( iostream [LIPPMAN98] 20.9 ). (input file stream) ifstream,. ifstream false.. / / i nf i l e. i f st r eam i nf i l e ( " seq_dat a. t xt " ) ; i nt num_t r i e s = 0 ;
Chapter 1 C+ + 63 i nt num_co r = 0 ; i f (! i nf i l e ) { / /... / /... } el se { / / ok : / / / / : / / name num_t r i es num_cor r ect / / nt : / / nc : st r i ng name ; i nt nt ; i nt nc ; } whi l e ( i nf i l e >> name ) { i nf i l e >> nt >> nc ; i f ( name == us r_ name ) { / /! cout << "Wel come bac k, " << us r_name << "\ nyour cur r ent s co r e i s " << nc << " out of " << nt << "\ ngood Luc k!\ n" ; num_t r i es = nt ; num_cor = nc ; } } whi l e end-of-file( ). i nf i l e >> name
64 Essential C+ +. i nf i l e. end-of-file false., whi l e end-of-file. whi l e ( i nf i l e >> name ). anna 24 19 da nny 16 12... i nf i l e >> nt >> nc ; nt, nc. fstream. i os_ba se : : i n i os_bas e : :a pp :. 2) f s t r eam i of i l e ("s eq_dat a. t xt ", i os_bas e : :i n i os_ba se : : app ) ; i f (! i of i l e ) / /... el se { / /. i of i l e. s ee kg ( 0 ) ; / / ok :... }. end-of-file. s ee kg ( ) i of i l e.. iostream. iostream [LIPPMAN98] 20 [STROUSTRUP97] 21. 2) [LIPPMAN98] 20.6.
Chapter 1 C+ + 65 1.5.... C, string. 1.6.... 1.7. vect or <s t r i ng>. cout. so r t ( ). #i ncl ude <al go r i t hm> so r t ( cont ai ne r. begi n( ), cont ai ne r. end( ) ) ;,. 1.8 1.4 swi t ch..
66 Essential C+ + [1] instance( ) :.,..... [2] namespace( ) :.. di s pl ay ( )... name_a name_b name_a : : di s pl ay ( ) name_b : :di s pl ay ( ). [3] data object( ) : (variable)...,.., st r i ng name ; name string., i nt a ; a i nt a i nt.,.