Академический Документы
Профессиональный Документы
Культура Документы
85DBF - Pavlovskaya - T - A - C - C - Programmirovanie - Na - Yazyke - Vysokogo - Urov PDF
85DBF - Pavlovskaya - T - A - C - C - Programmirovanie - Na - Yazyke - Vysokogo - Urov PDF
, .
C/C++
,
3004^
300.piter.com
300
300- -
1;^'
-
--
2003
32.973-0187
681.3.06(075)
12
:
. ., , ,
. . .
. ., ,
-
B.C., ,
-
12 C/C++. / . . . .:
, 2003. 461 : .
ISBN 5-94723-568-4
C++
ISO/IEC 14882. , ,
. ,
C++ ,
- .
20 , ,
.
32.973-0187
681.3.06(075)
!. 13
1 . C++ 15
15
17
18
19
19
20
22
C++ 22
22
.23
26
28
28
31
37
38
40
40
44
49
51
51
58
. 58
, 65
(typedef) 65
(enum) 66
(struct) 67
(union) 69
2 . 72
73
73
76
76
77
82
83
85
mainO 87
88
93
#inclucle 93
#clefine 94
95
#undef 96
97
98
99
3 . 102
102
109
114
115
119
121
122
126
1 128
128
136
139
142
144
151
151
151
159
165
165
165
I I . - 173
4 . 178
178
; 180
this 181
182
184
185
186
186
187
187
188
188
189
190
191
191
new delete 192
195
195
196
197
198
5 . 200
200
201
205
207
208
208
209
6 . 211
211
219
220
221
7 . 222
223
223
225
227
228
229
8 . 231
231
const^cast 232
clynamic_cast 232
233
233
235
236
static_cast 237
reinterpret_cast 238
238
9 . 241
II 248
248
252
260
i l l . 263
1 0 . 265
267
269
269
271
273
278
280
282
, 284
1 1 . 286
287
288
8
288
288
289
291
293
294
1 2 . 295
297
(vector) 299
(deque) 304
(list) 306
(stack) 310
(queue) 311
(priority__queue) 313
315
() 316
(multimap) 321
(set) 321
(multiset) 323
(bitset) 323
325
1 3 . 328
328
331
332
333
334
335
336
337
337
338
341
1 4 . 343
344
adjacent_find 345
count, countjf 345
equal 346
find, findjf 346
find_first_of 347
find_end 347
for each 347
9
mismatch 348
search, search_n 348
348
, copy_backward 350
fill,filLn 351
generate, generate_n 351
jter_swap, swap, swap_ranges 352
random^shuffle 352
remove, removejf, remove__copy, remove_copy_lf 353
replace, replacejf, replace_copy, replace__copyJf 354
reverse, reverse^copy 355
rotate, rotate__copy 355
transform 356
unique, unique^copy 357
, 357
binary^search 358
equal__range 359
inplace_merge 359
lexicographical_compare 359
lower_bound, upper^bound 360
max, min 360
max_element, min_element 360
merge 361
next^permutation, prev_permutation 361
nth_element 362
partial^ sort, partial_sort_copy 363
partition, stable_partition 363
sort, stable_sort 364
364
includes 365
setjntersection 365
set_difference, set__symmetric_difference 366
set_union 366
366
make^heap 367
pop_heap 368
push^heap 368
sort_heap 368
1 5 . 369
369
accumulate 369
inner_product 370
partiaLsum 370
adjacent_difference 371
1
valarray 371
374
16. 378
III 381
382
383
1 . C++ 384
2.
printf 387
3. 390
4. 391
5. ,
394
6. 409
409
416
?. ASCII 447
450
C++
ISO/IEC 14882 (1998)
,
. , ,
.
.
,
, . ,
, C++
,
.
,
, , ,
.
C++
. : (, 1000 ),
.
. ,
100 000 , , ,
, . C++
,
, .
,
,
.
Windows ,
. C++,
, .
: , -
.
12
,
.
,
, ,
.
.
+-.
, , , ,
.
.
,
.
, , , , ,
, ,
.
,
, .
,
. . . . ,
. . , . . , . . -
. , , ,
, .
-
-
2001 . ,
-
( ).
mux@tp2055.spb.edu 2:5030/1016.20@fidonet.org.
, ,
compiapiter.com ( , ).
!
Web-
http://www.pi ter.com.
I
, .
.
,
,
,
.
.
,
,
.
,
, .
70-
.
,
.
, ,
. ,
. ,
, . ,
, , ,
.
,
.
14 I.
,
, .
: ,
, .
C++.
, , ,
,
.
,
, .
-
() ,
.
.
.
.
,
,
. . , ,
, .
, .
1
C++
: , , .
, (
), ,
. ,
, , (. 1.1):
, ,
.
, , ,
.
.
.
. 1 . 1 .
16 I.
.
, ^.
.
.
.
, .
.
,
.
. ,
, .
. 1,2
-+.
, ,
(,
,
).
,
,
, .
.
, , ,
,
,
(, ).
,
. .
.
, ,
.
,
,
, , -
, .
,
[ void I i n t ] :
,
, void,
int, . .
^ -- ,
{ }.
1. C++ 17
. ,
, .
^ (Myyl.
2 (.) (.)
]\
. 1.2.
C++ , ,
, -
.
, ,
,
, .
C++ :
;
18 I.
9;
:
" { } , i [ ] ( ) + - / % * . \
? < = > ! & # - ; ' '
: , ,
.
:
;
() ;
;
;
(, , , ).
, ,
.
.
, .
, , sysop, SySoP SYSOP
.
, . .
.
, (
Microsoft ),
, , ,
, , , iMaxLength,
IpfnSetFirstDialog. , ,
: maxjength, number_of_galosh.
,
.
, , . .,
.
:
(.
) ;
,
,
, , ;
1. C++ 19
, ,
( -
;
).
,
.
, . C++ . 1.1.
1 . 1 . C++
asm else new this
auto enum operator throw
bool explicit private true
break export protected try
case extern public typedef
catch false register typeid
char float reinterpret_cast typename
class for return union
const friend short unsigned
const_cast goto signed using
continue if sizeof virtual
default inline static void
delete int static__cast volatile
do long struct wchar_t
double mutable switch while
dynamic_cast namespace template
,
. .
, .
-
. [ ], ( ) ? :
.
C++ ^-, . 31,
1.
(). . 189.
20 I.
. ,
, . ,
, ^.
, , . 1.2.
1.2. C++
: 8, 0, 199226
,
,
: , 01, 020, 07155
(0,1,2,3,4,5,6,7)
: , , 0x1 8, OXOOFF
(0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F)
: 5.7, .001, 35.
[].[]^
: 0.26, .-, 510
[][.][]{|}[+|-][]^
, '', ', '*', 'db', '\0', V ,
'\012', '\x07\x07'
, " Vasia",
"\13 r=\0xF5\n"
. 1.4.
,
(-), : -218, -022, -, -4.8, -0.14.
/-
. ( ),
.
10.
, ,
, .
^ (. . 24).
^ , , .
^ , , .
, .
1. C++ 21
, ,
char.
1nt,
( ).
:
, (, \
, \ );
( ' ) , ( \ ), ( ? )
( " );
,
, \073, \0xF5.
255.
, ,
, escape'7. 1.3
.
.
, . 1.3, .
,
.
1.3. C++
\ 7
\ 8
\f ()
\
\ D
\t 9
\v
\\ 5
\' 27
\" 22
\? 3F
\Oddcl - *"^
\Oxddd ddd
, . ,
, ,
, :
" \"\""
22 I.
.
, ,
.
,
, .
,
. ,
" \
, \
"
" , "
-
, \0.
.
, "" 1 .
, , "", ''.
.
(//)
,
- /* */.
,
C++, .
- ,
.
//-, /* */
.
. , . 102.
++
.
-.
, ,
.
:
;
1. C++ 23
, ;
, .
, ,
.
.
, .
C++ . C++
,
, .
. , ,
, , , , .
{) ,
.
:
int ();
char ();
wchar^t ( );
bool ();
float (11);
double ( ).
{),
. ,
, .
,
:
short ();
long ();
signed ();
unsigned ().
(int)
int ,
. 16- 2
, 32- 4 .
short ,
2 .
long , 4 . ,
16- int short int, 32- int
long int.
24 I.
. signed
( , 1 ).
unsigned ,
. ,
1nt .
IBM PC-
. 1.4.
,
signed .
, ,
. -
, L, 1
(long) , U (unsigned). , 32L long
4 . L U , ,
Ox22UL 05LU.
short int, long int, signed int unsigned int short, long, signed
unsigned .
(char)
,
,
. , 1 . char,
, .
-128 127.
unsigned 255.
256- ASCH.
char ,
.
(wchar_t)
wchar_t ,
1 , , Unicode.
; , short.
wchar_t L, , L"Gates".
(bool)
true false,
.
false (). true.
true 1.
1. C++ 25
()
bool true false 1
signed char -128 ... 127 1
unsigned char 0 ... 255 1
signed short int -32 768 ... 32 767 2
unsigned short int 0 ... 65 535 2
signed long int -2 147 483 648 ... 2 147 483 647 4
unsigned long int 0 ... 4 294 967 295 4
float 3.4e-38 ... 3.4e+38 4
double 1.7e-308 ... 1.7C+308 8
long double 3.4e-4932 ... 3.4e+4932 10
.
int.
sizeof, .
, MS-DOS sizeof (int) 2,
Windows 9 OS/2 4.
26 I.
ANSI ,
, :
slzeof(float) < slzeof(double) < sizeofdong double)
sizeof(char) < slzeof(short) < sizeofdnt) < sizeofdong)
<Timits.h> (<c11m1ts>),
<float.h> (<cfloat>), numericlimits (.
, . 378, 5).
,
, ,
, .
-
,
.
void
, void,
. ,
,
( . 73), (. 51)
(cf 56).
C++ ^
(. 16). ma1 .
.
:
__ ( [ ] ) {
,
}
, - ,
.
. 73, :
, void:
, ,
;
;
(
).
, main, fl f2:
1. C++ 27
int ma1n(){
}
int fl(){
f l
}.
int f2(){
f2
}
( ).
/. C++
/ ,
, . :
, , C++.
/ :
int scanf (const char* format. ... ) //
int printf(const char* format. ... ) //
format.
, ( )
( ) , ,
%,
.
2.
, / :
#1nclude <std1o.h>
1nt main(){
int 1:
printf(" \"):
scafifC'^d". &1):
printf(" ^d. !", i):
return 0:
}
,
<stdio.h>,
/ (
).
#. . 93.
i.
. 28.
printf
-
28 I.
\. scanf
1 ( & ),
,
. /
/ (. 88).
C++:
#include <1ostream.h>
int main(){
1nt 1:
cout " \":
cin 1:
cout " " 1 ". !":
return 0;
}
<1 ostream. h>
/: .;. - 1
cout ,
.
. 265.
,
.
.
, ,
. . ,
. , ,
, ,
. .
.
.
,
. .
, .
.
.
:
1nt ; float ;
:
[ ] [const] [];
1. C++ 29
.
auto, extern,
static register. .
const , .
,
.
,
.
:
"
:
( )
.
, .
:
short int = 1; //
const char = ' ; //
char s. sf = 'f ; // sf
char t (54);
float = 0.22. x(3). sum;
,
(. . 38
3).
, ,
.
, .
,
.
.
(,
), ,
, .
,
, , .
( , ). ,
.
( )
( ).
,
. .
,
30 I.
. ,
. ,
, ,
::.
:
auto .
,
. ,
.
. ,
,
.
extern ,
( ). ,
, i.
, . 98.
static . .
,
.
.
, .
register auto,
. ,
auto.
int : / / 1
int main(){
int b: // 2 b
extern int X: / / 3 x
static int : // 4
= 1: / / 5
int ; // 6
= 2: / / 7
:: = 3: / / 8 .
return 0;
}
i n t X = 4; / / 9
.
,
. ,
6-8,
, -
^ , extern
.
1. C++ 31
. b ,
, : b
,
, .
,
.
.
(,
).
. 97.
.
, , , ,
, , 1,1 ( L) I ( i).
.
,
, . ,
( ), ,
,
.
.
,
, ,
. C++
. 3
, .
,
, ,
.
1.5 , C++,
( ,
).
. 1.
, ,
( ), ( )
( ).
, sizeof,
. . 189.
32 I.
1.5. C++
1
1
++ 1
1^
sizeof
1
( )
+
new
delete
(type) |
/
|
+
|
|
<
<=
>
|
\=
& 1 ()
1 ()
&& 1
II . 1
.
1. C++ 33
1
7 ; ()
1
/=
+=
=
=
&=
1=
|
.
1 (++ ). ,
, ,
, .
,
,
, .
#include <stdio.h>
int main(){
int X = 3. = 3:
printfC" : ^d\n". ++):
printf(" : \", ++);
pr1ntf(" : ^d\n". );
printfC" : ^d\n". ) ;
return 0;
:
: 4
: 3
: 4
: 4
--
(L'value). ,
, .
, (Left) ,
34 I.
.
L-.
sizeof
, :
sizeof
sizeof ( )
:
#1nclude <iostream.h>
int ma1n(){
float X = 1;
cout "sizeof (float) :" sizeof (float):
cout "\ns1zeof X :" sizeof x;
cout "\nsizeof (x + 1.0) :" sizeof (x +1.0):
return 0:
}
:
sizeof (float) : 4
sizeof X : 4
sizeof (X + 1.0) : 8
,
doubl , , ,
X . ,
, ,
, .
(-, I ~). ( - )
.
(!) ,
( ), 1, .
, .
(--), ,
.
(/) (%).
. ,
,
(. , . 38, 3).
.
.
#include <stdio.h>
int main(){
int X = 11. = 4:
float z = 4:
printf(" : ^d ^f\n". x/y. x/z):
printf(": ^d\n". x^y):
return 0:
1. C++ 35
:
: 2 2.750000
: 3
( ) .
, . ( )
. (>)
, ,
.
.
(<. <=, >. >=. = . !=)
. .
true false ( , ,
true).
, .
(=)
(=), , .
(&, |, ^)
.
(
, , .).
, (^ &)
1 ,
1.
, ( |)
1 ,
1.
( ^)
1 ,
1.
#1nclucle <iostreani.h>
1nt main(){
cout "\n 6 & 5 = " (6 & 5);
cout "\n 6 1 5 = " (6 1 5):
cout "\n 6 ^ 5 = " (6 " 5);
return 0:
}
:
6 &5 =4
6 15= 7
6^5 =3
36 I.
L-,
.
.
(, ,
false, true).
1 true,
, . ,
. .
if ( . 40).
#1nc1ude <stclio.h>
1nt main(){
i n t a = 11. b = 4. max;
max = (b > a)? b : a:
printfC" : " . max):
return 0:
}
:
: 11
. ,
1, ,
1:
1 = (1 < ) ? 1 + 1: 1:
.
, ,
. , ,
.
:
( + 0.12)/6
&& II !z
(t * sin(x)-L05e4)/((2 * + 2) * (2 * + 3))
.
. -
, ,
,
. , = b = = ( = ), + b + ( + ) + .
: ,
, (sin(x + 2) + cos(y + 1))
, , + 2 ,
+1.
.
, b :
int = 2. b = 5:
38 I.
+ b 7 1nt, = b ,
( 5) ,
. , C++
= b = : b = ,
.
.
, .
,
,
.
:
(
);
.
, ,
( ) (, ),
.
char, signed char, unsigned char, short int
unsigned short int int,
, unsigned int .
,
.
3.
(
. 231).
, ,
, ,
,
.
, , .
.
,
, ,
.
1966 ,
, .
,
. ,
( -
1. ++ 39
). ,
- .
(. 1.3). ,
,
, ,
,
(. 1.4).
^
? 1 1
*
^
L-xzi^
. 1.3.
. 1.4.
. ( -
40 I.
, ),
.
goto, :
. ,
, , goto ,
, .
goto (. 49).
; C++ (
).
, .
, ,
. ,
, , ,
.
,
.
, ,
, .
; ( ,
, ). :
i++: //
* = b + ; //
fund, ); //
if
if
. . 1.5.
:
i f ( ) _1; [else _2;]
,
. ( true),
, . ,
.
,
se. -
, ,
, . ,
1. C++ 41
(
). , , ,
.
1 2
">^
t
. 1.5.
:
if (<0) b = 1: /II
if (a<b && (a>d || a==0)) b++: else {b *- a: a = 0;} //2
if (a<b) { i f (a<c) m = a: else m = c:} else { i f (b<c) m = b; else m = c;} // 3
if (a++) b++: // 4
if (b>a) max = b: else max = a; //5
1 el se.
, ,
.
,
. , 2 ,
< .
, ,
.
3 .
,
el se i f.
4 , 1 f
, .
, 5,
( : max = (b > ) ? b : ;).
. , . 1.6.
.
finclude <iostream.h>
int main(){
float X. : int kol:
cout " \";
Gin X :
if ( x*x + y*y < 1 ) kol = 2;
else if( x*x + y*y < 4 ) kol = 1:
42 I.
else ! = 0 ;
cout "\n : ! i
return 0:
. 1.6.
--
('==^ (='), , 1f(a=l)b=0:.
, ,
/ .
b .
: 1 f (1==) =0;.
. ,
0<<1, ,
0<, (true fal se,
int) 1. : 1f(0<x && <1)...
.
,
, .
-
, ,
if ( , !
,
):
#1nc!ucle <iostream.h>
int main(){
f!oat X. . temp; int !;
cout " \";
cin X ;
temp = X X + * :
! = 0:
1. C++ 43
i f (temp < 4 ) ! = 1;
i f (temp < 1 ) kol = 2:
cout "\n : " kol;
return 0;
}
- ,
, :
i f (int i = fun(t)) a -= 1; else a += 1:
, ,
,
.
if .
.
switch
switch ()
.
. 1.7. :
switch ( ){
case __1: [__1]
case __2: [__2]
case 1 ^ 1
W W
2 2 W
default
W
. 1.7. switch
(
), ,
,
. , ,
.
break
return. break
44 I.
default ,
.
. , ,
, ,
(. 1.8).
.
( ),
( ).
, ,
, .
, ,
.
1. +-<- 45
<^ j>
^f
V
. 1.8. : ;
,
, .
, ,
.
,
, ,
.
, .
, .
break, continue, return goto (.
, . 49).
.
, , C++
while, do while for.
(while)
, . 1.8,,
:
while ( )
,
.
. ( false),
. false,
46 !.
. .
.
( /=2+1
):
#include <stdio.h>
i n t main(){
f l o a t Xn. Xk. Dx;
printf(" : " ) :
scdnfrxnfXr. &. &. &Dx):
printfC'l X I Y |\n"): //
float X = Xn: //
while (X <= ){ //
printfC'l ^5.2f I ^5.2f |\n". X. X*X + 1 ) : //
X += Dx: //
}
return 0:
}
( ):
#inclucle <iostream.h>
i n t main(){
i n t num:
cout " : ": cin num:
i n t half = num / 2: / /
i n t div = 2: / /
while (div <= half){
i f (!(num % div))cout div " \ n " :
div++:
}
return 0:
}
while (true) while (1)
- .
while
^. :
while ( i n t = 0){ ... / * */ }
(do while)
, . 1.8, ,
:
do while :
.
1. C++ 47
,
, . ( f ! se),
. ,
false -
. .
( ):
#include <1ostream.h>
1nt mdin(){
char answer;
do{
cout "\ ! ": c1n answer;
}while (answer != 'y');
return 0;
}
. X
Eps :
= I (-1 + X/yn-l).
_1 (
), .
,
, .
fabsO,
<math.h>.
#1nc1ucle <stdio.h>
#include <math.h>
i n t main(){
double X, Eps; / /
double Yp. Y = 1; / /
pr1ntf(" : " ) ;
s c a n f C ' ^ m i r . &. &Eps);
do{
Yp = Y;
Y = (Yp + X/Yp)/2;
}while (fabs(Y - Yp) >= Eps);
pr1ntf("\nKopeHb ^If ^If". X. Y);
return 0;
}
(for)
:
for (: : ) :
, .
, ( ),
, :
48 I.
for ( i n t 1 = 0. j = 2: ...
1nt . m:
for (k = 1. m = 0; ...
, ,
!. .
: ,
bool, true, .
.
.
.
. for (
!).
(, 1 100):
for ( i n t 1 = 1. s = 0: 1<=100; 1++) s += 1;
( y^x^+i
):
#1nclucle <std1o.h>
i n t main(){
float Xn. Xk. Dx. X;
printfC" : " ) :
scanfC'^f^f^f". &. &. &Dx):
printfC'l X I Y |\n"):
for (X = Xn: X<=Xk: X += Dx)
p r i n t f C ' l ^5.2f I %5.2f | \ n " . X. X*X + 1):
return 0:
}
( ):
#include <iostream.h>
i n t main(){
i n t num. half, div:
cout "\ : ": cin num:
for (half = num / 2. div = 2: div <= half: div++)
if (!(num % div))cout div "\n":
return 0:
}
,
, : ,
, .
while for
:
^
-.
1. C++ 49
for ( ; 2; ) ;
while (2){
; ;}
.
, :
, ,
, (
);
, ,
;
(. );
, , ,
, .
,
.
do while ,
(, ).
while ,
,
.
for (
).
C++ ,
:
goto;
break;
continue;
return.
goto
goto :
goto :
:
: :
50 I.
goto .
, ,
.
:
;
(,
- ).
, goto
^. goto
, ,
, .
if, switch
. ,
, , ,
:
int : ...
goto metka; ...
{1nt = 3. b = 4;
= + b;
metka: int m = + 1; ...
}
.
break
break switch
, ,
break.
.
eps
.
sh X = 1 + xV3! + xV5! + xV7! + ...
, ,
, .
#1nclude <1estream.h>
#1nc1ucle <niath.h>
int main(){
^ goto ,
.
,
.
1. C++ 51
, ,
int i=10:, (int)
(10).
(i) ,
.
.
.
, . C++
, void,
.
, -
.
52 I.
,
, ,
.
( , ,
),
. ,
:
(*) ( __ );
, :
1nt (*fun) (double, double);
fun , int
double.
,
( ).
( ) :
*:
, ( . 57)
(. . 69),
, (, , ,
).
, ,
, .
,
1nt *. . *;
, .
.
. .
void , ,
, (,
).
void ,
, -
, ,
(. . 231).
,
. :
int 1; //
const int ci = 1; //
int * p i ; //
const int * pci; //
int * const = &i; // -
const i n t * const = &ci; // -
1. C++ 53
, const,
, ,
const , .
&.
, .
,
( heap).
,
.
, ,
.
. C++
.
11 , new
delete.
,
.
.
, .
:
1. :
:
int = 5; //
int* = &; / /
int* (&): //
:
int* = ;
, (.
, . 58, , . 80):
i n t b[10]; / /
i n t * t = b; / /
void f ( i n t ){ / * ... * / } / /
void ( * p f ) ( i n t ) ; / /
pf = f; / /
2. :
char* vp = (char *)08000000:
8000000 , (char *)
: char.
54 I.
3. :
1nt* SUXX = NULL;
1nt* rulez = 0:
NULL,
, .
, 1 nt
.
, ,
, .
4. :
new:
int* = new 1nt: //1
1nt* m = new 1nt (10); // 2
1nt* q = new 1nt [10]; // 3
mal 1 oc^
1nt* u = ( i n t *)malloc(s1zeof(int)); // 4
1 new
1 nt
. (,
) .
2, ,
10.
3 new 10
int ( 10 )
q. .
. . 58.
,
bad^alloc (
, . 222, . 378).
0.
4 , 1,
11 , ,
.
(int*) , , ,
( . . 231).
, 0.
new , 11 ,
.
, new,
delete, , 11
^ malloc,
<malloc.h>.
1. C++ 55
free. -
.
:
delete ; delete m; delete [ ] q; free (u);
new[],
delete[]. .
, ,
,
. .
- ,
. , ,
, . ^ -
.
.
.
,
, ,
1nt * ( * [ 1 0 ] ) ( ) ;
10 ,
1 nt.
,
, , .
.
:
1) , ,
;
2) ,
;
3) ,
, ;
4) .
:
1nt *(*[10])():
5 42 1 3 //
: ,
(*), , ,
, (++), (), , .
(&).
56 I.
, ,
, .
, (
):
char : / / char
char * = new char: / *
char * /
* = '': = *: / /
, *_
, L- (. . 33),
.
, .
, (
).
.
. ,
#1nclude <std1o.h>
1nt main(){
unsigned long 1nt A = 0Xcc77ffaa:
unsigned short int* pint = (unsigned short int*) &A:
unsigned char* pchar = (unsigned char *) &A:
printfC I ^x I ^x I ^x I". A. *pint. *pchar):
return 0:
}
IBM - ^:
I cc77ffaa | ffaa | |
pint pchar , pchar
, pint .
.
: ,
. ,
.
. 231.
, void*.
boo! (,
), true, false.
:
void*:
.
| ffaa | 77 | ffaa
1. C++ 57
, void*.
.
( ) .
-, ,
( ,
-, ).
( , ,
) ,
.
,
, , .
,
.
sizeof ().
, ,
, :
short * = new short [ 5 ] ;
++; / / 2
long * q = new long [ 5 ] ;
q++; // q 4
,
( , ,
3). .
. ,
*++ = 10;
, , ,
. , ,
, 10,
, .
:
* = 10: ++;
(*)++. , ,
.
& ,
. ,
, .
.
58 I.
,
. ,
. :
& ;
, , & ,
,
, :
1nt !:
1nt& pal = k o l ; / / pal - !
const char& CR = ' \ n ' : / /
.
- ,
, (. . 77),
extern (. . 178).
.
, .
,
.
.
, , ,
(
, . 77).
, ,
.
, .
.
, ,
.
.
.
,
():
f!oat [10]; / / 10
,
.
1. C++ 59
.
( , const ),
.
. .
, , ,
, :
int b[5] = {3. 2. 1}; / / b[0]=3. b[l]=2. b[2]=l. b[3]=0. b[4]=0
,
, ,
.
, ,
.
,
(. , . 77).
() .
.
#include <1estream.h>
int ma1n(){
const i n t n = 10;
i n t i . sum;
int marksCn] = {3. 4. 5. 4. 4};
for (1 = 0. sum = 0; 1<n; 1++) sum += marks[1];
cout " ; " sum;
return 0;
}
, ,
. , ,
.
, .
. .
,
, , ,
, -1 (
).
#include <iostream.h>
1nt ma1n(){
60 I.
: i Imin
. 1.9.
. , b ,
&[0], i- ,
*(+1). ,
.
:
1nt [100]. [100]:
i n t * = : / / i n t * = &[0]:
1nt *pb = b:.
f o r d n t 1 = 0: 1<100: 1++)
*pb++ =>pa++: / / pb[1] = pa[1]:
new,
, :
1. C++ 61
i n t = 100:
f l o a t * = new f l o a t [ n ] ;
- float,
, 100
, .
, .
,
, , ,
.
, , , 5
[5] *(+5).
mall :
i n t = 100;
float *q - ( f l o a t *) malloc(n * s i z e o f ( f l o a t ) ) :
, mal 1,
, void*,
float.
, new [],
delete [], , mall
free, :
delete [ ] : free (q):
. delete ,
.
, ,
int matr [6][8]:
6 8 .
.
,
.
, , matr[i][j],
: *(matr[i]+j) *(*(matr+i)+j). , matr[i]
i- .
, (
),
, :
int mass2 [][2] = { {1. 1}. {0. 2}. {1. 0} }:
int mass2 [3][2] = {1. 1. 0. 2. 1. 0}:
. ,
, .
62 I.
#include <stdio.h>
1nt ma1n(){
const int nstr = 4. nstb = 5; //
int b[nstr][nstb]: //
int 1. j:
for (1 = 0: 1<nstr; i++) //
for (j = 0: j<nstb: j++) scanfC'^d". &b[1][j]):
int istr = -1. MaxKol = 0;
for (i = 0; i<nstr: i++){ //
int ! = 0:
for ( j = 0; j<nstb: j-b+) i f ( b [ i ] [ j ] == 0)Kol++;
i f (Ko1 > MaxKol){istr = i ; MaxKol = Kol:}
}
printfC :\n"):
for (i = 0; i<nstr: i++){
for (j = 0; j<nstb: j++)printf("^d ". b[i][j]):
printf("\n");}
i f ( i s t r == -Dprintf(" "):
else printf("HoMep : ^ d " . i s t r ) :
return 0:}
istr,
(i-) Kol,
MaxKol. ,
(
, Kol ).
.
new ( ),
:
i n t nstr = 5:
i n t * * m = ( i n t **) new i n t [ n s t r ] [ 1 0 ] :
, ,
:
i n t nstr. nstb:
cout " :":
cin nstr nstb:
i n t **a = new i n t * [ n s t r ] : // 1
f o r ( i n t i = 0: i<nstr: i++) // 2
a [ i ] = new i n t [nstb]: // 3
1 int
(
nstr). 2
. 3 -
1. C++ 63
,
. nstb int (. 1.10).
s.
V
nstb
. 1.10.
-
delete [ ] . .
:
.
* () [] (),
, : int *[10]: 10 int.
, -.
- , ,
4 0 ' . -
.
(. . 20):
char str[10] = "Vasia";
// 10 9
// - 'V. '', 's'. '1*. ''. '\0'
10 , 5
, -.
, (
):
char s t r [ ] = "Vasia": / / 6
char * s t r = "Vasia"
, ,
( , str[l]='o' ).
64 I.
,
. (
)
. ,
, , ,
. . (
, . 91, 6),
.
. .
#1nclude <stclio.h>
#1nclude <str1ng.h>
1nt main(){
char s[80]. passw[] = "kuku"; / / passw - .
/ / *passw = "kuku";
int 1. = 0;
for (1 = 0: !k && ; U+){
pr1ntf("\nBBeflHTe ;\n");
gets(s); / /
i f (strstr(s.passw))k = 1; / /
}
i f () p1ntf("\napob ");
else pintf("\napob ");
return 0;
}
.
-
.
src dest.
:
char srcClO]. dest[10];
string char.
( ASCII),
,
.
, ,
.
, ,
. C++
. ,
, ,
.
(typedef)
,
typedef:
typedef _ [ ] :
66 I.
.
. :
typedef unsigned int UINT:
typedef char MsgClOO];
typedef struct{
char f1o[30];
int date, code:
double salary:} Worker:
,
:
UINT i . j : / / unsigned int
Msg s t r [ 1 0 ] : / / 10 100
Worker staff[100]: / / 100
,
typedef :
- typedef,
.
(enum)
, ,
( ).
,
. :
enum [ ^ ] { ^ }:
,
. ,
.
.
,
, :
enum Err {ERR__READ. ERR__WRITE. ERR_CONVERT}:
Err error:
switch (error){
case ERR_^READ: / * * / break:
case ERR^WRITE: / * * / break:
case ERR^CONVERT: / * * / break:
}
ERR_READ, ERR_WRITE, ERR_CONVERT , 1 2
.
:
enum {two = 2. three, four, ten = 10. eleven, f i f t y = ten + 40}:
1. C++ 67
,
.
, .
(struct)
, ,
. C++
,
, :
struct [ _ ] {
_1 _1:
_2 _2;
^ ^;
} [ ^ ] ;
,
, .
, ,
.
:
/ / :
struct {
char f1o[30]:
i n t date, code:
double salary:
}staff[100]. *ps:
, ,
,
:
struct Worker{ / / Worker
char f1o[30]:
i n t date, code:
68 I.
double salary;
}; / /
/ / Worker Worker:
Worker staff[100]. *ps;
(
) ,
, :
struct L i s t ; . / / List
struct Link{
List *p; / / List
Link *prev. *succ; / / Link
}:
struct List { / * List * / } ;
.
:
struct{
char fio[30];
int date, code;
double salary;
}worker = {"". 31. 215. 3400.55};
(,
):
struct complex!
float real. im;
} compl [ 2 ] [ 3 ] = {
{{1. 1}. {1. 1}. {1. 1}}. // 1. TO compl[0]
{{2. 2}. {2. 2}. {2. 2}} // 2. compl[1]
,. }:
, .
.
(.
, . 189).
, .
. ()
-> ,
:
Worker worker, staff[100]. *ps;
worker.fio = "";
staff[8].code = 215;
ps->salary = 0.12;
1. C++ 69
,
:
struct { i n t ; double ; } ;
struct { : double ;} [ 2 ] :
[0].. = 1;
[1]. = 0 . 1 ;
, ,
. ,
(,
) ,
struct,
, .
.
, , /.
1 , .
( ):
struct Options{
bool centerX:!;
bool centerYil:
unsigned int shadow:2;
unsigned int palette:4:
}:
. ,
.
. ,
,
. ,
, ,
,
.
.
(union)
(union) ,
. ,
, struct union.
,
,
.
, ,
:
70 I.
#include <iostream.h>
1nt niain(){
enum paytype {CARD. CHECK}:
paytype ptype:
union payment{
char carcl[25]:
long check:
} info:
/* info ptype */
switch (ptype){
case CARD: cout " no : " info.card: break:
case CHECK: cout " : " info.check: break:
}
return 0:
}
,
, ,
.
, :
#include <iostream.h>
i n t main(){
enum paytype {CARD. CHECK}:
struct!
paytype ptype:
union{
char card[25]:
long check:
}:
} info:
... /* info */
switch (info.ptype){
case CARD: cout " no : " info.card: break:
case CHECK: cout " : " info.check: break:
}
return 0:
}
(, ,
).
, :
struct Options{
bool centerX:!:
bool centerY:!:
unsigned int shadow:2:
unsigned int palette:4:
}:
1. C++ 71
un1on{
unsigned char ch;
Options bit;
}option = {OxC4};
cout option.bit.palette;
option.ch &= OxFO; //
. :
;
;
, ,
;
.
2
.
.
. C++
,
.
, .
.
, ,
. ,
, .
.
.
(),
.
.
,
( ).
.
,
.
, , .
2. 73
, .
,
.
, .
. '-
, -
.
.
, ,
(, ).
,
.
.
C++ ,
, , (
, ,
).
.
.
,
- .
.
C++ ,
main ( ).
. .
, ,
.
, .
(, , ) ,
.
, , ,
:
[ ] ( [ ^ ])[throw ( ) ]
{ }
.
, extern static:
74 I.
extern -- (
);
static ,
.
,
( ).
, void.
,
. .
, , (
).
, ,
, . 227.
,
.
,
, (
).
inline,
. inline
. ,
( ,
). inline
. inline-
.
, inline-
.
.
,
.
,
, .
void, ,
, .
, :
#include <iostream.h>
int sumCint a. int b); //
int main(){
int a = 2, b - 3, c. d;
= sum(a. b): / /
cin d:
cout sum(c. d); / /
2. 75
return 0:
}
int sumdnt . int b){ //
return ( + b):
}
, :
finclude <1ostream.h>
struct Worker{
char fio[30];
int date, code;
double salary;
}:
void print_worker(Worker); //
int main(){
Worker staff[100];
... /* staff */
, .
m .
.
:
m
110
2 2
m
3
4 10
.
,
.
,
,
. ,
. ,
,
.
r e t u r n [ ] ;
return (
). void,
. return void,
, main.
return main ,
. ,
return,
.
:
Int fl(){return 1:} //
void f2(){return 1:} // . f2
double f3(){return 1:}// . 1 double
2. 77
, ,
,
.
:
1nt* f ( ) {
1nt = 5;
return &; / / !
}
. ,
, ,
,
, .
,
;
,
.
.
.
: .
,
.
, , , .
,
:
#1nclude <iostream.h>
void f d n t 1. i n t * j . int& k);
int main(){
int i = 1. j = 2. = 3:
cout "i j k\n";
cout ' j '\n';
f(i. &j.k);
cout j k;
return 0
}
void f d n t i . i n t * j , int& k){
i++: (*j)++: k++:
}
78 I.
1 23
13 4
(1) .
. (j)
,
,
. ()
.
, .
,
.
, ,
.
,
const:
1nt f(const char*);
char* t(char* a. const int* b);
const ,
. ,
, , .
, const& ,
.
, , ,
.
, (,
, , , , ),
.
, , .
,
( ,
, -):
^ , ,
,
.
2. 79
#1nclude <1ostream.h>
1nt sumCconst 1nt* mas. const int n);
int const n = 10;
int ma1n(){
int marksCn] = {3. 4. 5. 4. 4};
cout " : " sum(marks. n);
return 0;
}
int sum(const i n t * mas. const int n){
/ / Bapi^aHTbi: i n t sum(int mas[]. i n t n)
/ / i n t sum(int mas[n]. i n t n)
/ / ( n )
i n t s = 0;
for ( i n t i = 0; i<n; i++) s += mas[i]:
return s:
}
,
, .
,
.
. b
, :
#include <stdio.h>
finclude <stdlib.h>
int sum(const int *a. const int nstr. const int nstb):
int main(){
int b[2][2] = {{2. 2}. {4. 3}}:
printfC'CyMMa b: ^d\n". sum(&b[0][0]. 2. 2)):
// sum -
int i. j. nstr. nstb. *a:
printf(" : \ " ) :
scanf("^d^d". &nstr. &nstb):
= ( i n t *)manoc(nstr * nstb * s i z e o f ( i n t ) ) ;
for ( i = 0: i<nstr: i++)
for ( j = 0: j<nstb: j++)scanf("^d". &a[i * nstb + j ] ) :
printfC'CyMMa a: ^d\n". sum(a. nstr, nstb)):
return 0:}
i n t sum(const i n t *a. const i n t nstr. const i n t nstb){
i n t i . j . s = 0:
for ( i = 0: i<nstr: i++)
for ( j = 0: j<nstb: j++)s += a[i * nstb + j ] :
return s:
}
,
:
80 I.
#1nclude <1ostreani.h>
1nt sumdnt **a. const 1nt nstr. const int nstb);
1nt main(){
int nstr. nstb;
cin nstr nstb;
int **a. i. j;
// a:
a = new int* [nstr];
for (i = 0; i<nstr; i++)
a[i] = new int [nstb];
for (i = 0; i<nstr; i++)
for (j = 0; j<nstb; j++)cin a t i ] [ j ] ;
cout sum(a. nstr. nstb);
return 0;
}
int sumCint **a. const int nstr. const int nstb){
int i . j . s = 0;
for (i = 0; i<nstr; i++)
for (j = 0; j<nstb; j++)s += a[i][j];
return s;
}
:
, , . 1.10.
.
.
;
void f ( i n t ) { / * . . . * / } / /
void ( * p f ) ( i n t ) ; / /
pf = &f; / /
/ / ( pf = f ; )
pf(lO); / / f pf
// ( (*pf)(10) )
,
(typedef).
( , ,
);
// PF
// int;
typedef void (*PF)(int);
// :
PF menu[] = {&new. &open. &save};
menu[l](10); // open
2. 81
,
,
. , char
short int, float double.
printf, :
Int printf (const char*. ...);
,
char* , :
printf(" "); //
printf(": ^5.2f ", sum); //
printf("^d ^d ^d %\ a. b. . d); //
82 I.
va_start. va_arg va_end,
<stclarg.h>. 5.
,
(.
, . 83), ,
.
, .
. ,
. ,
, ,
.
. ,
, .
,
, ,
.
( , ).
,
(-1). , 0!=1 1!=1.
long factdong n){
i f (n==0 11 n==l) return 1;
return (n * fact(n - D);
}
To :
long factdong n){
return (n>l) ? n * fact(n - 1) : 1;
}
, ,
, , (. 122).
,
.
,
, ,
, .
2. 83
, ,
, . ,
, ,
.
,
, .
, ,
. (
resolution ).
.
, ,
, . , ,
:
/ / :
i n t maxdnt. i n t ) :
// :
char* maxCchar*. char*);
// , :
int max (int, char*);
// :
int max (char*, int);
void f(int a. int b. char* c. char* d){
cout max (a, b) max(c. d) max(a. c) max(c, b);
}
max
(
).
,
(. . 38 3), ,
boo! char int, float double . .
, , int double void*.
, (
, . 195),
.
,
.
:
;
Q -;
U .
84 I.
:
#inclucle <1ostream.h>
float f(float i){
cout "function float f(float 1)" endl;
return 1:
}
double f(double 1){
cout "function double f(double i ) " endl:
return i*2:
}
int main(){
float X = 10.09:
double = 10.09:
cout f(x) endl: // f(float)
cout f(y) endl: // f(double)
/* cout f(10) endl: - 10: float
double? */
return 0:
}
10.
-:
int f(int . int b), int
f (i nt . i nt &b), ,
, ,
, ,
.
:
#include <iostream.h>
int f(int a){return a:}
int f d n t a. int b = l){return a * b:}
int main(){ .
cout f(10. 2): // f ( i n t . int)
/* cout f(10): - : f ( i n t , int)
f(int) ? V
return 0:
}
.
,
.
,
.
.
2. 85
,
const (, int
const int int int&).
, (
).
,
. ,
(,
, ).
,
,
; , .
,
,
.
C++ .
( . . 211).
, 11
,
.
, . , ,
, .
-:
template <class > {
/* */
}
.
,
, ,
:
template <class . class . int 1> void f(){ ... }
, , ( . 59)
, :
template <class >
void sort__vybor(Type *b. int n){
Type a; //
for (int 1 = 0; i<n-l; 1++){
int imin = i ;
for (int j = i + 1; j<n: j++)
i f (b[j] < b[imin]) imin = j ;
86 I.
, . 211.
2. 87
.
:
void sort_vibor<int>(1nt *b. int n){
... //
}
,
.
, ,
.
main()
, ,
main.
.
. :
// :
main(){ /* ... */ }
// :
mainCint argc, char* argv[]){ / * ... */ }
.
, argc argv.
(argc) , ,
, (argv)
char*.
, -,
-. (argv[0])
, (argv[l])
, argv[2] , . argv[argc]
0.
mainO ,
, .
. mainO .
#include <iostream.h>
void maindnt argc. char* argv[]){
for (int i = 0: i<argc: i++) cout argv[1] ' \ n ' :
}
main.exe
:
d:\cpp\main.exe one two three
:
D:\CPP\MAIN.EXE
one
two
three
88 I.
C++ ,
, , , .
, #inc1ude
,
. -
.
C++ , .
.
: /,
, , ,
. . 6.
/ ,
.
/
/ C++ , 11
, C++.
sync_with_stdio().
.
, /,
, , ,
. /
, -
. , ,
C++, .
C++ , . 265.
/
<stdio.h> <cstdio>. /
.
(, ,
).
.
/ .
:
FILE* fopen(const char* filename, const char* mode):
FILE,
, NULL .
-, :
"" ;
"w" ( , );
2. 89
"" ;
"+" ( );
"W+" ( ,
);
"+" .
t ( ) b
( ),
. ,
(0x13 0x10)
(
).
.
:
FILE * f = fopen("d:\\cpp\\clata". "rb+");
f .
/ .
, .
. ,
, .
. ,
, .
setbuf setvbuf .
,
: stdin, stdout, -
stderr,
stdaux stdprn.
. /
, .
/
/ :
,
.
. 6 . 409.
/ ,
.
( )
, /.
ftel 1 fgetpos
fseek fsetpos.
. /
.
90 I.
fread fwrite.
getc, fgetc, stdin
getchar.
put , fputc, stdout putchar.
fgets, stdin gets.
fputs, stdout puts.
fscant, stdin
scant, sscanf.
fprintf, stdout
printf, sprintf.
,
fclose:
int fclose(FILE*):
. ,
, .
,
, ,
, .
feof terror:
int feof (FILE*) ,
, 0;
int terror (FILE*) ,
/, 0.
, .
, .
: 20 ,
5 , ,
40 .
s,
.
.
#include <iostream.h>
#inclucle <stclio.h>
#include <stdlib.h>
#include <string.h>
int main(){
2. 91
<stcnib.h> <cstcnib>
(
sprintf):
double atof(const char* p) double;
int atoi (const char* p) int;
long atol (const char* p) long.
.
.
, 0.
, ( <>)
ERANGE .
( double ):
#include <stdio.h>
#1nclude <str1ng.h>
#inc1ude <stdl1b.h>
int ma1n(){
char s [ ] = "2. 38.5. 70. 0. 0. 1 s:
double m[10];
int 1 = 0:
do{
m[i++] = atof(p):
i f (1>9)break;
}wh1le(p = strchr(p
for( 1nt = 0; <1 k++) p r i n t f ( " ^ 5 . 2 f ". m[k]):
return 0;
(
<ctype. h> <cctype>) :
isalnum (A-Z, a-z, 0-9)
isalfa (A-Z, a-z)
iscntrl ( 0..31 127)
isdigit (0-9)
isgraph , (isalfa | isdigit | ispunct)
1 slower (a-z)
Isprint
ispunct
Isspace -
1 1 supper (A-Z)
1 isxd1g1t (A-F, a-f, 0-9)
int true,
. ,
2. 93
,
.
, tolower toupper,
.
wchar_t, w.
6.
+- ,
<math.h> (<cmath>).
(abs, fabs), (ceil, floor),
(sqrt), (pow), (sin, cos, tan,
sinh, cosh, tanh, asin, acos, atan, atan2), (exp), (log, loglO),
(modf), (fmod) .
<errno.h> (<>) EDOM
, , ERANGE
.
.
. #,
(. 18).
#include
#include <_>
, .
#include. , ,
.
(" ") ,
, .
#include
,
.
. h :
, , , , ;
, , , ;
;
;
.
94 I.
.
,
.
.h
1. ,
. <name. h>
C++ <cname>,
std. , #1nclucle <cstdio>
, #1nclude <stdio.h>,
std (.
, . 99).
#define
#def 1 .
:
:
#def1ne _
( );
, ,
:
#define ( ) _
^ .
#1 fdef #1 f ndef. :
#def1ne
:
#define VERSION 1
#def1ne VASIA " "
#define (.) ((x)>(y)?(x):(y))
#define MUX
,
.
, ,
= MAX(suml. sum2);.
= ((suml)>(sum2)?(suml):(sum2)):
,
. , #def1ne sqr(x) (*)
sqr(y+l), (+1*+1).
,
C++ . -
.
2. 95
const enum,
.
#if, #ifdef #ifndef ,
.
, ,
.
#if:
# i f _
[ # e l i f ^
[ #e1if _
^.]
[ #else
#endif
#elif .
, .
:
# i f VERSION == 1
#define INCFILE "versl.h"
# e l i f VERSION == 2
#def1ne INCFILE "vers2.h" / * * /
#else
#define INCFILE "versN.h"
#endif
#1nclude INCFILE
,
, defined(MMfl_KOHCTaHTbi), :
# i f defined(__BORLANDC___) && ___BORLANDC_ == 0x530 / / 5.3:
typedef istreaniJterator<int. char. char__tra1ts<char>. ptrd1ff__t>
1streain__1ter;
# e l i f defined(_BORLAND___) / / BC5.2:
typedef 1streamJterator<int. ptrd1ff__t> 1st reamJ ten
#else // VC5.0:
typedef 1 streamJterator<int> istream^iter;
#endif
,
:
#1f
int 1. j ;
double X, ;
#endif
96 I.
, .
#1 fdef #1 fndef,
,
#def 1 ( ,
, #def1ne 32JIT__SUPP0RT):
#ifdef
/ / ,
#1fndef
/ / ,
#el1f, #else #end1f.
#1 fndef ,
:
#ifndef HEADER_INCLUDED
#1nclude "myheader.h"
#def1ne HEADER_INCLUDED
#end1f
#undef
#undef . ,
, - .
C++ , ,
.
cplusplus , C++.
. ,
. ,
C++:
#1fdef _cplusplus
/ / , C++
#endif
, C++ .
DATE , :
p r i n t f C - %s \ " . __DATE__);
FILE .
LINE .
TIME , :
p r i n t f C ^s \ : ^s\n ".
FILE . TIME ):
2. 97
,
. : ,
, , , .
. , , .
, ,
. .
. , , ,
, -
1 .
. ,
, .
, .
. ,
() , .
. , (
) .
.
. C++
namespace.
,
, .
,
. , ,
, ::.
.
.
,
. ,
, :
struct Nocle{
1nt Node;
int 1;
}Nocle:
, ,
. C++
,
.
, , ,
(typedef) (. 66) -
98 I.
. , ,
.
, ,
.
.
,
. ,
.
.
.
, ,
static.
, :
;
extern.
1.
.
. two.cpp
my_header. h:
// my_header.h -
extern 1nt :
extern double b;
//
// .
#1nclude "my_header.h"
1nt a:
//
// two.cpp
#1nclude "my_header.h"
double b;
. two.cpp.
^ , , , .
extern : ,
extern .
2. 99
static,
, .
,
, , .
, ,
, . ,
, , ,
.
. ,
.
, , , .
-
.
.
( )
:
namespace [ _ ]{ /* */ }
,
. ,
.
,
, .
static. ,
.
.
.
namespace demo{
int 1 = 1:
int = 0;
void funcl(int):
void func2(int) { /* ... */ }
}
namespace demo{ //
// int i = 2 : -
void fund (double); //
void func2(int); // ( )
}
,
. ,
100 I.
::,
:
void demo::fund(1nt) { / * ... * / }
.
.
, ,
.
::, :
demo::1 = 100: demo::func2(10):
,
using:
using demo::i:
.
- ,
using namespace:
using namespace demo:
using using namespace
,
:
namespace Department_of_App1ied_Mathematics{
using demo::i:
// ...
}
,
using, ,
using namespace (
, ).
,
,
:
namespace DAM = Department_of__Applied_Mathematics:
.
std. ,
/ <stdio.h>
:
// stdio.h
namespace std{
int feofCFILE '^f):
. ,
, <cstdio>:
// cstdio
namespace std{
int feof(FILE * f ) ;
<cstdio>,
:
std::feof(f);
#include
.
,
(, ,
), , ,
, ,
.
, ,
. ,
.
3
C++ (
), ,
,
,
, .
. ,
.
C++ .
(. 109).
,
.
. ,
. , ,
, ,
.
, ^
.
Better simpler than clever ( -,
-).
, -
3. 103
1. ,
- ,
, , .
, , ,
.
,
. ,
, , :
#1nclude <stdio.h>
main (int t. int _. char *a){return!0<t?t<3?ma1n(-79.-l3.a+main(-87.l-^.
rna1n(-86.0.a+l)+a)): l.t<_ ? ma1n(t+l._.a):3.ma1n(-94.-27+t.a)Mt==2\
?__<l3?ma1n(2.^+l."^s ^d ^n"):9:16:t<0?t<-72?
main(_^/'(an'4-J7*s{}w+/w#cdnr/+.{}r/*de}+7*{*+7w{^+7w#q#n+./#{! + ./n\
{n+./+#n+./# :#q#n+./+k#:*+./'r :d*'3.}{w+K w'K:+}e#';dq#'l q#'+d'K#!\
/+k#;q#'r}eKK#}w'r}eKK{nl]7#:#q#n'){)#}w'){){nl]7+#n';d}rw' 1:# ){nl]!\
/n{n#': r{#w'r nc{nl]7#{l. + 'K (rw' iK{;[{nl]7w#q#n'wk nw' 1wk{KK{n1]!/\
w{^4##w#' 1: :{nl]7*{q#'ld;r'}{nlwb!/*de}'c ; ; { m ' - { } r w ] 7 + . } # ' * } \
#nc.'.#nw]7+kd'+e}+;#'rdq#w! nr7 ') }+}{rl#*{n' ' ) # } ' + } # ( ! ! / " )
:t<-50?_==*a?putchar(31[a]):
ma1n(-65._.a+l):
rTiain((*a==7')+t.__.a+l):
0<t?ma1n(2.2."^s")
:*a==7' ||main(0.main(-61.*a.
"!ek;dc 1@bK'(q)-[w]*^n+r3#l.{}:\nuwloca-0:m .vpbks.fxntdCeghiry"
).a+l);
}
, ,
, :
On the f i r s t day of Christmas my true love gave to me
a partridge in a pear tree.
On the second day of Christmas my true love gave to me
two turtle doves
and a partridge in a pear tree.
On the third day of Christmas my true love gave to me
three french hens, two turtle doves
and a partridge in a pear tree.
^ , .
-
.
^ , .
104 I.
.
, ,
.
, ,
.
, ,
, .
,
, .
,
( const).
,
, ,
.
,
,
. ,
11
new, .
.
, const enum (
, ).
, ,
.
. , ,
goto 1 f, ,
,
. -
switch, 1 f,
.
. ,
if (strstr(a. b) > ) { ... }
else i f (strstr(a. b) < 0 ) { ... }
else i f (strstr(a. b) == 0) { ... }
int is^equal = strstr(a. b);
if (is_equal > 0) { ... }
else i f (is_equal < 0) { ... }
else { ... } . / / is^equal == 0
if ,
else :
106 I.
( ), ,
, .
, ,
, , .
,
.
. C++ (, Perl,
, Pascal),
.
. ... ,
[9]. , ,
, -
,
, .
.
, .
^ (
,
, ). ,
f .
, ,
, .
.
, .
/ /
/ / .
{ / *
* / }
// ^
3-4 ,
. ,
, .
, :
#inc1ude <str1ng.h>
#1nc1ude <stcnib.h>
1nt main(){
^ , .
108 I.
double m[10]; //
const char s[] = "2. 38.5. 70, 0. 0. 1"; //
char *p = s: //
int 1=0; //
//
do{
m[1++] = atof(p):
i f (1 > 9) break;
}wh1le(p = strchr(p. . ' ) P++);
for( i n t = 0; k<1; k++)
p r i n t f ( " ^ 5 . 2 f ". m[k])
return 0;
:
if ( 1s_best ) bestO;
else i f ( is_bad ) worseO;
else i f ( 1s_vovochka ) worstO;
:
wh1le(l){
wh1le(gets(s)){
f o r d = 0; KIO; i++){
f o r ( j = 0; j<10; j++){
/ /
} / / f o r ( j = 0; j<10; j++)
} / / f o r d = 0; KIO; i++)
} / / wh1le(gets(s))
} // whiled)
He . ,
:
f=a+b;// ! f = + ;
C++ . [9].
, 1000 .
[17]. : "
C++?" "
?". : ", " ".
". C++,
, .
3. 109
, .
, .
- , .
.
, , :
,
, .
, , ,
, . ,
, ,
, .
,
, ,
. ,
, .
,
^.
,
,
.
,
.
,
. , ,
, ,
. ,
^, ,
,
.
, .
^ .
^ . ,
.
110 I.
,
,
. ,
, ,
,
. ,
,
.
, ,
: , ,
. , ,
,
.
, .
, .
,
.
I . .
,
, .
,
( : ),
. ,
.
(. 3.1), 1973
, .
,
, :
(, , ,
, )^;
, ;
;
.
, ,
.
II . .
, , .
^ .
3. 111
,
, ,
. , ,
, ,
.
( ).
j/^-x
. 3.1.
III . (
).
, :
, .
.
,
, ,
, .
,
.
,
.
112 I.
, .
,
.
.
, ,
- .
, , , .
,
. ,
,
; .
, , ,
.
,
.
IV . .
:
,
.
, , .
,
.
, ,
.
,
,
,
.
-,
(
, ,
).
,
.
-, ',
,
, .
,
.
C++ (
)
. ,
3. 113
.
(, , )
.
.
:
, ,
. ,
,
.
V . . ,
, .
.
.
, .
, ,
.
,
. , .
,
,
, ,
.
.
. ,
, ,
. ,
, (
, >10 , ,
10).
.
,
, .
, ,
. .
,
,
.
, ,
, ,
.
114 I.
, , ,
,
, . ,
,
,
. ,
,
,
, .
,
.
,
,
.
, C++,
. ,
, , ,
.
(
,
),
new mall (
).
.
,
,
, .
,
.
, ,
.
.
^ .
, , ,
, . ,
, .
3. 115
, ,
.
( struct), :
. .
: , .
(, ) :
struct Node{
Data d; // Data
Node *:
}:
(
, . . 211).
,
.
().
, (),
, .
, .
,
. {)
. , ,
, , ,
:
, , ,
. .
:
( );
;
;
(
);
;
.
.
. .
, ,
:
116 I.
Struct Node{
1nt d:
Node *next;
Node *prev:
}:
, 5 ,
, .
pbeg, pend,
pv .
#incTude <1estream.h>
struct Node{
int d;
Node *next:
Node *prev:
}:
// ---- - - --
Node * first(int d);
void add(Node **pend. int d);
Node * find(Node * const pbeg. int i);
bool remove(Node **pbeg. Node **pend, int key);
Node * insert(Node * const pbeg. Node **pend. int key. int d):
// --- ---
int main(){
Node *pbeg = first(l): //
Node *pend = pbeg: // ,
// 2, 3. 4. 5:
for (int i = 2; i<6; i++)add(&pend, i):
// 200 2:
insert(pbeg. &pend. 2. 200):
// 5:
if(!remove (&pbeg. Spend. 5))cout " ":
Node *pv = pbeg:
while (pv){ //
cout pv->d ' ':
pv = pv->next:
}
return 0:
}
//
//
Node * first(int d){
Node *pv = new Node:
pv->d = d: pv->next = 0: pv->prev = 0:
return pv:
}
//
3. 117
//
void add(Node ^*pend. 1nt d){
Node '^pv = new Node;
pv->d = d: pv->next = 0; pv->prev = '^pend;
('*^pend)->next = pv;
*pend = pv;
}
//
//
Node * find(Node * const pbeg. int d){
Node *pv = pbeg;
while (pv){
if(pv->d == d)break;
pv = pv->next;
}
return pv;
}
//
//
bool remove(Node **pbeg. Node **pend. int key){
if(Node *pkey = find(*pbeg. key)){ III
if (pkey == *pbeg){ // 2
*pbeg = (*pbeg)->next;
(*pbeg)->prev = 0 ; }
else if (pkey == '^pend)! // 3
^pend = (*pend)->prev;
(*pend)->next = 0 ; }
else{ // 4
(pkey->prev)->next = pkey->next;
(pkey->next)->prev = pkey->prev;}
delete pkey;
return true; // 5
}
return false; // 6
}
//
//
Node * insert (Node '^ const pbeg. Node ^'^^pend. int key. int d){
if(Node *pkey = find(pbeg. key)){
Node '^pv = new Node;
pv->d = d;
// 1 - :
pv->next = pkey->next;
// 2 - :
pv->prev = pkey;
// 3 - :
pkey->next = pv;
118 I.
// 4 - :
1f( != *1) (pv->next)->prev = pv;
/ / ,
/ / :
else *pend = pv:
return pv:
}
return 0:
}
:
1 2 200 3 4
, ,
const. , (,
), .
remove.
,
. 1 ,
f 1 nd.
, .
, 1 f (
), 2,
false ( 6).
- ,
, . 2 ,
pbeg ,
, next .
0.
( 3),
pend ,
prev . ,
.
, , ,
. -
poBijH - ,
true.
. 3.2.
.
. , ,
, .
3. 119
pkey->next
. 3.2.
(,
):
void acld_sort(Node **pbeg. Node **pend. 1nt d){
Node *pv = new Node; / /
pv->d = d;
Node * pt = *pbeg;
while (pt){ / /
i f (d < pt->d){ / / (pt)
pv->next = pt:
i f (pt == *pbeg){ / /
pv->prev = 0:
*pbeg = pv:}
else{ / /
(pt->prev)->next = pv:
pv->prev = pt->prev:}
pt->prev = pv:
return:
}
pt = pt->next:
}
pv->next = 0; //
pv->prev = *pend:
(*pend)->next = pv:
*pend = pv:
,
,
. .
. , LIFO
120 I.
//
//
int pop (Node *){
3. 121
,
, .
. .
, FIFO (first in first out,
). ,
-. ,
, ,
/.
,
. add,
del. !! pbeg,
pend.
#include <iostream.h>
struct Node{
1nt d:
Node *p:
}:
Node * firstdnt d):
void addCNode **pend. int d):
int del(Node **pbeg):
// -----
int main(){
Node *pbeg = first(l):
Node *pend = pbeg:
for (int i = 2: i<6: i++)add(&pend. i):
while (pbeg)
cout deU&pbeg) ' ':
return 0:
}
// -
//
Node * first(int d){
Node *pv = new Node:
pv->d = d:
pv->p = 0:
122 I.
r e t u r n pv;
}
//
/ /
void add(Node **pend. i n t d)(
Node *pv = new Node;
pv->d = d:
pv->p = 0:
(*pend)->p = pv:
*pend = pv;
//
/ /
i n t del(Node **pbeg){
i n t temp = (*pbeg)->d;
Node *pv = *pbeg;
*pbeg = (*pbeg)->p:
delete pv;
return temp;
}
:
12 3 4 5
, ,
, ,
. .
.
3.3 (
, , ) i. , ,
. ,
. ,
.
,
,
, . .
^,
^ ,
, .
^ ,
,
. ,
.
.
3. 123
.
,
,
.
6 25
/^ "^ /^ "^
( \ i \
1 8 ! 20 30
-^
\
21
. 3.3.
,
.
. ,
:
function way__arouncl ( ){
way^around ( )
way_around ( )
, , ,
,
,
, .
, . 3.3:
1. 6. 8. 10. 20. 21. 25. 30
124 I.
,
:
30. 25. 21. 20. 10. 8. 6. 1
, .
.
:
;
;
;
.
.
7 .
. ,
,
.
,
^.
.
#1nclude <1ostream.h>
struct Node{
1nt d:
Node '^left;
Node * r i g h t :
}:
Node * f i r s t d n t d);
Node * searchJnsert(Node *root. 1nt d);
void pr1nt_tree(Node '^root. 1nt 1);
// _-
1nt main(){
1nt b[] = {10. 25. 20. 6. 21. 8. 1. 30}:
Node *root = f 1 r s t ( b [ 0 ] ) :
for (1nt 1 = 1: 1<8: 1++)search_1nsert(root. b [ 1 ] ) :
print_tree(root. 0):
return 0:
}
//
/ /
Node * f i r s t d n t d){
Node *pv = new Node:
. , + =
[8].
3. 125
pv->d = d;
pv->left = 0:
pv->right = 0;
return pv;
}
II//
//
Node * searchJnsertCNode *root. int d){
Node *pv = root, *prev;
boo! found = false:
while (pv && !found){
prev = pv;
if (d == pv->d) found = true;
else i f (d < pv->d) pv pv->left;
else pv pv->right;
}
if (found) return pv;
// :
Node *pnew = new Node;
pnew->d = d;
pnew->left = 0;
pnew->right = 0:
if (d < prev->d)
// :
prev->left == pnew;
else
// :
prev->right = pnew;
return pnew;
} '
// ----
//
void print_tree(Node *p. int level){
i f (P){
print_tree(p->left. level +1); //
for (int i = 0; i<level; i++)cout " ";
cout p->d endl; //
print_tree(p->right. level +1); //
}
}
pv, pv
prev, pnew
. ,
(prev) , ,
.
126 I.
, . 3.3:
1
6
8
10
20
21
25
30
.
, , .
0. ,
( ,
, . 3.3).
, .
,
. ,
.
,
, ,
. , ,
.
, .
(, . 3.3 25,
21 30, 10 20 8, . .).
.
,
(,
),
. ,
11 ,
.
. ,
,
, .
, .
, .
3. 1 27
, :
10 25 20 6 21 8 1 30 -
1 2 3 4 5 6 7 - 1 -
0 -
1- i-ro
.
. :
10 25 20 6 21 8 1 30 -
2 7 4 5 1 0 3 - 1 -
6 -
( ).
. , ,
. 3.3, :
10 25 20 6 21 8 1 30 -
3 2 - 1 6 - 1 - 1 -1 -1 -
1 7 4 5 -1 -1 -1 -1 -
,
, ,
:
struct Nodej
Data d: // Data
1nt 1;
}:
Node sp1sokl[lOOO]; //
Node *pspisok2 = new Node[m]: //
(,
),
.
I
1
F
. . dX.
^ + b X <0 b 1^0
X -
X >0 b =0
X -
X
L
, , .
F ,
( ) ( )
, . ,
, , , .
, , , ., ., dX .
2
F
. . dX.
- b x + 5 <0 =0
X - X -\-5 >0 4^0
F=
X
10
-
, , .
F ,
( ) ( )
, . ,
, , , .
, , , ., ., dX .
I 129
3
F
. . dX.
^ -\- - <0 ^0
.
F= > =0
X -
( + )
,, .
F ,
( )
, . ,
, , , .
, by , ., ., dX .
4
F
. . dX.
- - <Qu X ^
f = X - > W =
-
1-
, , .
F ,
, . ,
, , , . , ,
, ., ., dX .
5
F
. . dX,
- X <0 b
10 + 6
X - X >0 b =0
X -
+ -
,, .
130 I.
F ,
( )
, . ,
, , , .
<, , , ., ., dX .
6
F
. . dX.
<0 b ^0
X -\-
F= b =0
X
X
I
, , .
F ,
( ) ( )
, . ,
, , , .
, , , ., ., dX .
7
F
. . dX.
- b X <5 ^Q
F = X - X > 5 W =
X
- X
,, .
F ,
( ) 2 ( )
, . ,
, , , , 02 (
2) . , , , ., ., dX .
8
F
. . dX.
I 131
- <0 ^
f = - (3=0
X
L
.
F ,
( 2 ) ( )
, . ,
, , , , 2 (
2) . , , , ., ., dX .
9
F
. . dX.
1+
, , .
F ,
( ) ( )
, . ,
<, , , , .
, , , ., ., dX .
10
F
. . dX.
^ - bx \- X < 3 w 6 ^
X -
F= X >3 b =0
X -
X
L
, , .
F ,
132 I.
( ) ( 2 )
, . ,
, , , , , 2 (
2) . , , , ., ., dX
.
11
F
. . dX.
b
^ + X <1 ^0
X -
X > 15 =0
-^2
, by .
F ,
( ) 02
, . ,
, , , 2 (
2) . , , , ., ., dX .
12
F
. . dX.
^ - ' + X <Q&u - ^0
-
F= x>0.6wfe + c = 0
X -
X X
+
.
, , .
F ,
( )
, . ,
, , , .
(2, by ., ., dX .
13
F
. . dX.
I 133
^+ - i<Oub- xj^O
- i>Ou + =0
X
X
I
, , .
F ,
( ) 2 ( )
, . ,
,fc,, , 2 (
2) . , 6, , ., ., dX .
14
F
. . dX.
- X -\- <0 ^
X -
F= X-i- =0
X -
X
+
X
, , .
F ,
( 2 ) ( 2 )
, . ,
, , , 2 (
2) . , , , ., ., dX .
15
F
. . dX.
- ^ - b X <0
X
F= + 5.5 X > / 6 =
X -
X
-
, 6, .
F 1 ,
134 I.
( )
, . ,
, , , .
, 6, , ., ., dX .
16
F
. . dX.
{ + )^ - b X =0 b ^0
X -
X =0 b =0
-
, by .
F ,
( 2 ) ( )
, . ,
, , , , , 02 (
2) . , , , ., ., dX
.
17
F
. . dX.
X <Qu b ^Q
-
F= X >0 b =0
X -
+5
[(~ 10)
, , .
F ,
( )
, . ,
, , , . (2,
6, , ., ., dX .
19
F
. . dX.
a(x + 7 ) ^ - b <5^0
f = X >5 b =0
X
I
, , , d .
F ,
( 02 ) ( 02 )
, . ,
, , , 02 (
2) . , , , d, ., ., dX .
20
F
. . dX.
2-
X <0 b ^0
-
X -
JF = X >0 b =0
X -
X -
+
2
. .
F ,
136 I.
( ) ( )
, . ,
, , , , .
, , , ., ., dX .
. .
1
, , :
1) ;
2) ,
.
.
2
, , :
1) ;
2) ,
.
.
3
, , :
1) ;
2) ,
.
,
, (, ,
).
4
, , :
1) ;
2) ,
.
, , 1.
.
, , :
1) ;
I 137
2) ,
.
, ,
[,]. .
6
, , :
1) ;
2) ,
.
,
, , .
7
, , :
1) ;
2) ,
.
,
, , ~
, .
8
, , :
1) ;
2) ,
.
,
, 1, .
9
, , :
1) ;
2) ,
.
, , ,
.
10
, , :
1) ;
2) , ,
.
138 I.
,
, , ,
.
11
, , :
1) ;
2) ,
.
, ,
[,]. .
12
, , :
1) ;
2) ,
.
,
, [,], .
13
, , :
1) , ;
2) , .
.
14
, , :
1) , 0;
2) , .
.
15
, , :
1) , ;
2) ,
.
,
, (, ,
).
16
, , :
1) ;
I 139
2) ,
.
.
17
, , :
1) ;
2) , ,
.
,
, 1, .
18
, , :
1) , ;
2) ,
.
,
, 20%,
.
19
, , :
1) ;
2) ,
.
.
20
, , :
1) ;
2) , .
, ,
, .
1
. :
1) , ;
2) , .
140 I.
2
. ,
.
. ,
.
3
. :
1) , ;
2) , .
4
. :
1) ,
;
2) ,
.
5
. :
1) , ;
2) ,
.
6
. :
1) ,
;
2) .
, i-
j- .
7
8 8 , k-
^- .
,
.
. ,
.
I 141
,
.
9
i-1 < < i+1, j - 1 < 1 < j+1,
(, 1) ^ (i, j).
,
.
10 10.
,
.
10
,
.
10 10.
, .
11
.
.
,
.
12
, ,
. ,
.
13
( ),
.
14
MxN k : 1-
, ,
, ;
.
15
.
, .
. ,
.
142 I.
16
.
,
.
17
, ,
(2,2), (3,3)
. ., .
,
.
18
. :
1) , ;
2) ,
.
19
. :
1) cjnviMy , ;
2) ,
.
20
. :
1) ,
;
2) .
i-
^- .
1
.
, .
2
,
, , -
I 143
,
.
3
.
, , ,
, .
4
.
,
.
5
.
,
.
6
. ,
(>=3)
.
7
.
,
( ) .
8
.
, 1 ,
, .
9
,
. ,
.
10
,
,
.
11
,
.
144 I.
12
d .
, , ,
abed .
13
,
,
.
14
. ,
. ,
.
15
,
.
16
, ,
.
17
.
,
.
18
R
.
, ,
.
19
,
.
20
, .
.
1
1. STUDENT, :
;
I 145
;
( ).
2. , :
,
STUDENT;
;
,
, 4,0;
, .
2
1. STUDENT, :
;
;
( ).
2. , :
,
STUDENT;
;
,
4 5;
, .
3
1. STUDENT, :
;
;
( ).
2. , :
,
STUDENT; ;
,
2;
, .
4
1. AEROFLOT, :
;
;
.
2. , :
146 I.
,
AEROFLOT;
;
,
, ,
;
, .
5
1. AEROFLOT, :
;
;
.
2. , :
,
AEROFLOT;
;
,
, ;
, .
6
1, WORKER, :
;
1;
.
2. , :
,
WORKER; .
,
, ;
, .
7
1. TRAIN, :
;
;
.
2. , :
,
TRAIN;
;
I 147
,
;
, .
8
1. TRAIN, :
;
;
.
2. , :
,
TRAIN;
;
, ,
;
, .
9
1. TRAIN, :
;
;
.
2. , :
,
TRAIN; ;
, ;
, .
10
1. MARSH, :
;
;
.
2. , :
,
MARSH; ;
,
;
, .
148 I.
11
1. MARSH, :
;
;
.
2. , :
,
MARSH; ;
,
, ;
,
.
12
1. NOTE, :
, ;
;
( ).
2. , :
,
NOTE; ;
,
;
, .
13
1. NOTE, :
, ;
;
( ).
2. , :
,
NOTE; ;
,
, ;
, .
14
1. NOTE, :
, ;
I 149
;
( ).
2. , :
,
NOTE;
;
, ;
, .
15
1. ZNAK, :
, ;
;
( ).
2. , :
,
ZNAK; ;
,
;
, .
16
1. ZNAK, :
, ;
;
( ).
2. , :
11 ,
ZNAK; ;
, ,
;
, .
17
1. ZNAK, :
, ;
;
( ).
2. , :
150 I.
,
ZNAK; ;
, ,
;
, .
18
1. PRICE, :
;
, ;
.
2. , :
,
PRICE;
;
,
;
, .
19
1. PRICE, :
;
, ;
.
2. , :
,
PRICE;
;
, ,
;
, .
20
1. ORDER, :
;
;
.
2. , :
,
ORDER;
;
I 151
,
, ;
,
.
,
.
,
.
.
.
1
:
struct scan_info{
char model[25]; //
i n t price: //
double x_s1ze: //
double y__s1ze: //
1nt optr: //
int grey: //
}:
,
. :
int, ;
.
,
scaninfo. .
, , - 1
.
, (
) 6-8
.
. .
2
,
1.
152 I.
,
. :
int, ;
.
,
: .
, .
, (
)
.
. .
3
,
1.
,
. :
1 ong, ;
.
,
.
, (
)
.
. .
4
,
1.
,
( ),
. :
int, ;
.
,
scan_info.
. ,
, - 1 .
, (
)
.
. 11 .
I 153
5
,
1.
,
. :
int,
; .
,
( ) , .
,
, .
, (
)
.
. .
6
,
1.
,
( ),
. :
int, ;
.
,
.
.
, , -1 .
, (
)
.
. .
7
, -
1.
,
. :
int, ;
.
,
scan^info, , -
154 I.
;
.
, (
) 6-8
2-3 , .
1
. .
8
,
1.
,
. :
int, ;
.
,
scaninfo .
. , ,
, , . .
, (
) 6-8 1-2
, .
. .
9
,
1.
, ,
,
(
). .
,
. 200,
.
, ,
, .
: long,
;
.
,
( ,
).
. .
I 155
10
(
):
struct NOTEBOOK{
char model[21]; //
struct s1ze{ / /
float x:
float y;
float z;
}:
float w; //
1nt price: //
}
, note.txt (.
. 158) . ,
. :
() ;
NOTEBOOK.
,
,
120 , .
11
,
10.
, note.txt (.
. 158) . ,
. :
() ;
NOTEBOOK.
,
, HDD
1 ,
.
12
,
10.
, note.txt (.
. 158) . ,
. :
() ;
NOTEBOOK.
156 I.
,
,
285 .
13
,
10.
, note.txt (.
. 158) . ,
. :
() ;
NOTEBOOK.
,
,
40 , .
14
,
10.
, note.txt (.
. 158) . ,
. :
;
NOTEBOOK.
,
11 ,
.
15
(
):
struct NOTEBOOK!
struct cl1sp__res{ / /
int : / /
int : / /
}:
int f: / /
float d; / /
int price; / /
char model[21]; //
}
, note.txt (.
. 158) . .,
. :
I 1 57
;
NOTEBOOK.
, -
,
7 , .
16
,
15.
, note.txt (.
. 158) . ,
. :
;
NOTEBOOK.
,
,
2 ,
.
17
,
15.
, 11 note.txt (.
. 158) . ,
. :
;
NOTEBOOK.
,
, HDD
1 ,
.
18
,
15.
, note.txt (.
. 158) . ,
. :
;
NOTEBOOK.
,
,
120,
.
158 I.
19
(
):
struct NOTEBOOK{
struct clisp_res{ / /
i n t ; / /
i n t ; / /
}:
1nt f; //.
float d: //
float hdd: //
char model[21]: //
}
, note.txt
. ,
. :
; NOTEBOOK.
, 11
,
120, .
20
,
19.
, note.txt
. ,
. :
; N O T E B O O K .
,
,
$3500, .
note.txt:
Acer Note Light 2699 5.6 02.0x11.8x08.3 100 40 10.4 1 1024x0768 60 0.774
ASW ND5123T 3489 7.2 02.3x11.8x10.1 133 32 12.1 2 1024x0768 70 1.300
ARMNote TS80CD 3699 7.2 02.0x11.5x08.8 133 64 11.3 1 1024x0768 75 1.300
AST Ascent1a P50 4499 7.5 02.3x11.3x09.0 133 40 11.3 1 0800x0600 70 0.774
BSI NP8657D 2605 8.0 02.3x11.8x09.3 133 40 11.3 1 1024x0768 60 0.810
BSI NP5265A 3765 8.2 02.5x12.0x09.0 150 32 12.1 2 1024x0768 70 1.300
Dell Xpi PIOOSD 3459 6.0 02.3x11.0x08.8 100 40 10.3 1 1024x0768 60 0.773
Digital HiNote 4799 4.0 01.3x11.0x08.8 120 40 10.4 1 0800x0600 56 1.000
Gateway Solo S5 4499 5.6 02.0x11.9x08.8 133 40 11.3 2 1024x0768 60 0.686
Hertz Z-Optima NB 3995 8.0 02.3x11.9x09.0 150 40 11.2 2 1024x0768 75 1.000
HP Omni Book 5500 6120 7.1 02.0x11.5x09.0 133 64 11.4 1 1024x0768 75 1.300
IBM ThinkPad 560 3749 4.1 01.3x11.8x08.8 120 40 12.1 2 1024x0768 85 0.774
NEC Versa 4080H 4780 6.6 02.3x11.8x09.5 120 48 10.4 1 0800x0600 70 0.776
Polywell Poly 500 3300 7.9 02.3x11.9x09.0 120 40 10.4 1 1024x0768 72 1.000
I 159
Samsung SENS 810 3667 8.7 02.3x11.5x09.5 100 32 11.4 2 1024x0768 75 0.773
Twinhead Slimnote 2965 7.4 02.0x11.5x08.0 075 64 10.4 1 1024x0768 70 0.772
note.txt .
.
:
1 : 20 ;
21 : 24 ( );
26 : 28 ( );
30 : 43 (-
tpn (4 , , '');
44 : 47 ( );
49: 50 ( );
52 : 55 (
, );
57 ;
59 : 67 ( ,
'');
69 : 70 ( );
72 : 76 HDD (
).
.
1
, ,
1000 ( 70
).
DAT.
, :
;
;
.
2
, ,
1000 ( 70
).
DAT.
, :
;
160 I.
;
.
3
, ,
1000 (
70 ).
DAT.
, :
;
, ;
, .
4
, ,
1000 .
( 70 ).
DAT.
, :
;
2, 1,3.
5
, ,
1000 (
70 ).
DAT.
, :
;
, ;
,
.
6
, ,
1000 (
70 ).
DAT.
, :
;
I 161
, ;
, .
7
, ,
1000 (
70 ).
DAT.
, :
;
;
, .
8
, ,
1000 (
70 ).
DAT.
, :
;
;
, .
9
, ,
1000 (
70 ).
DAT.
, :
;
,
;
, ,
.
10
, ,
1000 (
70 ).
162 I.
DAT.
, :
;
;
, .
11
, ,
1000 (
70 ).
DAT.
, :
;
'' ;
,
''.
12
, ,
1000 (
70 ).
DAT.
, :
;
(,
);
, .
13
, ,
1000 (
70 ).
DAT.
, :
;
( );
, .
I 163
14
, ,
700 (
70 ).
DAT.
, :
;
( );
,
.
15
, ,
700 (
70 ).
DAT.
, :
;
, (
);
,
.
16
, ,
700 (
70 ).
DAT.
, :
;
( );
, ,
.
17
, ,
700 (
70 ).
DAT.
164 I.
, :
;
,
,
.
.
18
, ,
500 (
70 ).
DAT.
, :
;
;
,
.
19
, ,
, 700 (
70 ).
DAT.
, :
;
( );
,
.
20
, ,
1000 (
70 ).
DAT.
, :
;
;
.
I 165
,
.
.
. ,
int, float double.
,
,
.
1
,
.
:
;
;
.
:
{2 ;
,
, ,
, ;
,
,
, , ;
, ,
, .
2
,
.
:
;
;
;
;
.
166 I.
:
;
, ;
;
,
.
3
,
.
:
;
;
;
.
:
;
;
;
;
.
4
,
.
:
;
;
;
.
:
;
;
;
.
5
,
.
I 167
:
;
;
;
;
.
:
;
,
,
, ;
,
;
.
6
,
.
:
;
;
;
, .
:
;
,
;
,
;
, ,
, .
?
, .
, .
, . .
, , .
.
, .
. .
168 I.
8
,
.
, , ,
, , ,
.
, .
,
.
.
,
.
9
, .
360 .
18 32 . ,
.
.
,
, ,
, .
, ,
.
, , ,
.
.
:
.
10
.
:
;
;
.
, :
;
;
, ;
.
.
I 1 69
11
.
,
. , ,
.
, :
;
;
.
.
12
.
() ,
. ,
, .
, :
;
;
.
.
13
.
:
;
;
;
.
, :
;
;
:
10%
,
;
.
.
170 I.
14
- .
,
.
.
.
, :
;
: )
; )
; ) . )
;
.
.
15
.
:
;
;
(, , ).
, ,
.
, :
;
: )
40 , ,
; ) 30 , ,
; ) 25 ,
, ;
.
.
16
,
.
:
I 171
;
;
;
;
.
:
;
, ;
;
,
.
17
,
, .
, :
;
;
;
.
.
18
,
, .
, :
;
;
;
.
.
19
.
:
;
;
.
172 I.
.
, Kol-opan:
;
;
;
,
.
.
20
.
:
;
;
.
.
, :
;
;
;
,
.
.
II
. , ,
, , ,
. ,
. ,
, ,
.
, ,
.
.
, .
. ,
.
,
,
. ,
, .
.
, .
,
.
,
174 II. -
, ,
.
,
.
, ,
.
.
. lOiacc
.
, , .
,
,^
.
^ .
-
( ) .
, . ,
,
, ,
, . .
,
( ,
, ,
, ,
, ^).
. ,
, .
-
(). Simula-67^
Smalltalk, -
. C++
, ,
.
, ( C++
, , -
^ , . C++
.
2
^ : ...
il. - 175
,
), .
, ,
, .
,
, . -
.
,
.
,
. ,
.
, ,
, ,
10 ;
Microsoft Word,
,
. : ,
, (
, ),
.
,
. ,
. ,
.
. \|:),
- ,
^
- ,
.
.
(),
:
( , ) .
^ -
( Windows
API).
176 . -
,
. ,
^ .
(encapsulation).
, .
:
,
. ,
,
(,
). ,
, .
, ; ,
.
,
, .
,
. -
,
.
,
,
. C++
. ,
.
, ,
.
C++ .
,
.
(
), ,
.
( . 205).
.
II. - 177
, ,
,
, , .
. -
,
.
.
, , .
,
- .
, , , ,
,
. ,
,
^.
^ , ,
cin cout.
,
.
4
,
-
.
,
, , (. ).
, ,
.
( ),
^. .
:
class <>{
[ private: ]
< >
public:
< >
} ; / /
private i
. , private,
. . -
^ - -,
,
. , ,
- , ,
.
4. 179
public.
.
private public, .
:
, (
);
const,
( ) ;
static (
, . 186), auto, extern register.
.
( )
( , , ).
:
, (static)
(extern) ,
, ;
;
;
;
, -
.
,
. (, ,
) . ,
, .
class monstr{
int health, ammo:
public:
monstr(int he = 100. int am = 10){ health = he: ammo = am:}
void drawCint x. int y, int scale, int position):
int get_health(){return health:}
int get_ammo(){return ammo:}
}:
health ammo,
get_health() get_ammo().
,
,
, . ,
,
.
180 II. -
,
, private
.
( draw). ,
(inline). , .
() ,
(::):
void monstr::draw(int . i n t . i n t scale, int position)!
/ * * /
}
inline ( , ):
i n l i n e i n t monstr::get_ammo(){
return ammo:
}
,
.
. .
,
.
, . 182.
struct union ,
, . 209. ,
. 211.
,
.
+-:
monstr Vasia: / / monstr
monstr Super(200. 300): / /
monstr stado[100]: / /
monstr *beavis = new monstr (10): / /
//( )
monstr &butthead = Vasia: / /
,
eib , ,
. .
, (
. 188).
4. 181
.
. ()
-> , :
int = Vasid.get_ammo();
stado[5].draw;
cout beavis->get_health();
ic.
private
.
,
. :
class monstr{
this
.
,
, .
this,
. this
.
(return this;) (return
*this;) .
this
monstr , (
health) , ,
( public
):
monstr & the_best(monstr &){
if( health > .health) return nhis:
return M:
182 II. -
. .
7ie , void.
.
( ).
, ,
.
, .
.
.
,
.
(.
, . 201). ,
, ,
,
.
.
const. virtual static.
main.
,
. (
, ).
, -
:
_ _ [( )];
/ /
_ ( );
4. 183
// ( )
^ _ = ;
//
:
monstr Super(200. 300). Vas1a(50). Z:
monstr X = monstr(lOOO):
monstr Y = 500;
.
.
health = 1000 ( ).
X, .
health = 500 ( ).
Y, .
,
.
monstr, , (skin)
(name):
enum color {red, green, blue}; //
class monstr{
int health, ammo;
color skin;
char *name;
public:
monstr(int he = 100. int am =10);
monstr(color sk);
monstr(char * nam);
int get_health(){return health;}
int get_ammo(){return ammo;}
}: '"
//---- - --
monstr::monstr(int he, int am){
health = he; ammo = am; skin = red; name = 0;
//---- -
monstr::monstr(color sk)(
switch (sk){
case red health = 100 ammo *= 10 skin = red; name = 0; break
case green health = 100 ammo = 20 skin =* green; name = 0; break
case blue health = 100 ammo = 40 skin = blue; name = 0; break
}
184 il. -
// -
monstr::monstr(char * nam){
name = new char [strlen(nam) + 1];
// 1 -
strcpyCname. nam):
health = 100: ammo = 1 0 : sk1n = red:
}
//----
monstr * m = new monstr ("Ork"):
monstr Green (green):
, . monstr
,
.
,
.
, .
(
) ,
:
monstr::monstr(1nt he. i n t am):
health (he), ammo (am), skin (red), name (0){}
.
, .
-, - -
, ,
.
,
.
(. , . 228).
,
:
T::T(const &) { ... / * V }
~ .
4. 185
,
:
;
;
^
,
.
. , , ,
, ,
.
monstr.
name, ,
:
monstr::mcnstr(const monstr &){
i f (M.name){
name = new char [strlen(M.name) + 1 ] :
strcpyCname. M.name):}
else name = 0;
health = M.health; ammo = M.ammo; skin = M.skin;
}
, - ,
,
.
, ,
, . 201.
static
. ,
.
' .
186 II. -
,
, ,
.
, .
.
( )
,
( ):
class {
public:
s t a t i c i n t count: //
}:
i n t A::count: //
//
// i n t A::count = 10:
, :
* . :
.
,
this. ,
, ,
, .
class {
static int count: // count -
public:
static void inc_count(){ count++: }
}:
4. 187
A::1nt count: //
void f(){
:
// a.count++ - , count
// :
a.inc_count(): // : :inc__count():
}
(const)
(virtual).
, 1 .
.
. , ,
,
, ,
, ,
(. . 284).
.
,
, friend.
, this
.
.
, .
.
,
nst. kill hero, steal ammo
.
monstr.
class monstr: / /
class hero{
public:
void kilKmonstr &):
188 II. -
class monstr{
-
,
friend. mistress
hero:
class hero{
void f l O :
void f2():
}
f 1 f2 hero (
friend) .
friend .
, ,
, .
,
, . ,
:
, ;
main:
, ^
delete.
4. 189
(~),
. :
;
const static;
;
(. , . 205).
,
.
,
, ~
, -,
. .
(. 183) :
monstr::~monstr() {delete [ ] name;}
, :
monstr %; ...
m -> -monstrO;
,
new .
.
C++ ,
. ,
. .
, C++, :
.* ?: # ## sizeof
{-) :
,
( ),
;
;
190 II. -
- ;
- ( =*);
- static.
- :
, , .
,
, ^.
- operator,
:
operator ( ) { }
-, ,
,
, :
class monstrf
* : -, ,
.
4. 191
return :
}
int. ,
:
class monstrj
-, ,
,
:
class monstr{
,
:
bool operator >(const monstr &M1. const monstr &M2){
1f( Ml.get_health() > M2.get_health()) return true:
return false:
. ,
. ,
,
. ,
- , ;;1 ,
.
const monstr& operator = (const monstr &M){
/ / :
192 II. -
1f (& == t h i s ) return n h i s :
i f (name) delete [ ] name;
i f (M.name){
name = new char [strlen(M.name) + 1];
strcpyCname. M.name);}
else name = 0;
health = M.health; ammo = M.ammo; skin = M.skin;
return *this;
}
:
monstr (10). . ;
= = ;
.
.
new delete
,
new new[]
, delete
delete [] .
- :
;
new new[]
s1ze_t ( , sizeof,
<stddef.h>);
;
void*,
return ( );
delete void
void*;
.
,
. new ,
,
(
. 222). delete ,
,
-
.
4. 193
(
::
).
,
. , loiacc,
:
class Obj {...}:
class pObjj
private:
Obj *p:
}:
pObj
new
pObj * = new pObj:
sizeof(pObj), new
(
delete):
. new
pObj, ,
Obj. pObj
headOf Free,
.
.
, (union),
,
:
class pObj{
public:
static void * operator new(size_t size):
private:
union{
Obj *p: / /
pObj *next: / /
}:
static const int BLOCK__SIZE://
// :
static pObj *headOfFree:
}:
void * pObj::operator new(size_t size){
//
194 II. -
// new:
if (size != sizeof(pObj)) return ::operator new(size);
pObj *p = headOfFree; //
// :
if () headOfFree = -> next:
// . :
else {
pObj *newblock = static__cast<pObj*>^
(::operator new(BLOCK__SIZE * sizeof(pObj))):
// Bee , (
// ), :
for (int i - 1: i< BLOCKJIZE - 1: ++i)
newblock[i].next = &newblock[i + 1 ] :
newblock[BLOCK__SIZE - l].next = 0:
// :
headOfFree = &newblock[l]:
= newblock:
}
return p; / /
}
new ,
. ( ,
, ), . ,
. ,
new,
new.
, pObj,
( . 186):
pObj *pObj::headOfFree: / /
const i n t pObj::BLOCK_SIZE = 1024:
,
,
.
, new ,
delete (,
delete ,
).
delete
:
void pObj::operator delete(void * ObjToDie. size^t size){
i f (ObjToDie *== 0) return:
i f (size != sizeof(pObj)){
^ staticcast.
static_cast . 237.
4. 195
-,
. :
operator ^ :
.
.
:
.monstr::operator 1nt(){return health;}
, ,
. :
class if_greater{
public:
1nt operator (1nt a. int b) const {
return a > b:
}
}:
.
:
if_greater :
cout (1. 5) endl; // -
cout 1f__greater()(5. 1) endl; // - 1
if_greater
, (1, 5) (
X.operator (1. 5)). ,
, .
1f_greater()
if_greater.
1f_greater. ,
, ,
.
196 ii. -
( ) .
.
C++.
[ ] ,
, .
,
. Vect,
:
#1nclucle <iostream.h>
#1nclude <stdlib.h>
class Vect{
public:
explicit Vect(int n = 10):
VectCconst int a[]. int n): //
-VectO { delete [] p: }
int& operator [] (int i):
void PrintO:
private:
int* p:
int size:
}:
Vect::Vect(int n) : size(n){
p = new int[size]:
}
Vect::Vect(const int a[]. int n) : size(n){
p = new intCsize]:
for (int i = 0: i < size: i++) p[i] = a[i]:
}
// :
int& Vect::operator [] (int i){
if(i < 0 II i >= size){
cout " (i = " i " ) " endl:
cout " " endl:
exit(O):
}
return p[i]:
}
void Vect::Print(){
for (int i = 0: i < size; i++)
cout p[i] " ";
cout endl:
}
4. 197
int ma1n(){
int [10] = {1. 2. 3. 4. 5. 6. 7. 8. 9. 10};
Vect (, 10);
a.PrintO;
cout a[5] end!;
cout a[12] endl;
return 0;
}
:
1 2 3 4 5 6 7 8 9 10
6
(1 = 12)
,
. ,
, .
explicit , (.
. 185), . explicit ,
.
[] .
.
.* ->*.
-.
:
^ (^::*_)();
, monstr
int get__health() {return health;}
int get^ammoO {return ammo:}
(a ) :
int (monstr:: *pget)():
.
:
void fun(int (monstr:: *pget)()){
(*this.*pget)(): // .*
(this->*pget)(): // ->*
}
:
198 II. -
// :
pget & monstr::get_health;
monstr Vasia. *p;
p = new monstr;
// .* :
int Vasin^health - (Vasia.*pget)();
// ->* :
int p__heaUh = (p->*pget)();
.
,
.
.
,
.
,
, , .
,
.
, .
.
, , .
, , .
:
_(_::*_):
:
&_: :__;// public
health public,
:
int (monstr::*phealth) &nranstr::health:
cout Vasia.*phealth: / / .*
cout p->*phealth: / / ->*
,
, ,
.
, , ,
(private) :
4. 199
, , ;
, ( ,
, const,
, );
, , ,
, ;
,
( . 228).
,
( ),
, ,
,
:
namespace Staff{
class monstr { / * ... * / } :
class hero { / * ... * / } ;
void interact(hero, monstr):
}
5
,
, ,
.
.
,
.
, ,
.
.
.
,
.
private, protected public:
class : [private | protected | public] _
{ };
, .
, :
class { ... };
class { ... };
class { ... };
class D: . protected . public { ... }:
private,
public.
5. 201
-
private public.
protected, ,
, private.
, :
private private
protected private
public private
protected private
protected protected
public protected |
public private
protected protected
public public 1
, private
.
.
protected private
private, .
public
.
private,
,
public
:
class Base{
,
. -
202 li. -
,
, .
, , .
monstr daemon,
.
, :
enum color {red. green, blue};
// monstr
class monstr{
// -- :
1nt health, ammo;
color skin;
char *name;
public:
// :
monstrdnt he = 100. i n t am = 10);
monstrCcolor sk);
monstrCchar * nam);
monstr(monstr &M);
// :
-monstrO {delete [ ] name;}
// :
monstr& operator ++(){
++health; return *th1s;
}
monstr operator ++(int){
monstr M(*this); health++: return M;
}
operator int(){
return health;
}
bool operator >(monstr &M){
1f( health > M.health) return true;
return false;
}
const monstr& operator = (monstr &M){
if (&M == this) return nhls;
If (name) delete [] name;
If (M.name){
name = new char [strlen(M.name) + 1];
strcpy(name. M.name);}
else name = 0 ;
health = M.health; ammo = M.ammo; skin ^ M.skin;
return *th1s;
} ^
// :
5. 203
// daemon
class daemon : public monstr{
int brain;
public:
// :
daemon(int br 10){brain = br;};
daemon(color sk) : monstr (sk) {brain = 10;}
daemon(char * nam) : monstr (nam) {brain = 10:}
daemon(daemon &M) : monstr (M) {brain = M.brain;}
// :
const daemon& operator = (daemon &M){
i f (&M -= this) return n h i s ;
brain = M.brain;
monstr::operator (M);
return *this;
204 ii. -
}
// , :
void thinkO:
// :
void drawCint . int . int scale, int position):
}:
// daemon
void daemon: :think(){ /* ... */ }
void daemon::draw(int x. int y. int scale, int position)
{ /* ... daemon */ }
daemon brain think,
, draw.
monstr, ( ) get^health,
get^ammo change_health daemon,
.
.
,
.
.
-
. ,
( , ).
daemon.
, ,
.
, ,
, .
.
,
(
).
,
daemon. -:
-
. ,
operator
-.
.
, :
,
.
5. 205
,
,
,
.
,
,
.
, ,
, :
, ,
.
, monstr,
, private. ,
daemon, ,
protected,
monstr, daemon ,
(. 201).
, ,
daemon draw,
monstr ( , ,
-). , ,
^.
,
(::).
.
, :
/ / :
monstr *:
/ / :
= new daemon:
,
, ,
, ,
p->draw(l. 1. 1. 1):
monstr, daemon,
. ,
^ (.
. ).
206 il. -
. daemon,
:
(daemon * p)->draw(l. 1. 1. 1);
,
,
.
, .
.
, .
C++ ,
, .
.
virtual,
:
virtual void draw(int . int . int scale, int position);
.
, ,
,
, .
,
.
.
,
.
static,
.
,
.
, :
virtual void f(int) = 0;
(
, ).
draw raonstr , ,
, ,
:
monstr * . * ;
= new monstr;. // monstr
= new daemon; // daemon
r->draw(l. 1. 1. 1); // monstr::draw
p->draw(l. 1. 1. 1); // daemon::draw
5. 207
( !),
, ^ (vtbl),
.
.
vtbl
.
vtb1,
vptr. (
).
vtbl vptr ,
. , ,
,
.
.
,
- ,
^ ,
. delete
, size_t.
,
.
, , .
,
, .
, ,
, .
, ,
(
),
.
, draw
.
(
208 il. -
, ),
,
. draw ,
, .
. ,
, .
,
,
.
, ,
. ,
.
,
.
:
,
;
,
;
, ,
, .
, ,
.
.
^
.
,
. ,
,
:
class monstr{
public: int get__hedlth():
}:
5. 209
class hero{
public: int get_health();
}: '
int main(){
ostrich A:
cout A.monstr::get_health():
cout A.hero: :get__health():
}
, gethealth
, .
A.get_health() ,
,
.
, ,
,
. ,
:
class monstr{
}:
class daemon: virtual public monstr{
}:
baby monstr.
, ,
.
,
.
,
, .
, ,
(
).
210 II. 1-
(struct) (union)
.
, ,
public.
, .
:
ic, ,
;
;
,
;
,
;
.
6
(. 85),
,
, .
, .
,
, .
. ,
- .
C++
(
. 295).
,
,
.
.
(. 115)
. ,
,
, .
.
, .
, .
:
class Nocle{
public:
212 II. -
int d; //
Node *next; //
Node *prev; //
Nodednt dat = 0){ //
d = dat; next = 0; prev = 0;
, ,
(public).
.
List:
class L1st{
class Node{
}: "*
Node *pbeg. *pend: //
public:
List(){pbeg = 0; pend =0;} //
4ist(): //
void add(int d): //
Node * find(int i ) : //
// d key:
Node * insert(int key, int d):
bool remove(int key): //
void printO: //
void print_back(): //
}:
. add
Node ,
:
void List::add(int d){
Node *pv = new Node(d): //
if (pbeg == 0)pbeg = pend = pv: //
else{
// :
pv->prev = pend:
pend->next = pv:
pend = pv:} //
}
, add_sort,
. 119.
f i nd
:
Node * L i s t : : f i n d ( i n t d ){
Node *pv = pbeg:
6. 213
while (pv){
if(pv->cl === cDbreak:
pv pv->next;
}
return pv;
}
insert key
. ,
0:
Node * L i s t : : i n s e r t ( i n t key. i n t d){
if(Node *pkey = find(key)){ / / key
/ / :
Node *pv = new Node(d);
/ / :
pv->next = pkey->next;
/ / :
pv->prev = :
/ / :
pkey->next = pv;
/ / :
i f ( != pend) (pv->next)->prev = pv;
/ / .
/ / :
else pend = pv;
return pv;
}
return 0;
}
remove
true - false,
:
bool List::remove(int key){
if(Node *pkey = find(key)){
i f (pkey == pbeg){ / /
pbeg = pbeg->next;
pbeg->prev *= 0;}
else i f (pkey == pend){ / /
pend = pend->prev;
pend->next - 0;}
else { II
(pkey->prev)->next pkey->next;
(pkey->next)->prev = pkey->prev;}
delete pkey;
return true;}
return false;
}
214 II. -
, :
void L i s t : : p r i n t ( ) {
Node *pv = pbeg:
cout endl "11st: ":
while (pv){
cout pv->d ' ' ;
pv = pv->next;}
cout endl:
}
void List::print_back(){
Node *pv = pend:
cout endl " l i s t back: ":
while (pv){
cout pv->d ' ':
pv = pv->prev:}
cout endl:
}
- :
List::4ist(){
i f (pbeg != 0){
Node *pv = pbeg:
while (pv){
pv = pv->next:
delete pbeg:
pbeg = pv:}
( , List.
. 116: 5 ,
, ,
:
i n t main(){
List L:
for ( i n t i = 2: i<6: i++) L.add(i):
L.printO:
L.print_back():
L.insert(2. 200):
i f (!L.remove(5))cout "not found":
L.printO:
L.print__back():
}
Li St .
,
.
6. 215
:
template <___> _:
.
, .
, .
class.
,
, :
template <class Data> class List{
class Node{
public:
Data d;
Node *next;
Node *prev;
NodeCData dat = 0){d = dat: next = 0: prev - 0:}
}:
}
Data ,
.
,
:
tempiate<class > class myarray { / * . . * / } :
//
template <class Data>
void List <Data>::print(){
Node *pv = pbeg;
cout endl "list: ";
while (pv){
cout pv->d ' ':
pv = pv->next;}
cout endl;
}
//
template <class Data>
void List <Data>::print_back(){
Node *pv = pend;
cout endl " list back: ":
while (pv){
cout pv->d ' ':
pv = pv->prev:}
cout endl:
}
//
template <class Data>
void List <Data>::add(Data d){
Node *pv = new Node(d):
if (pbeg == 0)pbeg = pend = pv;
else{
pv->prev = pend;
pend->next = pv;
pend = pv;}
}
//
template <class Data>
Node * List <Data>::find(Data d){
Node *pv = pbeg;
while (pv){
if(pv->d == d)break;
pv = pv->next;
}
return pv;
}
//
template <class Data>
Node * List <Data>::insert(Data key. Data d)(
if(Node *pkey = find(key)){
Node *pv = new Node(d);
pv->next = pkey->next;
218 II. -
pv->prev = ;
pkey->next =* pv:
i f ( != pend)(pv->next)->prev = pv:
else pend pv;
return pv:}
return 0:
}
// --
template <class Data>
bool List <Data>::remove(Data key){
if(Node *pkey = find(key)){
i f (pkey == pbeg){
pbeg = pbeg->next: pbeg->prev = 0:}
else i f (pkey == pend){
pend = pend->prev: pend->next = 0:}
else {
(pkey->prev)->next = pkey->next:
(pkey->next)->prev = pkey->prev:}
delete pkey: return true:}
return false:
}
List ,
,
(
, . 284)
, ,
.
, ,
, .
, .
,
. ,
:
template <class Type, int kol>
class Block{
public:
Block(){p * new Type [kol]:}
-BlockO{delete [ ] p:}
operator Type * ( ) :
protected:
Type * p:
}:
template <class Type, int kol>
Block <Type.. kol>:: operator Type *(){
return p:
}
.
6. 219
( ),
:
^ <>
_ [(_)]:
.
.
, :
List <int> L i s t j n t :
List <double> List_doub1e:
List <monstr> List^monstr;
Block <char. 128> buf:
Block <monstr, 100> stado:
^, :
tempiate<class - char> class String:
Stringo* p;
, ,
(
):
tempiate<class > class { / /
i n t :
}:
tempiate<class > class <*> { //
long :
}:
tempiate<template<class U> class V> class C{
V<int> y:
V<int*> z:
}:
C<A> c:
V<int> C<A> , ..
int, V<int*> , c.z.x
long.
, ,
.
,
, :
for (int i = 1: i<10: i++)List_double.add(i * 0.08):
List^double.printO:
//-- -- --
for (int i = 1: i<10: i++)List monstr.add(i):
220 II. '
List__monstr.print();
// -
strcpy(buf. " "):
cout buf endl;
typedef:
typedef List <double> Ldbl;
Ldbl L i s t j o u b l e :
, ,
; , .
, ,
.
- ,
, () .
,
. ,
print List
template <class Data> void List <Data>::print();
:
void List <char>::print(){
... / / print
}
List char,
.
,
. ,
Block, , 100 ^:
class Block<int. 100>{
public:
1(){ = new i n t [100]:}
-Block{delete [ ] p:}
operator i n t * ( ) :
protected:
i n t * p:
}:
He .
6. 221
,
, ,
. , ,
, ,
. ,
, .
.
C++
(.
, . 295).
7
,
,
. ,
.
. C++
.
C++ , ,
, ,
Ctrl+C. ,
.
,
, .
- .
.
. ,
, , ,
, , ,
.
, .
,
, ,
. , , ,
.
7. 223
, ,
, ,
.
, ,
, try.
, .
. ,
, .
throw , .
,
.
.
,
terminate, abort,
. .
(. , . 73) ,
.
,
. -
.
, .
. ,
,
.
.
try ,
.
:
try{
}
, try-,
.
224 II. -
() throw,
, :
throw [ ];
, throw, .
,
.
, try-, ,
.
, ,
.
,
throw .
catch,
.
try-.
.
. :
catch(TMn ){ ... /* */ }
catch(THn){ ... /* */ }
catch(...){ .. /* */ }
,
- ,
.
, . ,
.
, ,
. :
catchdnt 1){
... // int
}
catch(const char *){
... // const char*
}
catch(Overflow){
... // Overflow
}
catch(...){
... //
}
,
. ,
, , try-
.
7. 225
throw ,
C++ :
1) throw ,
, ;
2) ,
, ;
3) , ,
.
, ,
.
, , throw:
, catch (
, const . & const &. );
catch (
public);
,
catch.
,
,
. void
,
.
.
#include <fstream.h>
class Hello{
// ,
public:
Hello(){cout "Hello!" endl;}
~Hello(){cout "Bye!" endl;}
}:
void fl(){
ifstream ifs("\\INVALID\\FILE\\NAME"); //
if (!ifs){
cout " " endl;
throw " ";}
void f2(){
Hello //
flO; // . 1
}
int mainOi
226 II. -
try{
cout " try-" endl:
f2():
cout " try-" ;
}
catchdnt 1){
cout " int. - " 1 endl:
return -1:
}
catchCconst char * p){
cout " const char*, - "
p endl:
return -1;
}
catch(...){
cout " " endl:
return -1:
}
return 0: / /
}
:
try-
Hello!
Bye!
const char *. -
,
, f 1 ,
main. try-
. (
10 . 265).
,
.
^ ,
.
. , .
,
, .
, ,
.
,
.
, .
public,
(
, , public).
7. 227
,
. ,
, ,
.
. 7.1.
unexpected()
terminate()
. 7 . 1 .
throw, , :
void f l O throw (int. const char*){ /* */ }
void f2() throw (Oops*){ /* */ }
fl int const char*.
f2
Oops .
228 II. -
throw ,
. ,
:
void fO throw (){
// ,
}
.
,
, .
, .
unexpected,
terminate. set^unexpected
, terminate
.
terminate abort,
. set_terminate
, abort
. set_unexpected set^terminate
<exception>.
C++ .
,
. Vector,
:
class Vector{
public-
class Size{}: //
enum {max = 32000}: //
Vector(int n) //
{ i f (n<0 II n>max) throw SizeO: ... }
Vector
Size:
try{
Vector * = new Vector(i):
}
7. 229
catchCVector::S1ze){
... / /
}
. ,
, ,
. ,
,
.
,
, , ,
. ,
,
.
new
, -
.
.
,
, , .
,
. ,
,
. ,
:
class Matherr{}:
class Overflow: public Matherr{}: / /
class Underflow: public Matherr{}: / /
class ZeroDivide: public Matherr{}: / /
/
:
class IOerr{}:
class Readerr: public IOerr{}: / /
class Writerr: public IOerr{}: / /
class Seekerr: public IOerr{}: / /
, ,
.
230 li. -
,
C++ (. 16 ,
. 378). exception,
<stdexcept>. , new
1_.
,
.
8
.
, . 38 3.
C++
const_cast. clynamic__cast, re1nterprGt_cast stat1c_cast,
, ,
(. 56). .
:
()
()
, :
1nt = 2;
float b = 6.8;
p r i n t f C ' ^ l f ^ d " . double (a). (1nt) b);
a double, a b 1nt
,
, .
, , ,
void,
-
:
float *q = ( f l o a t *) mallocdOO * s i z e o f ( f l o a t ) ) ;
,
.
C++ , -
232 II. -
. C++
, .
.
const_cast
const. ,
, const. :
const_cast <> ()
11 , ,
const. .
.
, ,
,
const, . C++
. constcast
, . ,
, ,
.
:
void printdnt *){ // *
cout *;
}
const int *:
/* pr1nt(p); , */
const_cast , ,
, , .
,
const,
.
dynamic_cast
,
8. 233
,
.
:
dynamic_cast < *> ()
,
.
,
y^
bad^cast.
, .
(downcast),
.
(upcast),
, ,
(crosscast).
clynam1c_cast
:
class { / * ... * / };
class : public { /* ... */ }:
* = new :
* b = clynam1c__cast<B*>(c): / / * b = ;
dynam1c_cast
.
, .
,
.
RTTI (run-time type information)
. ,
dynamic_cast ,
(. . 205).
, .
234 . -
RTTI
<typeinfo>. , ,
.
clynam1c_cast
,
, .
, ,
: ,
, . ,
dynam1c_cast .
dynamic^cast
.
, f2.
demo ,
,
dynamic^cast :
#1nclude <iostream.h>
#include <type1nfo.h>
class B{
public: virtual void f l ( ) { } :
}:
class C: public B{
public: void f2(){cout "f2":}:
}:
void demo(6* p){
C* = dynamic_cast<C*>(p):
if (c) c->f2():
else cout " ":
}
int main(){
* b = new :
demo(b): // " "
* - new :
demo(c): // "f2" ()
return 0:
}
dynamic_cast
, :
* = (*) :
, PI
, .
, -
8. 235
. dynamic^cast
, .
,
:
#1nclude <1estream.h>
#include <type1nfo.h>
class A{ // A
public: virtual ~A(){}:} // / \
c]ass B: public virtual A{}: //
class C: public virtual A{}: // \ /
class D: public B. public C{}; // D
void demo(A *a){
D* d = dynamic_cast<D*>(a):
i f (d) { ... }
int main(){
D *d = new D; demo(d);
return 0;
}
- ,
. ,
dynamic_cast .
,
bad^cast:
#include <iostream.h>
#include <typeinfo.h>
class B{
public: virtual void f l ( ) { } ;
}:
class C: public B{
public: void f2(){ ... };
}:
void demo(B& p){
try{
C& = dynamic_cast<C&>(p):
c.f2();
catch(bad_cast){
}
}
int main(){
B* b = new B; demo(*b); //
236 II. -
* = new ; demo(*c); / /
return 0;
dynamic_cast
, :
#include <iostream.h>
#1nclude <typeinfo.h>
class B{
public: virtual void f l ( ) { } :
}:
class C: public B{
public: void f2(){ ... }:
}:
class D: public B{ ... }:
void demo(D* p){
C* = dynamic_cast<C*>(p):
if(c)c->f2():
else cout " ":
}
i n t main(){
B* b = new C: demo((D*)b):
return 0:
}
D . demo
D,
, D demo
.
, :
#include <iostream.h>
finclude <typeinfo.h>
class B{
public: virtual void f l ( ) { . . . } : / /
}: // \ /
class C{ // D
public: virtual void f2(){ ... }:
}:
class D: public B. public C{}:
8. 237
static_cast
stat1c__cast
:
;
;
;
, ,
.
:
static_cast <> ()
, ,
, .
, . :
float f = 100:
int 1 = static_cast <1nt> (f):
//
, ,
.
.
static_cast
:
class {};
class : public {}:
;
* = stat1c_cast<B*>(c): / / ->
:
& = stat1c_cast<C&>(b): / / ->
238 II. -
,
.
.
dynamic_cast.
,
, static_cast. , dynam1c_cast
,
.
reinterpret_cast
reinterpret_cast
, , ,
void* .
, .
:
reinterpret_cast <> ()
, ,
, .
:
char * = re1nterpret_cast <char*>(malloc(100)):
long 1 = reinterpret^cast <long>(p);
static^cast re1nterpret_cast
stat1c_cast,
re1nterpret_cast.
.
(RTTI)
, ,
. RTTI
typeid type_1nfoi.
typeid:
typeid ()
typeid ()
type^info, .
, bad_typeid.
^ Typeinfo typeinfo.
8. 239
,
( type_1nfo
,
).
,
, type_1nfo ,
, .
typeid ,
.
type_1nfo <type1nfo> :
class type_info{
public-
virtual 4ype_1nfo():
bool operator==(const type_1nfo& rhs) const:
bool operator!=(const type_1nfo& rhs) const:
bool beforeCconst typeJnfo& rhs) const:
const char* nameO const:
private:
typeJnfo(const type_info& rhs):
typeJnfo& operator=(const type_info& rhs):
}:
name , ,
typejnfo.
type__info .
private,
.
== ! = ,
before .
:
typeid(Tl).before(typeicl(T2))
1 2,
.
, ,
,
.
:
#include <typeinfo.h>
class {
public: virtual - ( ) { } :
}:
class : public {
public: virtual void some_method(){ ... }:
240 II. -
void demoCB* ){
i f (type1d(*p) == typeid(C))
dynamic_cast<C*>(p)->somG_method():
}
i n t ma1n(){
C* = new C: denio(c):
return 0;
}
:
void pr1nt_type(some_obj *){
cout type1d(*p).name();
}
typei d
, .
9
-
C++, ,
:
, , ,
.
, ,
C++ .
, , , ,
- . -
, , ,
.
,
, . . ,
, ,
.
,
, ,
- .
,
. ,
,
. [6], . [7] . [17].
,
.
, ,
,
. , -
242 . -
. , ,
. (private).
,
.
get/set
, , .
, ,
, . ,
,
, ( ,
, ).
, ,
^. ,
,
.
,
. - ,
, ;
, .
, ,
, .
,
.
(inline).
, ,
. ,
.
, inline
, ,
. ,
,
. ,
, ,
.
,
,
, (,
).
, .
9. 243
(, + -,
).
,
-
, .
- , , ,
, , +, += ++ (
). ,
.
.
, ,
.
, ,
, - -
-^. , ,
.
,
.
, -,
,
.
, , ,
.
. (
), ,
,
, , delete
.
, , -
,
. ,
, -,
.
,
. ,
:
class {
const X & operator=(const X & ) ;
};
const X & X::operator=(const X & ){
if(th1s != &){
244 II. -
/ /
}
return 4h1s:
}
:
class {
string s;
public:
X(const char * s t r ) :
}:
// :
X::X(const char * str){ s = str: }
// :
X::X(const char * str): s(str){}
, ,
(,
). ,
, C++ ^1
,
. ,
string ,
,
. , ,
.
,
, .
.
,
,
.
( ).
,
:
class {
public:
X(void):
/ / :
XCconst X & ) :
}:
class Y: public {
string s: //
public:
9. 245
// :
YCconst Y & ): (). s(r.s){}
}:
,
.
(. . 204).
, ,
, . .
! .
Y X , Y
X, , .
X , Y^ ,
X, Y, (,
).
,
,
.
, ,
, .
, .
,
.
,
,
. .
, C++
, , :
#1nclude <iostream.h>
class {
public:
virtual void fun(int a = 0){cout a:}
}:
class Y: public X{
public:
virtual void fun(int a = 1) {cout a : }
}:
i n t main(){
X *px = new X:
px->fun(): // 0
^ , , .
246 il. -
X * = new Y:
py->fun(); / /
}
,
.
, ,
,
, ()
.
.
,
,
,
.
, :
class { . . . };
class Y{
X :
}:
Y X <iY
X.
, , Y
X. , , ,
: ,
.
,
X Y , X,
Y X.
.
, ,
,
( private).
,
.
,
. ,
,
, .
,
,
. ,
.
9. 247
, , ,
(,
),
. -
, , ,
.
. [9] . [13].
II
1
, . ,
- .
,
( ).
.
2
, ,
, , ,
.
, -
. ,
.
.
3
: ,
, . ,
, .
, .
,
.
4
,
.
, .
, .
, .
5
.
, .
249
, .
, .
-,
.
( ),
, ,
.
, .
, .
7
,
.
, , ,
, ,
() .
, .
, .
8
().
,
,
,
1 .
, .
, .
9
. ,
,
,
,
.
, .
, .
10
,
.
, .
250 II. -
-,
.
, .
, .
11
,
.
,
,
,
, .
, .
, .
12
,
,
.
, .
, .
13
,
,
. :
;
;
;
.
, .
, .
14
.
, - (,
), ,
, .
, .
, .
15
.
, - (,
II 251
, ),
, .
, .
, .
16
.
, - (
, , ),
, .
, .
, .
17
,
. ! :
, , , (+, -, *, / ) (
, );
(+=, -=, *=, /==);
/;
,
;
;
, (, ,
, , , ,
);
/ (. . 284).
, .
, .
18
,
, , .
, .
, .
19
, . ,
.
, .
, . .
, , .
.
.
252 II. -
20
.
, .
, , .
, ,
, .
, .
, .
1
CFile, ,
Open, Close, Seek, Read, Write, GetPosition GetLength. Ha
CMyDataFile ,
MyData, ,
.
, .
, .
2
CPoint . CcoloredPoint
CLine. CLine CColoredLine CPolyLine
.
, .
,
.
3
CVehicle. CPlane,
CShip.
, (, , ).
,
. .
,
. ,
.
4
1. .
:
( );
;
.
253
:
;
;
, .
2.
, ( ),
.
:
, ,
.
:
;
;
;
, ;
,
;
, ( ).
3. ,
. ,
(. ), .
, .
:
( ),
( );
;
.
:
( );
;
;
, ;
,
;
, ( )
;
, == .
4. , .
:
;
254 II. -
;
;
() (),
.
:.
, (
);
,
;
, .
,
private, protected.
.
,
,
, / .
5. ,
.
.
,
.
1 - 4 RS 8
2 5 RST 10
3 2- 6 D 12 i
4 8 8
5 - 8 V 9
6 4 RS 10
7 - 5 JK
8 02 5 D 8
9 4 10
10 3 JK 8
- 3 RS 12
12 - 4 RST 4
13 02 5 D 10
14 02- 6 10
15 - 8 V 10 1
II 255
16 , 8 JK 6
17 - 8 RS 10
18 8 10
19 02 6 JK 8
20 2- 5 V 10
5
.
:
char
;
int .
:
;
, - (
);
, ;
;
;
( );
.
__.
,
-.
, _.
:
;
, - (
);
, ;
;
;
;
;
.
256 II. -
:
(=);
(+) ;
(-) ( ) ,
, ;
(<) . ,
- ( -1,
)
, .
(<) . ,
- (z w-1,
)
, .
( )
.
, :
(4-6);
,
;
.
(
).
.
6
.
:
char
;
int .
:
;
, - (
);
, ;
;
;
II 257
( );
.
__.
'' '.
,
, _ . -
.
.
:
;
, - (
);
;
;
( ).
(
;
):
(=);
(+) ;
(==) .
( )
.
, :
(4-6);
,
;
.
(
).
.
?
,
:
char
;
258 . -
int .
:
;
, - (
);
, ;
;
;
( );
.
_,.
- +, . - +
, + ,
.
, , _
.
.
:
;
, - (
);
;
;
, , int;
:
(=);
(-) ;
(>) ( );
(<) ( ).
( )
.
, :
(4-6);
,
;
Q
.
259
(
).
.
8
.
:
char
;
int .
:
;
, - (
);
, ;
;
;
( );
.
__.
, i.
, .
- +,
. - +
, + ,
.
, , _
. : 33112, -7il00, +5i-21.
:
;
, - (
);
;
.
:
(=);
(==) ;
(*) .
( )
.
260 11. -
, :
(4-6);
,
;
.
(
).
.
1
. 1
.
2
. 7
(. ).
3
. 8
(. ).
4
.
1 (.
).
5
.
3 (.
).
6
.
2 (.
).
?
.
4 (.
).
11 261
.
, .
9
. ,
..
,
.
10
.
. ,
. ,
.
11
(. 5
). , ,
double, .
12
(. 8
). ,
.
13
, 12 .
,
.
14
,
, ,
.
,
.
15
.
5 (.
).
16
.
6 (.
).
262 II. -
17
.
11
(. ).
18
.
2 .
19
.
18 (.
).
20
,
. 20
(. ).
III
, , ,
.
,
, , Microsoft Visual C++ Borland C++
Windows.
,
C++.
C++ .
, , ,
, C++.
(. 88).
, 5,
6. , ,
, , ,
.
lia
.
(,
), .
.
, , .
, .
.
.
264 Hi.
- .
,
,
. .
, ,
, (STL Standard Template
Library).
; #include
. , <1ostream>, <11st>.
.h std,
. h .
4.
C++ ,
std, , , <cstd1o>, <cstr1ng>,
<ct1me>. <> <. h>,
(. . 100)^.
^ : string
C++, cstring
std, string.h , cstring.h
C++
.
10
ioiaccbi
,
. C++, /
, ,
,
.
, ,
.
, (
, , ).
, ,
.
, .
(
), ( ) (
, ).
, ,
, .
,
(, ),
.
C++ ,
1os streambuf. 1os
, streambuf
.
1 stream ostream .
1 ostream,
.
. .
266 III.
ios ;
i stream ;
ostream ;
lost ream ;
istringstream ;
ostringrstream ;
stringstream ;
if stream ;
of stream ;
fstream .
:
<ios> /;
<iosfwd> /;
<istream> ;
<ostream> ;
<iostream> /;
<fstream> / ;
<sstream> / ;
<streambuf> /;
<iomanip> ( , . . 271).
<fstream> <sstream>
"^iostream>, .
/,
, ,
, , .
(
, , . 284).
, wchart.
wistream. wostream, wofstream . .^.
,
.
^ ,
. char wchar^t
typedef.
10. 267
<iostream> , /
, :
1 istream (
)
cout ostream (
)
ostream (
),
clog ostream (
),
<iostream>,
/.
.
i stream ostream
. :
#1nclude <1ostream.h>
int ma1n(){
int i ;
c1n 1;
cout " " 1;
return 0:
}
i stream ostream
. ,
.
.
,
, :
// - , :
cout 1 + j ;
// - , :
cout (1 < j);
cout (1 j ) ; / /
(,
). ,
.
268 III.
,
,
,
.
,
.
,
, ,
, :
#1nclude <1ostream.h>
1nt ma1n(){
1nt 1 = OxD:
double d:
// double:
Gin d:
// 1nt double :
cout 1 ' ' d:
return 0:
}
,
.
( ) .
. ,
1.53-2,
, double.
, :
13 0.0153
,
,
.
, Enter.
(
-):
char strl[100]. str2[100]:
c1n s t r l str2;
" ", strl
str2 "" "" ,
. (
'\') get getHne (. . 273).
.
,
. , :
cout 1 ' ' d " " j:
10. 269
, , ,
/,
. .
-
, .
, x_flags
long ios. . 10.1.
1 0 . 1 .
skipws 0x0001 +
left 0x0002
1 right 0x0004 +
internal 0x0008 ,
.
x_f i 11 (. ),
dec 0x0010 +
oct 0x0020
hex 0x0040
showbase 0x0080
( 0
)
showpoint 0x0100
uppercase 0x0200
showpos 0x0400
scientific 0x0800
fixed 0x1000
(
x_precision, . )
unitbuf 0x2000
stdio 0x4000 stdout stderr
270 ill.
:
#1nclucle <iostream.h>
i n t ma1n(){
long a = 1000. b = 077;
cout.w1dth(7);
cout.setfdos: :hex | ios: ishowbase | ios::uppercase):
cout a;
cout.width(7);
cout b endl:
double d = 0.12. = 1.3e-4;
cout.setfdos: : l e f t ) :
cout d endl;
cout c:
return 0:
}
b ,
7 ( width
, ).
e n d :
08 0X3F
0.12
0.00013
,
.
, , .
,
.
, .
dec ;
oct
;
hex
;
WS ;
endl ;
ends ;
flush .
.
272 III.
:
cout 13 hex ' 13 oct ' ' 13 endl:
, :
13 d 15
, . -'
<iomanip>.
setbaseCint n) ( = 8, 16, 10 0).
(,
, 8- 16- );
resetiosflags(long) ,
;
setiosfl agsdong) ,
1;
setf i 11 (i nt) - ,
;
setprecision(int)
( fixed)
(
scientific);
setw(int) .
:
#1nc1ude <1ostredin.h>
#include <1oman1p.h>
Int ma1n(){
double d[] = {1.234. -12.34567. 123.456789. -1.234. 0.00001};
cout setfil]('.') setprec1s1on(4)
setiosfl agsd OS: ishowpoint | 1os::f1xed);
for d n t 1 = 0; 1 < 5: 1++)
cout setw(12) d[i] endl;
return 0;
}
:
...1.2340
.-12.3457
.123.4568
..-1.2340
...0.0000
10. 273
(
).
, i stream.
gcountO ,
;
get
EOF;
get(c) ,
, ;
get(buf.num.l1m='\n' num-1 (
lim)
buf. 1 im
('\0'). lim
. ;
getl1ne(buf. num. l i m = ' \ n ' ) get, buf
lim;
1gnore(num = 1. lim = EOF) ,
num
, lim.
;
EOF, ;
putback(c) ,
;
reacKbuf. num) num (
, num)
buf ;
readsomeCbuf. num) num (
, num)
buf
;
seekg(pos)
pos;
^ , , ,
.
274 III.
}
return 0;
}
2. (
) ,
:
#1nclude <fstream.h>
#include <string.h>
int main(){
//
ofstream out("test");
10. 275
1f(!out){
cout "Cannot open file 'test* for writing" endl:
return 1;
}
double num = 100.45;
char str[ ] = "This is a test.":
out.wr1te(reinterpret__cast<char *>(&num). sizeof(double)):
out.writeCstr. strlen(str)):
out.closeO:
//
ifstream in("test", ios::in|1os::nocreate):
1f(!in){
cout "Cannot open file 'test' for reading" endl:
return 1:
}
double check_num:
char check__str[60]:
1n.read(re1nterpret__cast<char *>(&check__num). slzeof(double)):
in.read(check_str. 60):
int Istr = in.gcountO: //
check_str[lstr] = 0; // -
cout check^num ' ' check_str endl:
in.closeO:
return 0:
}
reinterpret_cast<char *> writeO readO
, .
3. test,
.
#include <fstream.h>
#include <string.h>
int ma1n(){
//
ofstream out("test"):
ifdout) {
cout "Cannot open file 'test' for writing" endl:
return 1:
}
char *str[ ] = {"This is the first line.".
"This is the second line.".
"This is the third line."}:
for (int 1 = 0: 1<3: ++i){
out.wr1te(str[i]. strlen(str[i])):
out.put('\n'):
276 III.
out.closeO;
//
ifstream inC'test". ios: :1n|1os: :nocreate):
if(!1n){
cout "Cannot open file 'test' for reading" end!:
return 1;
}
char check_str[3][60]:
for (1 = 0: 1<3; ++1){
1n.get(check__str[1]. 60):
in.getO;}
//
for (1 = 0; 1<3; ++i) cout check_str[1] end!:
in.closeO;
return 0;
}
get(check_str[i], 60) - '\n'
, get
. get
getline, - .
4. putbackO ,
.
. (
) , .
.
#1nclude <fstream.h>
#1nc1ude <ctype.h>
#1nclude <stdl1b.h>
int main(){
char ch:
//
ofstream out("test"):
if(lout) {
cout "Cannot open file 'test' for writing" end!:
return 1;
}
char str[80]. *p;
out 123 "this is a test" 23:
out " there!" 99 "bye" end! :
out.closeO:
//
ifstream in("test". ios::in|ios::nocreate):
if(!in){
cout "Cannot open file 'test' for reading" end! :
return 1:
10. 277
}
do{
= str;
ch = in.peekO: //
1f(1sd1g1t(ch)){
wh1le(1sd1g1t(*p = In.getO)) ++: //
1n.putback(*p); //
'^ = '\0': //
cout "Number: " atoKstr):
}
else 1f(1salpha(ch)){ / /
whlledsalphaC'^p = i n . g e t O ) ) p++:
1n.putback(*p): //
* = ': //
cout "String: " str:
}
else i n . g e t O : / /
cout endl:
} whiledin.eofO):
in.closeO:
return 0:
}
:
Number: 123
String: this
String: is
String: a
String: test
Number: 23
String: Hello
String: there
Number: 99
String: bye
. ,
, ,
,
:
cout " ":
cin :
278 ill.
basic_ios tieO,
istream ostream cin.tie(&cout).
( cout.flushO) ,
.
,
<cstdio> <stdio.h>, . -
,
ios^base sync_with_stdio(),
. sync_with_stdio(false) (
).
ios state,
:
im 1 state {
goodbit = 0x00. //
eofbit = 0x01. //
fallbit = 0x02. //
badbit = 0x04. // ,
//
hardfall = 0x08 //
}:
:
1nt rdstateO ;
1nt eofO ,
eofbit;
1nt f a i l ,
failbit, badbit hardfail;
int badO - ,
badbit hardfail;
1nt good - ,
;
void clear(int = 0) ,
0;
operator void*() - ,
;
operator !() - ,
.
.
10. 279
// , flag;
if(stream_obj.rdstate() & ios::flag)
// flag;
strea[n_obj.clear(rdstate() & -1os::flag)
// flag;
stream_obj.clear(rdstate() | 1os::flag)
// flag :
stream_obj.clear( 1os::flag)
// :
stream^obj.clearO
void*() , 0.
:
while (stream_obj){
// , /
}
hdstateO.
,
.
CheckStatusO.
#include <iostream.h>
#1hclude <fstream.h>
void CheckStatusdfstream &in);
int maindnt argc. char *argv[ ]){
1f(argc != 2){
cout "Usage: <program__name> <f1le__name>" endl;
return 1:
}
ifstream 1n(argv[l]. ios::1n|1os::nocreate);
1f(!1n){
cout "Cannot open file"- argv[l] endl:
return 1:
}
char c:
wh1le(1n.get(c)){
cout c: CheckStatusdn):
}
CheckStatusdn): //
in.closed:
return 0:
}
void CheckStatus(ifstream &in){
int i:
i = in.rdstateO:
if(i & ios::eofbit)
cout "EOF is occured" endl:
else if(i & ios::failbit)
280 III.
, , .
,
, ,
.
,
^ .
:
if stream ;
of St ream ;
fstream .
i stream, ostream iostream
, ,
, , , . .
:
;
;
(/);
;
.
,
.
,
:
IfstreamO:
ofstreamO:
fstreamO:
,
:
ifstream(const char *. int mode = 1os::1n):
ofstreamCconst char *name. int mode = ios::out | ios::trunc):
fstream(const char *name. int mode = ios::in I ios::out):
10. 281
.
,
, , ios:
enum open_mode{
in = 0x01 //
out = 0x02 //
ate = 0x04 //
= 0x08 //
trunc = 0x10 // ,
nocreate = 0x20 // ,
noreplace = 0x40 // ,
binary = 0x80 //
}:
10.2 ios
, <stdio.h>.
10.2.
ios
binary in out trunc app stdio
4- "w"
+ + "a"
4- + "w"
+
+ + "r+"
+ + + "w+" 1
+. + "wb"
+ + + '"ab"
+ + + "wb"
+ + "rb"
+ + + "r+b"
+ + + + "w+b"
,
open, ,
, :
ifstream inpf ( " i n p u t . t x t " . . ios::1n|1os:inocreate); //
1f(!1npf){
cout " ": return 1:
282 III.
}
ofStream f:
f.open("output.txt"); // open
if (!f){
cout " ";
return 1;
}
,
, .
( ):
#include <fstream.h>
i n t main(){
char text[81]. buf[81]:
cout " :";
c1n text;
ifstream f ( t e x t . 1os::1n|ios::nocreate):
i f (!f){
cout " "; return 1;
}
while (!f.eof()){
f.getlineCbuf. 81);
cout buf end!;
}
return 0;
}
closeO,
, ,
.
, , .
i;
istringstream ;
ostringstream ;
stringstream .
<sstream>2
1 stream, ostream iostream , -
^ .
^ <strstrea.h>.
10. 283
, ,
, , . .
, ,
C++ ( string).
. 286.
:
explicit^ istringstreamdnt mode = 1os::1n):
explicit istringstream(const strings name, int mode = i o s : : i n ) :
explicit ostringstream(int mode == i o s : : o u t ) ;
explicit ostringstreamCconst string& name, i n t mode = i o s : : o u t ) ;
explicit stringstream(int mode = i o s : : i n | i o s : : o u t ) :
explicit stringstream(const strings name, int mode = i o s : : i n | i o s : : o u t ) ;
sscanf sprintf
11,
,
.
.
str,
:
string s t r const:
void strCconst string & s):
,
.
,
:
#include <sstream>
#include <string>
#include <iostream>
#include <ctime>
using namespace std:
string message( i n t i ) {
ostringstream os:
time__t t :
time(&t):
OS " time: " ctime(&t) " number: " i end!:
return os.strO:
}
int main(){
cout message(22):
return 0:
}
^ CM. 197.
284 Mi.
.
.
, ,
. ,
-, ,
. ,
, .
, , MyClass:
class MyClass{
1 nt X:
float ;
,
MyClass :
/ / :
friend ostream& operator (ostream& out. MyClass& C){
return out "x = " C.x " = " C.y:
}
// :
friend istream& operator (istream& in. MyClass& C){
cout " x: ":in C.x;
cout " : ":in C.y;
return i n ;
}
MyClass
:
#include <iostream.h>
class MyClass{
int x;
f l o a t y;
public:
MyClass(int nx = 1. float ny = 0.01){x = nx; = ny;}
friend ostream& operator (ostream& out. MyClass& C){
return out "x = " C.x " = " C.y;
}
friend istream& operator (istream& in. MyClass& C){
cout " x: "; in C.x;
cout " : "; in ;
return in;
10. 285
}
}:
1nt ma1n(){
MyClass :
cout endl:
MyClass CKIOO. 100):
cout CI endl:
MyClass C2:
c1n C2: cout C2 endl:
return 0:
}
MyClass
, :
X = 1 = 0.01
1 MyClass 100, 100:
X = 100 = 100
2 ,
.
11
+-+- ,
.
C++ .
, -.
,
<str1ng.h> (<cstr1ng>).
, . 91.
,
, .
string ,
.
; ,
.
<str1ng>i.
:
#1nclucie <cstr1ng>
#include <str1ng>
#1nclude <1ostreaiTi>
using namespace std;
1nt main (){
char c l [ 8 0 ] . c2[80]. ];//
string s i . s2. s3;
//
strcpyCcl. "old string one"):
strcpy(c2. cl):
^ string
cstri ng. h bstri ng. h,
strijng.h.
11. 287
string .
:
stringO;
string(const char * ) ;
string(const char * . int n);
string(string &);
string.
string , string
, .
,
, .
string :
strings operator=(const string& s t r ) ;
string& operator=(const char* s);
string& operator=(char c);
, string,
, :
string s l ;
string s2("Bacfl");
string s3(s2);
s l = 'X';
s l = "";
s2 = s3;
288 Hi.
string :
>
+ >=
[ ]
!=
<
<= +=
.
, .
, string
,
, &s[0] s.
,
at:
string s("Bac");
cout s.at(l); //
, out_of_range.
,
(, , ,
) string (),
.
string
: , ,
, .
assign:
assign(const string& str);
assign(const string& str. size_type pos. size_type n):
assign(const char* s. size_type n);
str ,
:
string slC'Bacfl"). s2;
s2.assign(sl); // s2 = si:
11. 289
str,
pos^. pos , out_of_range.
, , pos + ,
str, str.
s
.
append:
appendCconst str1ng& s t r ) ;
append(const string& s t r . s1ze_type pos. size_typG n);
append(const char* s. size_type n):
str ,
(+).
str,
pos. pos , out_of_range.
, , pos + ,
str, str.
,
length_error.
s .
insert:
1nsert(s1ze__type posl. const str1ng& s t r ) :
insert(s1ze_type posl. const str1ng& s t r . s1ze_type pos2. s1ze_type n);
1nsert(s1ze__type pos. const char* s. s1ze_type n):
str ,
posl . ,
, posl ,
str ,
. posl ,
out_of_range.
, length__error.
str,
posl .
, posl ,
str, pos2,
. str,
str. posl pos2
, out_of_range.
^ size_type ,
.
290 ill.
,
length_error.
s
, pos .
erase:
erase(s1ze_type pos = 0. s1ze_type n = npos):
, pos. pos
, . ,
.
npos string
s1ze_type ( ).
clear:
void clearO;
replace:
replace(size_type posl. size_type nl. const str1ng& str):
replace(s1ze_type posl. s1ze_type nl. const str1ng& str. s1ze_type pos2.
s1ze_type n2):
posl ,
, nl , pos2 str,
, 2
str. posl pos2 ,
out_of_range.
, length_error.
nl
2 s:
. replace(s1ze_type posl. size_type nl. const char* s. s1ze_type n2):
swap:
swap(strings s);
substr:
string substr(size_type pos = 0, size_type n = npos) const:
,
pos. pos , out_of_range.
, .
string .
c_str:
const char* c_str() const:
-
. . ,
, -.
11. 291
data, ,
-:
const char* data const:
copy s ,
pos. - .
:
size__type (char* s. s1ze__type . size__type pos = 0) const;
:
#include <string>
#include <1ostreani>
using namespace std;
int main (){
string sl(" "), s2("e"). s3("KopoBa");
cout "sl= " si endl;
cout "s2= " s2 endl;
cout "s3= " s3 endl;
// insert:
cout " insert:" endl:
cout "s3= " s3.insert(4. s2) endl:
cout "s3= " s3.insert(7. "") endl:
// erase:
sl.erase(0.3):
cout " erase:" endl:
cout "sl= " sl.erase(12.2) endl:
// replace:
cout " replace:" endl:
cout "sl= " sl.replace(0.3. s3. 4.2) endl:
}
:
sl=
s2=
s3-
insert:
s3=
s3=
erase:
sl=
replace:
sl=
string .
:
size__type findCconst string& str. size_type pos = 0) const:
292 ill.
str ,
pos, npos, .
s1ze_type find(char . s1ze_type pos = 0) const:
,
pos, npos, .
s1ze_type rf1nd(const str1ng& str. size_type pos = npos) const:
str , pos,
npos, .
s1ze_type rf1nd(char . s1ze_type pos = npos) const:
, pos,
npos, .
s1ze_type f1nd_first_of(const str1ng& str. s1ze_type pos = 0) const:
str ,
pos, npos,
.
s1ze_type f1nd_f1rst_of(char . s1ze_type pos = 0) const:
,
pos, npos, .
s1ze_type f1ndJast_of(const string& str. s1ze_type pos = npos) const:
str
, pos, npos,
.
s1ze_type f1nd_last__of(char . s1ze_type pos = npos) const:
,
pos, npos, .
s1ze_type f1nd_f1rst_not_of(const str1ng& str. s1ze_type pos = 0) const:
, pos,
str .
s1ze_type f1nd_f1rst_not_of(char . s1ze_type pos = 0) const:
, pos,
.
s1ze_type f1nd_last_not_of(const str1ng& str. s1ze_type pos = npos) const:
pos,
str .
size_type f1nd_last_not_of(char . s1ze_type pos = npos) const:
pos,
.
,
.
11. 293
:
#1ncTucle <str1ng>
#inclucle <iostrea(n>
using namespace std;
1nt main (){
string sl("eca "), s2("e");
cout "sl= " s i encll;
cout "s2= " s2 endl;
int i = s l . f i n d ( s 2 ) ;
int j = sl.rfind(s2):
cout " s2 s i " i end1:
cout " s2 s i " j endl:
cout " '' s i " s l . f i n d ( ' o ' ) endl:
cout " '' s i " s l . r f i n d ( ' ' ) endl:
cout " s i " sl.find_first_of("ae") endl:
cout " s i " sl.findJast_of("a6e") endl:
}
:
sl=
s2=
s2 si
.s2 si
'' si 8
'' si 10
si 1
si 14
, , compare:
i n t compare(const string& s t r ) const:
int compare(size__type posl. size_type n l , const string& s t r ) const:
i n t compare(size_type posl. size_type n l ,
const string& s t r . size_type pos2. size__type n2) const:
,
, str,
, ,
. strstr
.
'^ ^yHxxiJ5M ^jjjjaujjjgeT ,
str nl , posl.
nl ,
posl, str 2 , pos2.
294 ill.
string
.
:
#inclucle <string>
#inclucle <iostream>
using namespace std:
int main (){
string sl(" "), s2("e"). s3(""):
cout "sl= " si endl;
cout "s2= " s2 endl;
cout "s3= " s3 endl;
i f (s2.compare(s3) > 0) cout "s2 > s3 " endl:
i f (sl.compare(7. 4. s3) < 0)
cout "sl[7-10] < s3 " endl:
i f (sl.compare(7. 4. s3. 0.4 ) == 0)
cout "sl[7-10] == s3[0-3] " endl:
}
:
sl=
s2=
s3=
s2 > s3
sl[7-10] < s3
sl[7-10] == s3[0-3]
string -, ^
, :
size_type sizeO const: //
size_type lengthO const: //
size_type max_size() const: //
size_type capacityO const: // ,
bool emptyO const: / / ,
12
, ,
.
, .
, ,
,
.
, C++,
, ,
,
(STL Standard Template Library).
, ,
.
, :
.
. ,
.
STL , ,
, ,
, . :
.
.
(vector), (deque) (list),
, , (stack),
(queue) (priority^queue).
.
, .
296 111.
,
,
.
.
.
: (),
(multlmap), (set), (multiset)
(bitset).
.
STL , ,
, ,
(. , . 85,
, . 211). ,
(. 99), (. 83) (. 189).
.
, .
,
.
.71
:
value_type
s1ze__type , . .
Iterator
constjterator
reverse_1terator
const_reverse_1terator
reference
const_reference
key_type ( )
key_compare ( )
.
. ,
.
, (
, . 328).
12. 297
,
, .
,
.
iterator begInO,
const_iterator beginO const
iterator endO, ,
const_iterator endO const
reverse__iterator rbeginO,
const_reverseJterator rbeginO const
reversejterator rendO. ,
const_reverse_iterator rendO const ,
,
.
,
:
sizeO
max_size() ( )
empty0 , ,
.
STL 13 :
algorithm deque functional iterator list map
memory numeric queue set stack utility vector
(vector), (deque) (list)
, .
:
push_front - + +
pop^front - + +
push_back + + +
298 ill.
+ ,
, . (+) ,
, .
, +, ,
(+),
.
,
,
.
, ,
, .
,
.
, .
.
. .
#1nc1ude <fstreani>
#1nclucle <vector>
using namespace std:
1nt main(){
Ifstream in ("1npnum.txt");
vector<1nt> v;
int x:
while ( in x. lin.eofO)
v.push__back(x):
for (vector<int>::iterator i = v.beginO; i !- v.endO: ++i)
cout *i " ";
}
,
vector ~ vector<int>. v
. ,
. push_back
, i.
^ ,
. ,
(. . 301).
12. 299
i
(, ::
, i
).
, . beginO
, end ,
. , .
!=, < <= .
(1++) ,
.
, .
vector deque list.
, .
for
for (int 1 = 0; i<v.size(); i++) cout v[1] " ":
[ ],
list, .
(vector)
( ):
explicit vectorO: // 1
explicit vector(size_type . const & value = TO); 111
template <class InputIter> // 3
vectordnputlter f i r s t . Inputlter last);
vectorCconst vector<T>& x); //4
explicit ,
(. . 197).
1 .
2
val ue.
,
.
value. ,
.
,
.
,
(. ).
300 ill.
.
.
3
. .
4 .
:
/ / 10 :
vector <1nt> v2 (10. 1);
/ / , v l :
vector <int> v4 (vl);
/ / , v l :
vector <1nt> v3 (vl.beginO. vl.begInO + 2);
/ / 10 monstr (. . 183)
/ / ( ):
vector <monstr> ml (10):
/ / 5 monstr
/ / ( char*):
vector <monstr> m2 (5. monstr("Bac")):
vector :
vector<T>& operator=(const vector<T>& ):
void ass1gn(s1ze_type . const & value):
template <class Inputlter>
void assigndnputlter f i r s t . Inputlter l a s t ) :
.
, .
,
.
assign 2,
. assign
, first last, 3,
:
vector <int> vl. v2:
/ / 10 v l 1:
vl.assign(lO.l):
// 3 vl vl[5]. vl[6]. vl[7]:
v2.assign(vl.begin() + 5. vl.beginO + 8):
vector . 12.
:
reference operator[](size_type n):
const_reference operator[](size_type n) const:
12. 301
capacity
. reserve ,
.
resize:
void resize(size__type sz. = );
,
sZy sizeO, .
, .
. ,
sizeO, sizeO - sz .
vector:
void push__back(const & value):
void pop_back();
iterator insert(iterator position, const T& value):
void insertCiterator position, size_type n. const T& value):
template <class InputIter>
void insertCiterator position, Inputlter f i r s t . Inputlter last);
iterator eraseCiterator position);
iterator eraseCiterator f i r s t , iterator last):
void swapO:
void clearO: / /
pushback ,
.
insert .
value , (),
, .
.
,
, :
vector <int> v(2). vl(3.9):
int m[3] = {3. 4. 5}:
v.insert(v.begin(), m. m + 3): // v: 3 4 5 0 0
vl.insert(vl.begin() + 1. v.beginO.v.beginO + 2);
// vl: 9 3 4 9 9
,
. ,
, .
.
, .
.
erase (
) , ( ):
vector <int> v;
for (int i = 1; i<6; i++)v.push_back(i);
12. 303
/ / v: 1 2 3 4 5
v.erase(v.beg1n()); / / v: 2 3 4 5
v.erase(v.beg1n(). v.beginO + 2); / / v: 4 5
,
, , .
erase , , ,
.
.
swap ,
:
vector <1nt> vl. v2;
vl.swap(v2); / / v2.swap(vl):
= , !=,<,,<= =.
, .
, ,
, (
). :
#1nc1ude <vector>
using namespace std;
vector <1nt> v7. v8;
1nt ma1n(){
for (int i = 0: 1<6: 1++)v7.push__back(i);
cout "v7: ":
for d n t 1 = 0; 1<6: 1++) cout v7[i] " ";
cout end!;
for (int 1 = 0; i<3; 1++)v8.push_back(1+l):
cout "v8: ":
for (int 1 = 0; i<3; i++) cout v8[i] " ":
cout end!:
i f (v7 < v8 ) cout " v7 < v8" endl:
else cout " v7 > v8" endl;
}
:
v7: 1 2 3 4 5
v8: 1 2 3
v7 < v8
, .
, , , , .
. 343.
(vector <booi>)
vector <boo1>
, ,
304 III.
1 .
. .
.
(flip).
reference, :
class reference{
friend class vector:
referenceO:
public:
-referenceO:
operator boolO const:
references operator=(const bool x):
references operator=(const references x):
void flipO:
}:
( 10 1,
).
#include <vector>
#include <iostream>
using namespace std:
vector <bool> V (10):
int main(){
forCint i = 0: i < v.sizeO: i++)cin v [ i ] :
for (vector <bool>:: constjterator p = v.beginO: p!=v.end(): ++p)
cout *p:
}
(deque)
, ,
,
.
,
. .
(. 12.1).
,
, .
.
,
(, 4
5, 2 1).
,
. .
12. 305
,
, , .
d.beginO
t
d.endO
. 12.1.
( ),
:
e x p l i c i t dequeO: // 1
e x p l i c i t deque(size__type . const & value = T O ) : // 2
template <class InputIter> // 3
dequeCInputlter f i r s t . InputIter l a s t ) :
dequeCconst vector<T>& x ) : //4
1 .
2
val ue.
3
. .
4 .
:
// 10 :
deque <int> d2 (10. 1):
/ / , v l :
deque <int> d4 ( v l ) :
/ / ,
/ / v l :
deque <int> d3 (vl.beginO. vl.beginO + 2):
/ / 10 monstr (. . 183)
/ / ( ):
deque <monstr> ml (10):
/ / 5 monstr
/ / ( char*):
deque <monstr> m2 (5. monstr("Bac ")):
306 ill.
deque , ,
, ,
,
.
, ,
.
,
.
,
.
,
.
,
:
void push_front(const & value);
void pop_front();
.
capacity reserve, resize
size.
,
14 . 343.
(list)
,
. list STL
,
.
,
, .
.
, ,
, , .
:
reference frontO;
const_reference frontO const;
reference backO:
const_reference backO const:
,
:
void push_front(const & value);
void pop_front();
void push_back(const T& value);
12. 307
void pop^backO:
, 11 st,
:
iterator i n s e r t d t e r a t o r position, const & value);
void i n s e r t ( i t e r a t o r position, size^type n. const T& value):
template <class Inputlter>
void i n s e r t ( i t e r a t o r position. Inputlter f i r s t . Inputlter l a s t ) :
iterator erase(iterator position);
iterator erase(iterator f i r s t , iterator l a s t ) :
void swapO:
void c l e a r O ;
capacity,
. ,
( ):
void resize(size_type sz. = ) ;
,
. (splice)
,
:
void spliceCiterator position. list<T>& x):
void spliceCiterator position. list<T>& x. iterator i ) ;
void splice(iterator position. list<T>& x. iterator f i r s t , iterator l a s t ) ;
.
,
, , ,
:
l i s t <int> L I . L2:
... //
L l . s p l i c e d l . b e g i n O + 4. L2);
. .
,
, .
.
.
.
, . :
#include <list>
using namespace std;
int main(){
list<int> LI;
list<int>::iterator i. j. k;
for (int i = 0; i<5; i++)Ll.push_back(i + 1);
for (int i = 12; i<14; i++)Ll.push_back(i);
308 HI.
unique
(. . 336),
. true, , false
. :
template <class BinaryPrecl1cate>
void uniqueCBinaryPredicate binary_pred);
merge:
void mergedist<T>& x);
template <class Compare> void merge(list<T>& x. Compare comp);
(
< ,
Compare). .
- ,
.
reverse
( ):
void reverseO;
:
#include <fstream>
#include < l i s t >
using namespace std;
void show (const char * s t r , const l i s t < i n t > &L){
cout str " : " endl:
for (list<int>: :const_iterator i = L.beginO; i != L.endO: ++i)
cout *i " ";
cout endl;
}
int main(){
list<int> L:
list<int>::iterator i;
int X;
ifstream inC'inpnum");
while ( in x, lin.eofO) L.push_back(x);
show(" ". L);
L.push_front(l):
i = L.beginO; L.insert(++i. 2);
show(" 1 2 ". L);
i = L.endO; L.insert(--i. 100);
show(" 100 ". L);
i = L.beginO; = *i; L.pop_front();
cout " " endl;
i = L.endO; x = *--i; L.pop_back();
cout " " x endl;
show("CnHCOK ". L);
L.remove(76);
310 ill.
(stack)
, ( . , . 119)
,
.
: , . ,
, ,
. (
) .
STL :
template <class , class Container = deque<T> >
class stack {
protected:
Container c:
12. 311
public:
explicit stack(const Containers = ContainerO):
bool empty const {return c.emptyO:}
size_type sizeO const {return c.sizeO;}
value_type& topO {return c.backO;}
const value_type& topO const {return c.backO:}
void push(const value_type& x) {c.push_back(x):}
void pop {c.pop__back();}
}:
( ) ,
push push^back,
pop _, , top
.
stack ,
.
, ,
. , ,
(. 303).
(
):
#include <fstream>
#include <vector>
#include <stack>
using namespace std:
int main(){
ifstream in ("inpnum"):
stack <int. vector<int> > s:
int x:
while ( in x. !in.eof()) s.push(x):
while (! s.empty()){
X = s.topO; cout << X " ":
s.popO:
}
}
i npnum:
56 34 54 0 76 23 51 11 51 11 76 88
:
88 76 11 51 11 51 23 76 54 34 56
(queue)
( . , . 121)
, .
, -
312 Hi.
( ,
).
STL :
template <class . class Container = cleque<T> >
class queue {
protected:
Container c:
public:
e x p l i c i t queueCconst Containers = ContainerO):
bool empty const (return c.emptyO:}
size_type sizeO const {return c.sizeO;}
value_type& frontO {return c . f r o n t O : }
const value_type& front const (return c . f r o n t O : }
value_type& back (return c.backO:}
const value_type& back const (return c.backO:}
void push(const value_type& x) (c.push_back(x):}
void popO {c.pop_front():}
}:
front back ,
(
).
(
, ):
#include <fstream>
#include < l i s t >
#include <queue>
using namespace std:
i n t main(){
ifstream in ("inpnum"):
queue <int. l i s t < i n t > > q:
i n t x:
while ( in x. l i n . e o f O ) q.push(x):
cout " q . f r o n t O : " q. front " ":
cout "q.backO: " q.backO endl:
while (! q.empty()){
q.popO:
cout " q . f r o n t O : " q.frontO " ":
cout "q.backO: " q.backO endl:
}
}
i npnum:
56 34 54 0 76 23 51 11 51 11 76 88
:
q . f r o n t O : 56 q.backO: 88
q . f r o n t O : 34 q.backO: 88
12. 313
q.frontO 54 q.backO
q.frontO 0 q.backO
q.frontO 76 q.backO
q.frontO 23 q.backO
q.frontO 51 q.backO
q.frontO 11 q.backO
q.frontO 51 q.backO
q.frontO 11 q.backO
q.frontO 76 q.backO
q.frontO 88 q.backO
q.frontO 0 q.backO 0
,
(. 343).
(priority_queue)
,
.
<; ,
.
,
, , ,
. (,
, ).
(. . 195 334),
:
template <class . class Container = vector<T>.
class Compare = less<typename Container::value_type> >
class priority_queue {
protected:
Container c;
Compare comp;
public:
e x p l i c i t priority_queue(const Compare& x = CompareO,
const Container& = ContainerO):
template <class InputIter>
priority_queue(lnputlter f i r s t . Inputlter last.
const Compare& x = CompareO. const Container& = ContainerO):
bool empty const {return c.emptyO:}
size_typesize() const {return c.sizeO:}
const value_type& topO const {return c . f r o n t O ; }
void push(const value_type& x):
void popO:
. , push, pop top.
314 Hi.
:
#1nclude <1ostream>
#include <vector>
#1nclucle <funct1ona1>
#1nclude <queue>
using namespace std:
i n t ma1n(){
priority_queue <int. vector<int>. less<int> > P;
i n t x;
P.push(13): P.pushCSl); P.push(200); P.push(17);
while (!P.empty()){
X = P.topO: cout " : " x end!:
P.popO:
}
}
:
: 200
: 51
: 17
: 13
,
<functional> (. ,
. 334). .
greater<THn>, 1_1<>, less_equa1<THn>.
,
.
:
#include <iostream>
#include <vector>
#include <functional>
#include <queue>
using namespace std:
class CompareSum{
public:
bool operator()(int x. i n t y){
i n t sx == 0. sy = 0:
while (x){sx += x ^ 10: x /= 10:}
while (y){sy += ^ 10: y/=10:}
return sx > sy :
}
}:
i n t main(){
priority__queue <int, vector<int>. CompareSum > P:
i n t x:
P.pushdS): P.push(51): P.push(200): P.push(17):
while (!P.empty()){
12. 315
,
, , ,
(
, ).
: (),
(multimap), (set), (multiset)
(bitset).
.
,
, .
, ,
. , -
, .
, . ,
STL, .
<> <set>.
pair,
<utility>:
template <class Tl. class T2> struct pa1r{
typedef Tl first__type:
typedef T2 second__type:
Tl f i r s t :
T2 second:
pairO:
pairCconst T1& x. const T2& y):
template <class U. class V> pa1r(const pa1r<U. V> &p):
}:
pair , .
first, second.
: ,
( ) .
,
.
316 ill.
( STL
). 1 2. 1.first < 2.first
1.first == 2.first && 1.second < 2.second.
make_pair:
template <class T l . class T2>
pair<Tl. T2> make_pair(const T1& x. const T2& y ) :
:
#include <iostream>
#include < u t i l i t y >
using namespace std:
i n t main(){
pair<int. double> pKlO. 12.3). p2(pl);
p2 = make_pair(20. 1 2 . 3 ) : / / p2 = pair <int. double >(20. 12.3)
cout " p i : " p i . f i r s t " " pi.second endl:
cout "p2: " p 2 . f i r s t " " p2.second endl:
p 2 . f i r s t -= 10:
i f (pi == p2) cout "pi == p2\n":
pi.second -= 1:
if (p2 > pi) cout "p2 > pl\n":
}
:
p i : 10 12.3
p2: 20 12.3
pi == p2
p2 > pi
<utility> <map> <set>
.
(map)
(), (multimap),
. ,
.
: ,
, (
. 195):
template <class Key. class . class Compare = less<Key> >
class map{
public:
typedef pair <const Key, T> value_type:
e x p l i c i t map(const Compare& comp = CompareO):
template <class InputIter>
12. 317
( ),
value_type Key .
,
.
, .
1 ,
, , .
.
, ,
. . 12.
[ ]:
& operator[](const Key & );
,
.
, ,
:
#1nclude <fstream>
#1nclucle <1ostream>
#1nclude <str1ng>
#1nclude <map>
using namespace std;
typedef map <string. long^ less <string> > map_sl: / / 1
1nt main(){
map_sl ml;
ifStream in("phonebook"):
string s t r ;
long num:
while (in num. ! i n . e o f ( ) ) { / /
in.getO: / /
getline(in. str): / /
m l [ s t r ] = num; / /
cout str " " num endl;
}
ml["Petya P."] = 2134622: //
map_sl :: iterator i;
cout "ml:" endl; //
for (i =ml.begin(): i !=ml.end(); i++)
cout (*i).first " " (*i).second endl;
i = ml.beginO; i++; //
cout " : ";
cout (*i).first " " (*i).second endl;
318 III.
cout ml [ s t r ] :
else{
cout (*ml.upper_bound(str)).first " " ;
cout (*mLlower_bound(str)).first " " ;
}
, ,
, ,
, :
Petya . / /
Petya . Petya .
count , (
1).
:
pair<iterator. bool> insertCconst value_type& x):
iterator insertCiterator position, const value_type& x):
template <class InputIter>
void insertdnputlter first. Inputlter last):
void erase(iterator position):
size_type eraseCconst key_type& x):
void erase(iterator first, iterator last):
void clearO:
. , ,
, : true,
(
), false ( ).
.
, ,
, . .
,
:
map_sl m2: / /
m2.insert(map_sl: :value__type("Lena". 3157725)):
str = "Anna":
num = 5536590:
m2.insert(make__pair(str. num)):
num = 5530000:
/ / :
m2.insert(mal<e_pair(str. num)):
i = ml.beginO:
m2.insert(*i): // m2 ml
m2["Lena"] = 2222222: //
for (i = m2.begin(): i != m2.end(): i++) //
cout (*i).first " " (*i).second endl:
320 III.
:
Anna 5536590
Ivanova N.M. 3563398
Lena 2222222
insert .
,
i.
.
, .
, ,
,
( ):
#1nclude <1ostream>
#1nclude <str1ng>
#1nclucie <map>
using namespace std:
typedef map <str1ng. long, less <str1ng> > map_sl:
typedef pair <string. long > pa1r__sl:
int ma1n(){
pair_sl p[3]-= { pa1r_^sl("Anna". 123123).
pair_sl("Maria". 234234).
pa1r_sl("Teresa". 345345)}:
map_sl ml:
map_sl :: iterator i = ml.beginO:
for ( i n t = 0: k<3: k++)
i = ml.insert(i. p[k]): / / sic!
for ( i = ml.beginO: i != ml.endO: i++)
cout (*i).first " " (*i).second endl:
return 0:
}
insert ,
.
:
erase , ,
, .
,
, .
swap:
template <class Key. class . class Compare>
void swap(map<Key. T. Compare>& x. map<Key. T, Compare>& y ) :
' , ,
.
12. 321
equdl_range (lower_bound(x).
upper_bound(x)) :
pa1r<1terator.iterator> equal_range(const key_type& ):
palr<const_1terator. constjterator>
equal_range(const key_type& x) const:
, , , ,
.
(multimap)
, (multimap)
.
[ ], insert
.
.
.
erase
. equal_range ,
. count
, 1.
.
(set)
,
, value_type Key.
. :
, :
template <class Key. class Compare = less<Key> >
class set{
public:
typedef Key key_type:
typedef Key value_type:
e x p l i c i t set(const Compare& comp = CompareO):
template <class Inputlter>
s e t d n p u t l t e r f i r s t . Inputlter last,
const Compare& comp = CompareO):
set(const set<Key. Compare>& x):
pair<iterator.bool> insert(const value_type& x):
iterator i n s e r t ( i t e r a t o r position, const value_type& x):
template <class InputIter>
void i n s e r t d n p u t l t e r f i r s t . Inputlter l a s t ) :
void erase(iterator position):
size_type eraseCconst key_type& x):
void erase(iterator f i r s t , iterator l a s t ) :
322 ill.
, , ,
. ,
:
#include <iostreani>
#include <set>
using namespace s t d ;
typedef set<int. less<int> > s e t j ;
set_i;:iterator i ;
i n t main(){
i n t a[4] = {4. 2. 1. 2};
setJ s i ; //
s e t J s2(a. a + 4 ) ; //
s e t J s3(s2); //
s2.insert(10); //
s2.insert(6);
for ( i = s2.begin(); i != s2.end(); i++) //
cout * i " ";
cout endl;
/ / equal_range;
pair < s e t j : ; i t e r a t o r . s e t J ; : i t e r a t o r > p;
p = s2.equal_range(2);
cout *(p.first) " *(p.second) end!;
p = s2.equal_range(5);
cout *(p.first) " *(p.second) endl;
return 0;
}
:
1 2 4 6 10
24
66
, .
.
,
. 364.
12. 323
(multiset)
,
, insert
.
. find
endO,
.
multiset
count, lower__bound, upper__bound equal_range, ,
.
(bitset)
^. bitset ,
,
. , 0.
<bitset>.
,
:
template<size_t N> class bitset { . . . } ;
bitset reference:
class reference {
friend class bitset:
referenceO:
public:
-referenceO:
references operator=(bool x): // b[i] = x:
references operator=(const references): // b[i] = b [ j ] :
bool operator~() const: // b[i]
operator bool const: // x = b [ i ] :
references f l i p O : // b [ i ] :
}:
,
long string^:
bitsetO: III
bitset(unsigned long val): // 2
^ , ,
int, long,
(. 31).
, ,
.
^ .
324 ill.
e x p l i c i t bitsetCconst string& s t r . // 3
string::size_ty pos = 0.
string::size_type n = string::npos):
,
long
.
, ( ,
invalid_argument)
.
,
. .
:
bitset <100> 1; //
bitset <16> 2 (OxfOf): // 0000111100001111
bitset <16> (""): // 0000111100001111
bitset <5> 4 ("00110011". 3): // 10011
bitset <3> 5 ("00110101". 1. 3); // 011
:
bool operator==(const bitset<N>& rhs) const:
bool operator!=(const bitset<N>& rhs) const:
bitset<N>& operator&=(const bitset<N>& rhs):
bitset<N>& operator I=(const bitset<N>& rhs):
bitset<N>& operator^=(const bitset<N>& rhs):
bitset<N> operator(size__t pos) const:
bitset<N> operator(size_t pos) const:
bitset<N>& operator=(size_t pos):
bitset<N>& operator=(size_t pos):
bitset<N>& s e t O :
bitset<N>& set(size_t pos. i n t val = true):
bitset<N>& resetO:
bitset<N>& reset(size_t pos):
bitset<N> operator-O const:
bitset<N>& f l i p O :
bitset<N>& f l i p ( s i z e _ t pos):
reference operator[](size_t pos): // b[i]:
( = ) (!=).
, pos
. .
set 1, reset 0. ~
. flip
, .
.
,
out of range.
12. 325
bitset
, :
unsigned long to__ulong() const; // unsigned long
string to_string() const: // string
size_t count const: // 1
size_t sizeO const: //
bool test(size_t pos) const: // true, b[pos] == 1
bool anyO const: // true, 1
bool noneO const: // true, 1
.
'' ' , .
,
,
.
, ,
, .
,
.
#1nclude <fstream>
#1nclude <1oman1p>
#1nclude <str1ng>
#1nclude <set>
#include <map>
using namespace std:
typedef set<1nt. less<int> > s e t j :
typedef map<str1ng. set_1. less<string> > map^ss:
bool wordreaddfstream &1n. string &word. int &num){
char ch:
// :
for (::){
1n.get(ch):
if (in.fanO) return false:
1f (Isalpha(ch) || ch == '__') break:
if (ch == '\n') num++:
}
word = "":
// :
do{
word += tolower(ch):
1n.get(ch):
}wh1le (!in.fa1l() && (isalpha(ch) || ch == '_')):
if (in.failO) return false;
326 III.
In.putback(ch); // - '\'
return true;
}
1nt main(){
map_ss m;
niap_ss: [iterator 1m;
set_1;;iterator is. isbegin, isend;
string word;
int num = 1;
ifstream in ("some_file");
if (!in){cout "Cannot open input file.\n"; exit(l);
}
while (wordread(in. word. num)){
1m = m.find(word);
if (im == m.endO)
im = m.insert(map_ss;;value_type(word. set_i())).first;
(*im).second.i nsert(num);
}
for (im = m.beginO; im != m.endO; im++){
cout setiosflagsdos; ; l e f t ) setw(15) (*im).f1rst.c_str();
isbegin = (*im). second. beginO;
isend = (*im). second. endO;
for (is = isbegin; is != isend; is--+)
cout " " * i s ;
cout endl;
}
return 0;
}
, some_fi 1 e :
class value_compare;
public binary_funct1on <value_type. value_type. bool> {
friend class map; protected;
Compare comp;
value_compare(Compare c) ; comp(c) {} public;
bool operatorO (const value_type& x. const value_type& y) const {
return comp(x.first, y . f i r s t ) : }
}:
:
binary_function 2
bool 2 6
5
class 1 3
comp 4 58
compare 4 5
const 7
first 8
12. 327
friend 3
map 3
operator 6
protected 3
public 25
return 8
value_compare 15
value_type 27
X 78
78
. wordread
, , -
, .
, -
. ,
. ,
. ,
.
f i nd ,
. ( endO),
insert , (
set_i()). insert ,
(first) .
im.
insert
, (second) .
.
.
13
.
,
.
, ,
(. 343).
.
.
.
. ,
,
, ,
.
,
, , ,
.
, .
.
* ->. -
13. 329
++.
, .
,
, , .
, .
, . 13.1.
.
1 j , ,
, . :
1++ ++1 1 = j 1 == j 1 != j
1 3 . 1 .
(input) X = *i
(output) *i = X
(forward) X = * i , *i = X
X = * i , * i = X,
(bidirectional) - - i , i--
X = * i , * i = X, , 1 i st
(random access) - - i , i--
i + n, i - n, i += n, i -= n
1 < j , 1 > j , 1 <= j , 1 >= j
,
,
.
, , ,
.
, ,
.
, ,
. ,
,
. ,
,
.
<iterator>.
.
.
,
.
330 ill.
( -
) .
:
;
, , ;
.
,
. .
. .
,
(, ),
. <1terator>
1terator_tra1ts (trait
).
template<class Iter> struct iterator_traits{
typedef typename Iter: :differGnce_type cl1fference_type:
typedef typename Iter::value_type value^type:
typedef typename Iter::pointer pointer;
typedef typename Iter::reference reference;
typedef typename Iter: :iterator_category iterator__category;
}:
typename ,
Iter . iterator_category , ,
. di f ference_type
.
:
struct input_iterator_tag{};
struct output_iterator_tag{};
struct forwardJterator_tag: public inputJterator_tag{};
struct bidirectional_iterator_tag:
public forward_iterator_tag{};
struct random_accessJterator___tag:
public bi di recti onal_iterator__tag{};
,
,
, ,
.
iterator^traits (
. 220):
tempiate<class > struct iterator_traits<T*>{
typedef ptrdiff^t difference__type;
13. 331
typedef va1ue_type:
typedef * pointer;
typedef & reference:
typedef random_access_iterator_tag iterator_category:
}:
ptrd1ff_t,
<cstddef>.
const *.
+
-, distance advance. distance
:
distancednputlterator f i r s t . Inputlterator last);
di fference_type,
. ,
, ++ ,
,
. advance,
i += :
void advance(InputIterator& i . Distance n);
n
.
, . ,
, reverse_
iterator:
template <class It>
class reversejterator ; public
iterator<typename iterator__traits<It>; ;iterator_category.
typename iterator_traits<It>; ;value__type.
typename iterator__traits<It>; ;difference_type.
typename iterator_traits<It>;;pointer.
typename iterator_traits<It>;;reference>{
protected;
It current;
public;
typedef It iterator_type;
reverseJteratorO;
explicit reversejterator (It x);
template <class U> reverse_iterator(const reverseJterator<U>& u);
It baseO const;
reference operator*() const;
pointer operator->() const;
332 III.
reverseJterator& operator++();
reverse__1terator operator++(1nt);
reverseJterator& operator--();
reversejterator operator--(int);
reverse__1terator operator^ (d1fference_type ) const:
reverseJterator& operator+=(d1fference_type n);
reverse_iterator operator- (difference^type n) const;
reverseJ terator& operator-=(difference__type n):
reference operator[](difference__type n) const;
}:
,
current.
:
template <class It>
reverse_iterator<It>& reverse_1terator<It> operator++(){
--current;
return '^thls;
}
,
, ,
, . current
, , .
:
&*(reverseJterator(i)) == &*(i - 1).
= , !=,
<, <= >=.
. , rbeginO rendO,
reverse_iterator. ,
, :
vector <1nt> v;
, , .
,
.
, :
back_insert_iterator, front_insert iterator insert iterator.
13. 333
, :
template <class > back_1nsert_iterator<C>
back_1nserter(C& );
template <class C> front_1nsertJterator<C>
front_1nserter(C& x);
template <class class Iter> 1nsert_1terator<C>
1nserter(C& x. I t e r 1);
, .
back_1nserter , front_inserter ,
Inserter , -.
(. 355),
.
,
,
/. .
:
istreamjterator ostreamjterator.
,
, HPIM ,
. ,
temp :
1 stream inC'temp"):
1stream_iterator<1nt> K i n ) ;
i n t buf = * 1 ;
, :
++1 ;
1nt bufl = * i ;
. ,
:
while ( i != istream_iterator<int> )
cout *i++ " ";
. , , .
,
.
,
, i == j , ,
++i == ++j. -
334 III.
. ,
.
, .
,
:
ostreani_1terator<int> os(cout. " ");
*os = 100: // : 100
++0S: *os = 2; // : 2
:
ostream_iterator& operator=(const & value){
*out_stream value:
ifCdellm != 0) *out_stream delim;
return (nhis):
}
out_streani ,
, del im , .
. 350.
,
(. . 195).
(. 313).
.
,
, .
,
() .
,
.
<functional>. ,
bool. .
, bool.
,
, :
template <class Arg. class Result>
struct unary_function{
typedef Arg argunient_type:
typedef Result result_type:
}:
13. 335
, C++.
13.2.
plus X +
minus X -
multiplies X *
divides X /
1 modulus X ^
negate -X
1 us (
):
template <class > struct plus : binary_function<T. . >{
operatorO(const & . const & ) const{
return X + ;
}
}:
336 Hi.
,
. 356, 370.
, C++.
bool, .
13.3.
equal_to X ==
not_equal_to X !=
greater X >
less X <
greater_equal X >=
less_equal X <=
log1cal_ancl X &&
log1cal_or X II
log1cal_not ! X
equal^to (
):
template <class > struct equal_to :
b1nary_function<T. . bool>{
bool operatorO(const T& x. const T& y) const{
return X == y;
}
}:
. ,
. , . 183 monstr
, ammo, :
struct monstr_less__ammo:
public b1nary_funct1on<monstr. monstr. bool>{
bool operator 0(monstr & ml, monstr & m2){
return ml.get_ammo() < m2.get_ammo();
}
notl not2
.
. , less<int>(),
not2(less<int>()). greater_equal<int>.
,
,
.
.
unary_negate binary^negate, unary^function
binary_function .
, :
template <class Pred1cate>
class unary_negate: public
unary__function<typename Predicate::argument_type. bool>{
public:
e x p l i c i t unary__negate(const Predicates pred):
bool operatorO
(const typename Predicate::argument_type& x) const:
}:
() , notl not2
, :
template <class Predicate>
unary_negate<Predicate> notKconst Predicates pred):
template <class Predicate>
binary_negate<Predicate> not2(const Predicates pred):
.
, .
,
. bind2nd
bindlst,
.
,
f
, val ue.
, f
val ue .
binder2nd binderlst. -
:
template <class Op. class >
binder2nd<0p> bind2nd(const Op& op. const T& x):
338 III.
,
, .
pointer^tounaryfunction pointer_to_binary_
function, - ptrfun ,
.
:
template <class Arg. class Result>
class pointer__to__unary__funct1on : public unary_function<Arg. Result>{
public:
e x p l i c i t pointer__to_unary__function(Result (*f)(Arg));
Result operator()(Arg x) const; / / f(x)
}:
template <class Arg. class Result>
poi nter_to__unary_functi on<Arg, Resul t>
ptr__fun(Result (*f)(Arg));
template <class Argl. class Arg2. class Result>
13. 339
color sk1n;
char *name;
public:
monstrdnt he = 100. int am = 10):
monstr(color sk):
monstr(char ^ nam):
monstr(const monstr &M):
-monstrO {delete [] name:}
operator int(){return health:}
int get_health(){:return health:}
friend ostream&
operator (ostream & out. monstr & m){
return out "monstr: " " ammo = " m.ammo
" health = " m.health endl:}
}:
monstr::monstr(int he. int am):
health (he), ammo (am), skin (red), name (0){}
monstr::monstr(color sk){
switch (sk){
case red :
health = 1: ammo = 1 0 : skin = red: name = 0: break:
case green:
health = 2: ammo = 2 0 : skin = green: name = 0: break:
case blue:
health = 3: ammo = 40: skin = blue: name = 0: break:
}}
monstr::monstr(char ^ nam){
name = new char [strlen(nam) + 1]: strcpy(name. nam):
health = 200: ammo = 1 0 : skin = red:
}
bool less_health (monstr ml. monstr m2){
return ml.get__health() < m2.get_health() :}
int main(){
vector <monstr> m:
monstr M (10. 30):
m.push_back(M);
13. 341
m.push_bacl<(monstr("vas1a")):
m.push_back(monst(red));
cout " M0N5TRY:" endl;
for ( i n t 1 = 0: 1<ni.size(): i++) cout m[1] " ";
cout endl;
cout " COUNT_IF: ":
cout countJf(m.begin(). m.endO.
b1nd2nd(ptr^fun(less_health). 20)):
return 0:
}
less_health health
monstr. health ,
. bind2nd
,
ptr_fun. (
2 ):
MUNb1KY:
monstr: ammo == 30 health == 10
monstr: ammo = 10 health = 200
monstr: ammo = 10 health = 1
COUNT IF : 2
.
for_each. ,
, :
#1nclude <1ostream>
#1nclude <algor1thm>
using namespace std:
void showdnt a){ cout a endl:}
i n t ma1n(){
1nt m[4] = {3. 5. 9. 6}:
for_each(m. m + 4. show):
return 0:
}
,
, show
,
.
. , -
342 III.
, .
:
13.4.
mem fun mem fun t
mem fun const mem fun t
i mem_fun mem_funl_t
mem_fun_ref mem_fun_ref _t
mem_fun_ref const_mem_fun_ref _t
mem_fun_ref mem_funl_ref _t
mem_fun_ref const_mem_funl_ref _t
mem_fun_ref const_mem_funl_t
monstr dead, ,
:
bool deadO{return !health;}
count_1 f
, , ,
vector <monstr> ostrich (100):
:
cout count_1f(ostr1ch.beg1n(). ostrich.endO.
mem fun ref(&monstr::dead));
14
STL
.
,
.
(. . 334).
,
, ,
,
, .
<algorithm>, <funct1onal>.
STL :
;
;
, ;
;
, ,
<numeric> (. 15
, . 369).
,
.
, . ,
(sort)
, 1 ist.
.
, ,
STL.
. , ,
, .
344 III.
:
In ;
Out ;
For ;
1 ;
Ran ;
Pred ();
BinPred ;
;
;
BinOp .
, .
.
1 4 . 1 .
adjacent_f1nd
count
countjf
equal
find 11
f1ncl_end
f1nd_first_of
findjf
for_each
mismatch
search
search_n -
.
14. 345
adjacent^find
acljacent_f1nd .
tempiate<class For> For adjacent_f1nd(For f i r s t . For l a s t ) ;
tempiate<class For, class B1nPred>
For adjacent_f1nd(For f i r s t . For last. BinPred pred):
( , ).
, ,
pred .
(
, ):
#include <algorithm>
using namespace std:
struct A{ i n t X. y : } :
bool f(A &al. A8. a2){
return al.x + al.y == a2.x + a2.y:}
int main (){
i n t m[8] = {45. 60. 60. 25. 25. 2. 13. 35}:
cout *(adjacent_find(m. m + 8)): / / : 60
A ma[5] = {{2.4}. {3.1}. {2.2}. {1.2}. {1.2}}:
cout (*adjacent_find(ma. ma + 5. f)).x endl: // :3
return 0:
count, countjf
count
:
tempiate<class In, class T>
typename iterator_traits<In>::difference_type
countdn f i r s t . In last, const T& value):
val ue.
difference_type (. . 330).
count_i f
:
tempiate<class In. class Pred>
typename iterator_traits<In>::difference_type
c o u n t j f (In f i r s t . In last. Pred pred):
, , pred
.
count_i f . 338.
346 III.
equal
equal
. , ,
:
template <class Inl. class 12>
bool equal(Inl f i r s t l . Inl lastl. In2 first2);
template <class Inl. class In2, class B1nPred>
bool equal(Inl f i r s t l . Inl lastl. In2 first2. BinPred pred);
find, f i n d j f
find .
find value:
tempiate<class In. class T>
In finddn f i r s t . In last, const T& value);
findif ,
pred:
tempiate<class In. class Pred>
In f i n d j f d n f i r s t . In last. Pred pred);
.
,
(
):
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class InJ0__50{
public:
bool operator()(int x) {return x > 10 && x < 50;}
}:
int main(){
ifStream in ("inpnum"):
vector<int> v;
int x;
while ( in x. lin.eofO)
v.push_back(x);
for (int i = 0: i<v.size(): i++) cout v[i] " ":
cout endl;
// , 51:
cout *find(v.begin(). v.endO. 51) endl:
14. 347
/ / , 10<<50:
cout * f i n d j f ( v . b e g i n ( ) . v.endO. 1_10_50()) endl:
return 0:
}
:
56 34 54 76 23 51 11 51 11 76 88
51
34
find_first_of
f1nd_first_of
:
tempiate<class Fori, class For2>
Fori f1nd_f1rst_of(Forl f i r s t l . Fori l a s t l .
For2 f1rst2. For2 last2):
tempiate<class Fori, class For2. class B1nPred>
Fori find_f1rst_of(Forl f i r s t l . Fori l a s t l .
For2 f i r s t 2 . For2 last2. BinPred pred);
.
, ,
,
.
lastl.
find_end
f 1 nd_end
( )
:
tempiate<class Fori, class For2>
Fori find_end(Forl f i r s t l . Fori l a s t l .
For2 f i r s t 2 . For2 last2):
tempiate<class Fori, class For2. class BinPred>
Fori find_end(Forl f i r s t l . Fori l a s t l .
For2 f i r s t 2 . For2 last2. BinPred pred);
lastl.
for^each
:
tempiate<class In. class Function>
Function for__each(In f i r s t . In last. Function f ) ;
. 341.
348 III.
mismatch
mismatch
:
tempiate<class I n l . class In2>
pa1r<Inl, In2> mismatchdnl f i r s t l . I n l l a s t l .
In2 f i r s t 2 ) :
template <class I n l . class In2. class BinPred>
pa1r<Inl. In2> mismatchdnl f i r s t l . I n l l a s t l .
In2 f1rst2. BInPred pred);
.
, , .
search, search^n
search
( )
:
tempiate<class Fori, class For2>
Fori searchCForl f i r s t l . Fori l a s t l .
For2 f1rst2. For2 last2);
tempiate<class Fori, class For2. class B1nPred>
Fori search(Forl f i r s t l . Fori l a s t l .
For2 f1rst2. For2 last2. BInPred pred):
lastl.
search_n ,
value ( )
:
tempiate<class For. class Size, class T>
For search_n(For f i r s t . For last.
Size count, const T& value):
template <class For. class Size, class T. class B1nPred>
Fori search_n(For f i r s t . For last.
Size count, const T& value. BInPred pred):
, . , ,
.
14. 349
14.2,
,
1 backward ,
1 fill
1 f^^Ln
1 generate
generate n
1 iter_swap ,
random_shuffle
remove
remove_copy
remove_copy_1f
remove_1f
1 replace
replace_copy
replace_copy_1f
replacejf
reverse
reverse_copy
rotate
rotate_copy
swap
swap_ranges
transform
unique
unique_copy
.
350 ill.
, copy^backward
, first last,
, result:
tempiate<class In. class Out>
Out copy(In first. In last. Out result);
copy^backward ,
. ,
,
:
tempiate<class B i l . class Bi2>
Bi2 copy__backward(Bil f i r s t . B i l last. Bi2 result):
.
, .
:
#include <iostream>
#include <algorithm>
using namespace std;
i n t main(){
i n t b [ 4 ] . a[5] - { 1 . 2. 3. 4. 5}. i ;
copy (a + 1. a + 5. b):
for ( i = 0: i < 4; i++) cout b [ i ] : //2345
cout endl:
copy (a + 1. a + 5. a);
for ( i = 0: i < 5: i++) cout a [ i ] : //23455
cout endl:
copy__backward (b. b + 3. b + 4);
for ( i = 0: i < 4; i++) cout b [ i ] : //2234
cout endl:
return 0:
}
copy .
(. . 333):
#include <iostream>
#include <algorithm>
using namespace std:
int main(){
const int N = 5;
int a[N] = {1. 2. 3. 4. 5};
copy (a. a + N. ostreamJterator<int>(cout. " " ) ) :
cout endl:
return 0; .
}
14. 351
fill, flll_n
f 111 ,
first last, value.
fin_n :
tempiate<class For. class T>
void f i l l ( F o r f i r s t . For last, const T& value);
tempiate<class Out, class Size, class T>
void fill_n(Out f i r s t . Size n. const T& value);
:
#include <iostream>
#include <algorithm>
using namespace std;
i n t main(){
int a[5]. i ;
f i l l (a. a + 5. 1);
for (i = 0; i < 5; i++) cout a[i]; / / 1 1 1 1 1
cout endl;
f i l l _ n (a + 2. 2. 0);
for (i = 0; i < 5; i++) cout a[i]; / / 1 1 0 0 1
cout endl;
return 0;
}
,.
fill (. + 5. 1);,
.
, .
generate, generate^n
generate .
,
gen,
.
tempiate<class For. class Generator>
void generateCFor f i r s t . For last.
Generator gen);
tempiate<class Out. class Size, class Generator>
void generate_n(Out f i r s t . Size n. Generator gen);
:
#include <iostream>
#include <algorithm>
using namespace std;
int f ( ) {
static i n t i = 1;
return (++i) * 3;
352 III.
1nt ma1n(){
int a[5]. 1;
generateCa, a + 5. f):
for ( 1 = 0 : i<5; i++) cout a[i] " ": // 6 9 12 15 18
return 0;
random^shuffle
random_shuffle ()
.
.
.
, i nt
.
tempiate<class Ran>
void random_shuffle(Ran f i r s t , Ran l a s t ) :
tempiate<class Ran, class RandomNumberGenerator>
void random_shuffle(Ran f i r s t . Ran last,
RandomNumberGenerator&, rand):
rand,
<ti.h>:
#include <iostream>
#include <algorithm>
#include <time.h>
using namespace std:
struct random_gen{
random_gen(){srand((unsigned int)time(NULL)): }
int operator()(int n){return randO % n:}
}:
int mainOj
14. 353
:
0123401234
0134013434
01340134
remove_1f erase
, 10 50:
#1nclucle <iostream>
#1nclude <algor1thm>
#1nclude <vector>
using namespace std;
bool In_10_50 (int X) (return x > 10 && x < 50;}
int ma1n(){
vector<1nt> a;
1nt 1;
for (1 = 1; i<10; i++) a.push_back(i*10):
for (1 = 0: 1<a.s1ze(); i++) cout a[1] " ";
cout endl:
vector<int>::iterator new_end = remove_if(a.begin(), a.endO. In_10_50);
a.erase(new_end. a.endO);
for (i = 0; i<a.size(); i++) cout a[i] " ";
cout endl;
return 0;
}
:
10 20 30 40 50 60 70 80 90
10 50 60 70 80 90
,
b ( ) ,
10 50, 33:
#include <iostream>
#1nc1ude <vector>
#1nclude <algor1thni>
using namespace std:
boo! In_10_50(int x){
return X >10 && X < 50;
}
i n t ma1n(){
vector<1nt> a. v:
vector<1nt>::1terator 1;
for ( i n t = 1: < 10; k++)a.push_back(k * 10);
for ( i = a.beginO; i != a.endO; i--+) cout * i " ";
cout endl;
replace_copy_if(a.begin(). a.endO.
inserterCv. v.beginO). In_10_50. 33);
for ( i = V.beginO; i != v.endO; i++) cout * i " ";
cout endl;
return 0;
}
( ,
):
10 20 30 40 50 60 70 80 90
10 33 33 33 50 60 70 80 90
reverse, reverse^copy
reverse
, reverse^copy
:
tempiate<class Bi>
void reverse(Bi f i r s t . Bi l a s t ) ;
tempiate<class Bi. class Out>
Out reverse_copy(Bi f i r s t . Bi last, Out result);
rotate, rotate^copy
rotate
, rotate^copy :
tempiate<class For>
void rotate(For first. For middle. For last);
tempiate<class For. class Out>
Out rotate_copy(For first. For middle. For last.
Out result);
356 ill.
, ,
, .
. ,
1 nt [5] = {1.2.3.4.5} rotateCa. +2. +5)
, 34512.
rota te( , +3. +5), .
transform
transform
. ,
,
, result:
tempiate<class In. class Out. class Op>
Out transformdn f i r s t . In last. Out result. Op op):
,
result:
tempiate<class I n l . class In2. class Out. class B1naryOperation>
Out transformdnl f i r s t l . I n l l a s t l .
In2 f1rst2. Out result. BinaryOperation binary_op):
. unary_
function binary_function (. . 334).
transform
: = -^ - -^,
b negate.
#inclucle <iostream>
#include <algorithm>
#inclucle <functional>
using namespace std:
struct preobr: binary_function <double. double. double>{
double operator()(double x. double y) const{
return X * X - * y;}
}:
i n t main(){
const i n t m = 5;
double a[m] = {5. 3. 2-. 3. 1}.
b[m] = ( 1 . 10. -3. 2. -4}:
transformCa. a + m. b. a. preobrO):
transform(b. b + m. b. negate<double>()):
int i:
for (i = 0: i<m: i++) cout a[i] " ";
cout endl:
for (i = 0: i<m: i++) cout b[i] " ":
14. 357
cout endl;
return 0;
}
:
24 -91 -5 5 -15
-1 -10 3 - 2 4
unique, unique^copy
unique
, ,
pred. ^.
.
tempiate<class For>
For un1que(For f i r s t . For l a s t ) ;
tempiate<class For. class BinPred>
For uniqueCFor f i r s t . For last, BinPred pred):
unique_copy :
tempiate<class In. class Out>
Out unique_copy(In f i r s t . In last. Out result):
tempiate<class In. class Out, class BinPred>
Out unique_copy(In f i r s t . In last.
Out result, BinPred pred);
,
,
, , ,
, . .
14.3. ,
b1nary_search
equal_range
lnplace_merge
lex1cograph1cal_compare
lower_bound
list.
358 Hi.
14.3 ()
max
max_element
merge
min
min element
next_permutat1on
nth_element -
partial_sort
partial_sort_copy
partition ,
prev_permutation
sort
stable_partition ,
,
stable_sort ,
upper_bouncl , ,
.
: <, ,
. ,
.
binary_search
binary_search value
, first last. ,
. ,
(
).
tempiate<class For. class T>
bool b1nary_search(For f i r s t . For last,
const T& value);
tempiate<class For. class T. class Compare>
bool binary_search(For f i r s t . For last,
const T& value. Compare comp):
14. 359
equaLrange
equal__range
,
. .
,
(*. value) == false &&
comp(value. *) == false.
tempiate<class For. class T>
pa1r<For. For> equal__range(For f i r s t . For last,
const T& value);
tempiate<class For. class T. class Compare>
pa1r<For. For> equal_range(For f i r s t . For last,
const T& value. Compare comp);
, 2 4 5 5 7 9 12 18 equal_range value = 8
, 9 9,
value = 5 , 5, 7.
inplace.merge
inplace_merge
.
, .
tempiate<class Bi>
void inplace_merge(Bi f i r s t . Bi middle. Bi last);
tempiate<class Bi. class Compare>
void inplace_merge(Bi f i r s t , Bi middle. Bi last.
Compare comp);
lexicographicaLcompare
lexicographical_compare
<,
. true,
(
), false
. ,
.
tempiate<class Inl, class In2>
bool lexicographical_compare
(Inl f i r s t l . Inl lastl. In2 first2. In2 last2);
tempiate<class Inl. class In2. class Compare>
bool lexicographical_compare
(Inl f i r s t l . Inl lastl. In2 first2. In2 last2.
Compare comp);
360 Hi.
:
#1nclude <1ostream>
#1nclude <algorithm>
#1nc1ude <funct1onal>
using namespace std;
int main(){
const int m = 5;
double a[m] = {5. 3 . 2.3. 1}.
b[m] = {5. 3. 2. 3. 2}.
c[m] = {5. 3. 1. 3.10}
cout lexicograph'ical__compare(a. a + m, b. b + m) // 1
cout lexicograph'ical__compare(a. a + m. c. + m) // 0
cout lexicograph-ical__compare(a. a + m. b. b + m. greater<int-'()): // 0
return 0;
lower^bound, upper^bound
lower_bound , upper_bound
,
, .
tempiate<class For. class T>
For lower_bound(For f i r s t For last, const T& value);
tempiate<class For. class T class Compare>
For .lower_bound(For f i r s t For last, const T& value.
Compare comp);
tempiate<class For. class T>
For upper_bound(For first For last, const T& value);
tempiate<class For. class T. class Compare>
For upper_bound(For first For last, const T& value.
Compare comp);
max, mm
max , min ,
<, .
tempiate<class > const & min(const & . const & b);
tempiate<class . class Compare>
const T& min(const T& a. const T& b. Compare comp);
tempiate<class T> const T& maxCconst T& a, const T& b);
tempiate<class T. class Compare>
const T& max(const T& a. const T& b. Compare comp);
max_element, min^element
max_element
, min_element .
14. 361
tempiate<class For>
For min_element(For f i r s t . For l a s t ) :
tempiate<class For. class Compare>
For min__element(For f i r s t . For last. Compare comp);
tempiate<class For>
For max__element(For f i r s t . For l a s t ) ;
tempiate<class For. class Compare>
For max__element(For f i r s t . For last. Compare comp):
merge
merge .
tempiate<class I n l . class In2. class 'Out>
Out mergednl f i r s t l . I n l l a s t l . In2 f i r s t 2 .
In2 last2. Out result):
tempiate<class I n l , class In2. class Out. class Compare>
Out mergednl f i r s t l . I n l l a s t l . In2 f i r s t 2 .
In2 last2. Out result. Compare comp):
,
.
(
, , , ).
:
#include <iostream>
#include <algorithm>
using namespace std:
int main(){
const int m = 5:
doublea[m] = {3. 4. 8. 17. 20}.
b[m] = (5. 6. 8. 10. 35}. c[m * 2 ] :
i nt i :
merge(a. a + m. b. b + m. c):
for ( i = 0: i < m * 2: i++)
cout c [ i ] " ": //345688 10 17 20 35
cout end!:
return 0:
next^permutation, prev^permutation
.
! (1*2*...'^).
next_permutation
, prev_permutation .
true, , false
.
362 III.
tempiate<class Bi>
bool next__permutat1on(B1 f i r s t . B1 l a s t ) ;
tempiate<class B1. class Compare>
bool next_permutat1on(B1 f i r s t . Bi last. Compare comp);
tempiate<cl ass Bi>
bool prev_permutation(Bi f i r s t . Bi l a s t ) ;
tempiate<class B i . class Compare>
bool prev_permutation(Bi f i r s t Bi last. Compare comp);
:
#include <iostream>
#inclucle <algorithm>
using namespace s t d ;
i n t main(){
const i n t m = 3;
i n t a[m]={l. 4. 2}. b[m];
int i ;
copy(a. a + m. b);
cout " next_permutation(a. a + m);" endl
while (next_permutation(a. a + m)){
for ( i = 0; i < m; i++) cout a [ i ] " "
cout endl;} .
cout " prev_permutation(b. b + m);" endl
while (prev_permutation(b. b + m)){
for ( i = 0; i < m; i++) cout b [ i ] " "
cout endl;}
return 0;
:
next_permutation(a. a + m):
2 1 4
2 4 1
4 1 2
4 2 1
prev_permutation(b. b + m):
1 2 4
nth_element
.
, nth, ,
, ,
, , .
tempiate<class Ran>
void nth_element(Ran first. Ran nth. Ran last);
tempiate<class Ran. class Compare>
14. 363
partition, stable_partition
partition , ,
. stable_part1t1on
, .
.
tempiate<class 1. class Pred>
1 part1t1on(B1 f i r s t , 81 last. Pred pred):
tempiate<class 81. class Pred>
81 stable_part1t1on(81 f i r s t . 81 last. Pred pred):
364 Ml.
sort, stable^sort
sort
, N-log2N.
stable_sort.
N-(log2N)2.
tempiate<class Ran>
void sort(Ran f i r s t . Ran l a s t ) ;
tempiate<class Ran. class Compare>
void sortCRan f i r s t . Ran last. Compare comp);
tempiate<class Ran>
void stable_sort(Ran f i r s t , Ran l a s t ) ;
tempiate<class Ran. class Compare>
void stable_sort(Ran f i r s t . Ran last. Compare comp);
,
.
.
, ,
.
, .
^ ,
:
a [ i ] < a[2*i + 1]
a [ i ] < a[2^i + 2]
10 : 23 20 21 17 19 18 15 12 10 14.
,
,
(. . 313),
.
.
, ,
: <, ,
.
( heap).
14. 365
14.4.
includes
set_i intersection
set_di fference
,
set_symmetri c_di fference
,
set_union
inake_heap
pop_heap
push_heap
sort_heap
includes
includes
. true ,
[first2. Iast2) [ f i r s t l . 1astl)^.
tempiate<class I n l . class In2>
bool includesdnl f i r s t l . I n l l a s t l .
In2 f i r s t 2 . In2 last2);
tempiate<class I n l . class In2. class Compare>
bool includes ( I n l f i r s t l . I n l l a s t l .
In2 f i r s t 2 . In2 last2. Compare comp);
setjntersection
setjntersection (
, , ,
):
tempiate<class I n l . class In2. class Out>
Out s e t j n t e r s e c t i o n
( I n l f i r s t l . I n l l a s t l . In2 f i r s t 2 . In2 last2.
^ ,
, ( ).
366 ill.
Out result):
tempiate<class Inl. class In2. class Out.
class Compare>
Out setjntersection (Inl firstl. Inl lastl.
In2 first2. In2 last2. Out result. Compare comp):
set_difference, set_symmetrlc_difference
set_difference Out ,
:
tempiate<class I n l . class In2. class Out>
Out set_difference(Inl f i r s t l . I n l l a s t l .
In2 f i r s t 2 . In2 last2. Out result): .
tempiate<class I n l . class In2. class Out. class Compare>
Out set_difference ( I n l f i r s t l . I n l l a s t l .
In2 f i r s t 2 . In2 last2. Out result. Compare comp):
set_symmetric_difference Out ,
:
tempiate<class I n l , class In2. class Out>
Out set_symmetric_difference(Inl f i r s t l . I n l l a s t l .
In2 f i r s t 2 . In2 last2. Out result):
tempiate<class I n l . class In2. class Out. class Compare>
Out set__symmetric__difference(Inl f i r s t l . I n l l a s t l .
In2 f i r s t 2 . In2 last2. Out result. Compare comp):
.
set^union
set_union (
, ):
tempiate<class I n l . class In2. class Out>
Out set_union(Inl f i r s t l . I n l l a s t l ,
In2 f i r s t 2 . In2 last2. Out result):
tempiate<class I n l , class In2, class Out, class Compare>
Out set_union(Inl f i r s t l . I n l l a s t l ,
In2 f i r s t 2 , In2 last2. Out result. Compare comp):
#include <iostream>
#include <algorithm>
using namespace std:
void show(const char *s. const int *begin.
const int *end){
14. 367
make^heap
make_heap
:
tempiate<class Ran>
void make_heap(Ran first. Ran last);
tempiate<class Ran. class Compare>
void make_heap(Ran first. Ran last. Compare comp);
368 III.
pop^heap
pop^heap ,
:
tempiate<class Ran>
void pop_heap(Ran f i r s t . Ran l a s t ) ;
tempiate<class Ran. class Compare>
void pop_heap(Ran f i r s t , Ran last. Compare comp);
. , :
X = *: pop_heap(a. + m);
push.heap
push_heap
:
tempiate<class Ran>
void push_heap(Ran f i r s t . Ran l a s t ) :
tempiate<class Ran. class Compare>
void push_heap(Ran f i r s t . Ran last. Compare comp);
, push_heap
, , :
v.push_back(x); push_heap(v.begin(). v.endO);
sort_heap
sort_heap
:
tempiate<class Ran>
void sort_heap(Ran f i r s t . Ran l a s t ) ;
tempiate<class Ran. class Compare>
void sort_heap(Ran f i r s t . Ran last. Compare comp);
,
.
.
15
<numer1c>.
15.
accumulate
1nner_product
part1al_suni
adjacent_d1fference
accumulate
accumulate
, first last.
( 0) .
( ):
template <class In. class >
accumulatedn f i r s t . In last. T i n i t ) ;
accumulate
:
template t<class In. class . class BinOp>
T accumulatedn f i r s t . In last. T i n i t . BinOp binary_op):
,
:
370 III.
#include <1ostreani>
#include <numer1c>
#1nclude <funct1onal>
using namespace std;
int sumkv( int s. int x){ return s + x * x:}
int ma1n(){
const int m = 5;
int a[m] = {3. 2. 5. 1. 6}. sum = 0. mul = 1. sum2 = 0;
cout accumulate(a. a + m. sum) end!;
cout accumulateCa. a + m. mul. multipTies<int>()) end!;
cout accumulate(a. a + m, sum2. sumkv) endl;
return 0:
}
inner^product
inner_product
(
b 2ai*bi).
.
( ):
template t<class Inl. class 12. class >
inner_product(Inl f i r s t l . Inl lastl. In2 first2.
T init);
inner^product
,
. ,
:
template t<class Inl, class 12, class .
class BinOpl. class Bin0p2>
T innerjroductdnl f i r s t l . Inl lastl. In2 first2.
T i n i t . BinOpl binary^opl. Bin0p2 binary_op2);
:
cout inner__product(a. + m. b. mul. multiplies<int>(). plus<int>());
partiaLsum
partial_sum
. , 3 1 2 3 5
3 4 6 9 14, ,
.
.
, -
15. 371
.
, .
template t<class In. class Out>
Out part1al_sum(In f i r s t . In last. Out result);
template t<class In. class Out. class B1nOp>
Out part1al_sum(In f i r s t . In last. Out result.
BinOp binary_op):
adjacent^difference
adjacent_difference
, di = ai - ai_i.
. ,
.
template t<class In. class Out>
Out adjacent_difference(In f i r s t . In last. Out result):
template t<class In. class Out. class BinOp>
Out adjacent_difference(In f i r s t . In last. Out result.
BinOp binary_op);
,
,
.
valarray
\/] array.
.
,
valarray: slice_array, gslice_array, mask_array indirect_array.
,
.
valarray
<val>. ,
, si ice gsl ice,
, ,
.
slice_array ,
valarray (, ).
valarray
.
gslice_array
, mask_array
. indirect_array ,
.
372 HI.
val array ,
, :
tempiate<class > class valarray{
public:
/ / :
valarrayO:
// n (
// :
explicit valarray(size_t n ) :
// v:
valarray(const & v, size__t n ) ;
// n :
valarray(const * , s1ze_t ) ;
//
valarrayCconst valarray&):
// :
valarray(const slice_array<T>&):
// :
valarray(const gslice_array<T>&):
// :
valarray(const masl<_array<T>&):
/ / indirect_array:
valarrayCconst incl1rect_array<T>&):
:
valarray <clouble> v l : //
valarray <int> v2(100): // 100 .
//
valarray <int> v3(5. 100): // 100 , 5
const i n t m = {4. 4. 3. 6. 2}:
valarray <int> v4(m. 5): / / 5 : 4.4.3.6.2
:
operator[](size_t) const:
& operator[](size_t):
/ / :
valarray<T>& operator=(const valarray<T>&):
/ / :
valarray<T>& operator=(const &):
/ / valarray:
valarray<T>& operator=(const slice_array<T>&):
valarray<T>& operator=(const gslice_array<T>&):
valarray<T>& operator=(const mask__array<T>&):
valarray<T>& operator=(const inclirect_array<T>&):
valarray 0.
.
15. 373
, ,
valarray^:
valarray<T> operator[](sl1ce) const;
slice_array<T> operator[](sl1ce);
valarray<T> operator[](const gsl1ce&) const;
gsl1ce_array<T> operator[](const gslice&);
1<> operator[](const valarray<bool>&) const;
mask_array<T> operator[](const valarray<bool>&);
valarray<T> operator[](const valarray<s1ze_t>&) const;
1nd1rect_array<T> operator[](const valarray<s1ze_t>&);
const
valarray, const .
+, -, ~ !, +, -, *, /, %, ^, &, |, ,
, (+=, '^= . .),
, , :
valarray<T>& opGrator+= (const &);
valarray<T>& operator+= (const valarray<T>&);
,
. ,
. , , ,
.
( /).
:
v4 += 1; // - 5.5.4.7.2 (v4 )
v2 += v3; // v2[1] = v2[1] + v3[1] 1 = 1 100
v4 = 1; // - 2.2.2.3.1
val array :
s1ze_t sizeO const; //
sumO const; //
m1n() const: II
const; //
// 1 (, 1>0):
valarray<T> shiftdnt 1) const;
// 1 (, 1>0):
valarray<T> cshiftdnt) const;
// func ;
valarray<T> apply( func(T)) const;
valarray<T> apply( func(const T&)) const;
// (
// , - sz;
void res1ze(s1ze t sz. = T O ) ;
.
374 111.
si ice () :
class slice{
public:
sliceO:
slice(size_t start. size_t size. size_t stride):
size_t startO const: //
size_t sizeO const: //
size_t strideO const: //
}:
. , slice(3. 4, 2)
, 3, 5, 7, 9, sliced. 3.
4) 1, 5, 9. ,
, .
valarray, : a[slice(l. 5. 3)] = b
b , .
15. 375
slice_array. ,
:
sl1ce_array<double>& si = v2[slice(0, v.slzeO / 10. 10)]:
// 5 10 X 10:
si += 5:
slice_array<double>& s2 = v2[sl1ce(0, v.sizeO / 10. 1)]:
// 2 10 X 10:
s2 *= 2:
si 1_ray (+=, *=, >=
. .), :
void operator*= (const valarray<T>&) const:
void operator/= (const valarray<T>&) const:
, , si ice_array. ,
,
, .
,
, ,
valarray .
, ,
gslice :
class gslice^
public:
gsliceO:
gslice(size_t s. const valarray<size_t>& len.
const valarray<size_t>& d):
size_t startO const: //
valarray<size_t> sizeO const: //
valarray<size_t> strideO const: // [0]. [1]....
}:
.
s, len,
d. ,
, .
,
start =
len = {2. 3}
d = {4. 1}
= (0.1) X 4 + (0.1.2) X 1
2x3 3x4,
( 2 4 -
376 III.
, 3 1
):
: :
00 01 02 00 01
10 11 12 10 11
20 21 22 20 21
30 31 32
:
00 10 20 30 01 11 21 31 02 12 22 32
, , , 2x3, :
valarray<clouble> v ( l . 12); / /
i n t length[] = {2. 3};
i n t s t r [ ] = {4. 1};
valarray<int> len (length. 2);
valarray<int> d (str. 2);
// v[0]. v[l]. v[2]. v[4]. v[5]. v[6]:
v[gslice(0. len. d)] = 0:
si ice_array,
, , gslice_array,
, slice_array.
gslice_array :
.
,
,
mask^array, valarray<bool>. ,
true, , val array
. mask_array ,
gslice^array.
val array, , :
bool m[] = {true. true, false, true, false};
valarray<bool> mask (m. 5);
/ / 0. 1 2 v6
/ / v4.
/ / :
valarray<int> v6 = sin(v4[mask]):
,
val array, inciirect_array.
.
valarray<size_t>, :
i n t i [ ] = {3. 2. 4. 0. 1}:
valarray<int> index(i. 5);
15. 377
valarray<1nt> v7 = v4[1nclex];
// v7[0] = v4[3]. v7[l] = v4[2]. v7[2] = v4[4]. v7[3] = v4[0]. v7[4] = v4[l]
, ,
.
sl1ce_array, gsl1ce_array, mask_array 1ndirect_array
.
va1 array ,
, val array size_t
.
16
, C++
, , , ,
200 , ,
.
,
.
, new delete,
, ,
.
<cstclclef>, <l1mits>, <climits>, <cfloat>, <cstdl1b>, <new>,
<type1nfo>, <exception>, <cstdarg>, <csetjmp>, <ct1me>, <csignal>.
( <1 im1ts>) numer1c_l im1 ts,
, ; : ,
, . .
numeric_l imits.
, <cl1m1ts> <cfloat>,
, :
FLT_DIG ; float;
FLT_MIN ; ,
float;
UINT_MAX ,
unsigned int ( 65 535).
5.
bad_anoc, - new_handler
set_new_handler.
16. 379
: new
,
- new_hancller,
, bad_al 1
, abort exit
. set_new_handler
. ,
new delete.
type_1nfo, . 231,
bad_cast bad_type1d.
<except1on> exception.
,
,
. (log1c_error),
,
(runt1me_error).
, ,
:
exception
- bad_aIloc
bad_cast
- bad_typeid
- bad_exception
logic_error
- length_error
- domain_error
- out_of_range
- invalid_argument
-TCfrttimfe "feiitn
range_error
- overfiow_error
underflow error
. 1 6 . 1 .
:
bad_al 1 new;
bad_cast dynam1c_cast (.
, . 231);
bad_type1d typeld (. . 238);
^bad_exception , ,
, ;
380 III.
length_error , ,
s1ze_t;
doma1n_error ;
out_of_range ,
;
inval1d_argument ;
range_error ;
overflow_error (
);
underflow_error (
).
<stdexcept>.
, , , ,
, .
.
.
<11> <11>.
complex, float, double long double,
.
<1>.
6, ,
5, 4.
III
(.
) ,
.
(.
) .
,
(. )
/ .
(. ) string.
,
(. )
.
(.
) .
valarray
(. ) valarray .
C++,
, .
,
.
, , C++
.
, , ,
; .
, . C++
. ,
, ,
.
,
, .
C++ [18]. ,
, .
[17], [13] [9].
ISO/IEC 14882:1998
( , ,
).
C++!
.
.
->
[ ]
( )
<>( )
++
--
typeid
dynam1c_cast
stat1c_cast
reinterpret_cast
const_cast
sizeof
--
++
~
!
1. C++ 385
- ( )
+
& 1
new
delete
(<>)
->*
*
/
1^
+
1-
<
<=
>
>=
==
1^
()
1 ()
1 &&
II
? ()
=
*'
/-
386 1. C++
1 ^
+
--
&-
h
^
throw |
.
2
printf
1^ .
d, i
, , float double,
[-]m.nnnnnne[+-]xx,
.
6
1f , float double,
[-]mmm.nnnnn,
.
6
g,G % ^f, ;
0 (
)
(
,
)
s : ,
,
u
X, X
( )
% %
388 2. printf
,
^ . ,
, ,
(
).
, :
)l>-minC ^minC;
^-min.precisionC ^min.precisionC.
1/ , min ,
. precision,
, ,
:
( Xs) precision
;
( ^f *) precision
;
( ^i), precision
.
, precision, ()
.
( ^d XG) precision
, .
(-) ,
, , .
.
1 h, , ^1 f, >thu.
h d, i, , X , short int,
U short unsigned int.
1 d, i, , X , long int,
U - long unsigned int, , , f, g G double,
float.
:
#include <std1o.h>
int main(){
int i n t l = 45. int2 = 13;
float f = 3.621;
double dbl = 2.23;
char ch = ' z ' , *str = "ramambahari";
p r i n t f C i n t l = ^d| int2 - %3\ 1nt2 - -4d|\n". intl. int2. int2)
printf ("intl = ^ 1 int2 ^3x| int2 = Mo|\n". intl. int2. int2);
printfC'f = fI f ^4.2f| f - 6.1f|\n". f. f. f);
printfC'f = ^g| f = ^e| f - UE|\n". f. f. f ) ;
2. printf 389
printfC'dbl " %b.Z\1\ dbl - ^e| dbl - %^.lG|\n". dbl, dbl, dbl);
printf("ch %z\ ch = ^3c|\n". ch. ch);
printfC'str -' 14s|\nstr = ^-14s|\nstr = ^S|\n". s t r , s t r , s t r ) ;
return 0;
}
:
i n t l = 45| int2 = 13| int2 = 13
i n t l - 2D| int2 = d| int2 = 15
f = 3.6210001 f - 62 I f = 3.6
f - 3.621 I f - 3.6210008+000 f = +3.621000E+000I
dbl = 2.23 I dbl 2.2300008+000 dbl = 2 I
ch = z| ch = z I
str = ramambahari|
str = ramambahari |
str = ramambahari |
3
, , ,
. .
char, unsigned char short int
:
char char;
unsigned char ;
signed char ;
short, unsigned short enum .
int, float, double long
double.
long double,
long double.
double,
double.
float,
float.
, unsigned long,
unsigned long.
, long,
long.
, unsigned,
unsigned.
i nt.
, .
4
/
<cstdio> /
l<cstcnib>
<cwchar> /
<fstream>
<iomanip>
1 <ios> /
<iosfwcl> /
<iostream> /
<istreanp>
<ostreanp'
<sstream>
<streambuf>
<cctype>
<cwctype>
<cwtype>
<cstring> |
<cstcnib>
<string>
392 4.
1 <b1tset> 1
<deque> 1
1 <list>
<map> ,
1 <queue> , |
1 <set> , |
1 <stack>
<vector> |
, ,
1 <algor1thni> 1
1 <cstdl1b>
1 <ctime> |
1 <functional> |
1 <iterator> |
1 <memory> |
1 <ut1lity> |
, ,
1 <cassert> assert |
<cerrno>
1 <cfloat> |
1 <clim1ts>
<clocale> |
1 <cs1gnal>
1 <cstclarg>
1 <cstdclef> |
1 <cstdlib>
<ct1me> |
<exception> |
<l1m1ts>
<loca1e>
<new> |
<stdexcept>
<type1nfo> 1
4. 393
, <cmath>
<complex>
<cstdl1b>
<numer1c>
<valarray>
5
,
,
, .
assert <assert.h>
BUFSIZ <stctio.h>
BUFSIZ
fopen(), f reopen(), setbuf ().
BUFSIZ.
BUFSIZ - 255 .
CHAR^BIT
( char). CHAR^BIT 8.
,
.
5. , 395
cin stdin 1 stream
cout stdout ostream
stderr ostream
clog stderr () ostream
CHAR_MAX ,
char. signed ctiar^
CHAR_MAX SCHAR^MAX, UCHAR__MAX.
CHAR_MIN ,
char. signed char,
CHAR_MIN SCHAR^MIN, UCRAR_MIN.
clock t <time.h>
docket :
typedef long docket;
1().
CLOCKS_PER_SEC .
1() , .
, CLOCKS_PER_SEC.
DBL... <float.h>
(. FLT...)
div t <float.h>
di v_t
divO. quot , rem .
typedef struct{
int quot;
int rem;
}divj;
EDOM <errno.h>
EDOM ,
, ,
, .
396 5. ,
EOF <stdio.h>
EOF (
).
ERANGE <errno.h>
ERANGE ,
. ,
doubl .
(. ) ERANGE.
<errno.h>
, (
,
).
,
exitO .
,
exitO .
FILE <stdio.h>
FILE ,
. ,
.
typedef struct{
int level; //
unsigned flags; //
char fd; //
unsigned char hold; //
int bsize; //
unsigned char_FAR*buffer; /*
; ,
*/
unsigned char FAR *curp; /*
; */
unsigned istemp; //
short token; //
}FILE;
5. , 397
filebuf <fstream.h>
FILENAME_.MAX <stdio.h>
FILENAME__MAX .
float_round_style <stdio.h>
float_round_style
(. numericalimits).
enum float_round_style{
round^i ndetermi nate.
round_toward_zero.
round_to__nearest,
round_towardJnfinity.
round^towa rd_neg_i nf i ni ty
}:
[FLT... <float.h>
double (DBL...) long
double(LDBL_...):
1 FLTJIG float |
1 FLTJPSILON float, ,
1.0 + FLTJPSILON 1- 1.0 |
1 FLT_MANT__DIG FLT^RADIX
float ~ 1
1 FLT_MAX float 1
FLT_MAX__10JXP , , ,
10 FLT_MAX__10_EXP,
float |
FLT_MAXJXP , , ,
FLT^RADIX (FLT_MAX__EXP -
1), float |
FLTJIN ,
float |
FLT_MINJOJXP , , ,
10 FLT_MIN_10_EXP,
float ^
398 5. ,
FLT MIN , , ,
FLT_RADIX (FLT_MIX_EXP - 1),
float
FLT_RADIX 1
FLTJOUNDS ,
FOPEN <stdio.h>
FOPEN^MAX .
8,
stdin, stdout stderr. ,
,
,
.
fpos_t <stdio.h>
HUGE_VAL ,
double.
1fstream <fstream.h>
INT_MAX ,
int. INT^MAX 32 767.
INT__MIN ,
int. INT_MIN -32 767.
5. , 399
setvbufO:
_IOFBF ;
_IOLBF 1 ;
_IONBF 2 .
10S <ios.h>
ba$1c_1os char.
typedef basicJos<char> ios;
istringstream <sstream.h>
bas1c_1stringstrearo char.
typedef basicjstringstream<char> istringstream;
L_tmpnam <stdio.h>
L^tmpnain ,
tmpnamO .
LC .. <locale.h>
Iconv <locale.h>
Iconv - :
typedef struct lconv{
char *currency_symbol;
//
char *decimal_point; //
char *frac__digits; //
char grouping; //
char *int__curr__symbol:
// ISO
char *mon_decima1__point;
//
char mon_grouping: //
char *mon__thousand_sep;
//
char n_cs_precedes; // -
char n_sep_by_space; // -
//
char n__sign__posn; //
//
char *negat1ve__sign; //
char p_cs_precedes; //
char p_sep_by_space: //
//
char p_s1gn_posn: /* +
*/
char *positive_sign: //
char *thousands_sep: //
}:
LDBL <float.h>
(CM. FLT_...)
Idiv t <stdlib.h>
div_t
IdivO. quot ,
.
typedef struct{
int quot:
int rem;
}divj;
LONG^MAX ,
long. LONG_MAX 2 147 483 647.
5. , 401
LONG^HIN ,
long. LONG__MIN -2 147 483 647.
MB_,CUR_MAX <stcnib.h>
MB__CUR_MAX
.
MB_LEN_MAX <lim1ts.h>
,
.
mbstate t <wchar.h>
,
.
,
new new[].
typedef void (*new handler)():
NULL . ANSI-
NULL 0 0L.
offsetof <stddef.h>
of fsetof .
.
size t offsetof(struct bez. element bez):
ofstream <fstream.h>
ostream <ostream.h>
basic_ostream char.
typedef basic__ostream<char> ostream;
402 5. ,
ostringstream <sstream.h>
bas1c_ostr1ngstream char,
typedef bas1c_ostringstream<char> ostringstream:
ptrcl1ff_t <stclclef.h>
ptrdiff^t ,
.
typedef int ptrdiff^t:
,
randO.
SCHAR^MAX ,
signed char. 127.
SCHAR_MIN ,
signed char. - 1 2 7 .
fseek()
.
SEEK^SET ;
SEEK^CUR 1
- ;
SEEK^END 2 .
SHRT^MAX ,
short int; 32 767.
SHRT_MIN ,
signed short; - 3 2 767.
SIG... <signal.h>
r a i s e O
S i gna 1 ().
5. , 403
|
1 SIG_DFL )-6 |
1 SIGJRR signal 0
1 SIG IGN |
1 SIGABRT |
1 SIGFPE
1
1 SIGILL |
1 SIGINT
1 SIGSEGV
SIGTERM 0
sig__atomic__t <signal.h>
sig__atoraic__t ,
.
typedef sig_atomic_t;
size t <std1o.h>
sizeJ: ,
sizeof. ,
unsigned long.
typedef unsigned s1ze_t;
stderr .
stdin .
stdout .
streambuf <streambuf.h>
basic__streambuf char.
typedef bas1c_streambuf<char> streambuf;
404 5. ,
,
.
typedef OFF_T streamoff;
typedef POS_T streampos:
streamsize <1os.h>
,
.
typedef INT_T streamsize:
string <string.h>
basic__string char,
typedef basic_string<char> string:
stringbuf <sstream.h>
basic_stringbuf char,
typedef basic_stringbuf<char> stringbuf:
, set_
terminateO .
typedef void (^terminate handler)():
time t <time.h>
time_t .
, time()
difftimeO.
typedef long time_t:
MAX <stdio.h>
TMP_MAX
, tmpnamO
. _ 25.
5. , 405
Struct tm <time.h>
tm ,
locatimeO ptimeO.
struct tni{
int tm^sec; // ( ... 59)
int tnfi_min; // (0 ... 59)
int tm_hour; // ( ... 23)
int tm^mday; // (1 ... 31)
int tm^mon; // . ( ... 11)
int tm__year: // 1900
int tm__yday; // (0 ... 365)
int tmjsdst;//
}:
UCHAR <limits.h>
UCHAR^MAX ,
unsigned char. 127.
INT <limits.h>
UINT^MAX ,
unsigned int. 65 535.
ULONG <limits.h>
ULONG_MAX ,
unsigned long int.
4 294 967 295.
USHRT <limits.h>
USHRT^MAX ,
unsigned short int. 65 535.
.
set_unexpected(), ,
.
typedef void (*unexpected_handler) :
406 5. ,
. <stclarg.h>. va^list
. vastart
. va_arg ,
, va^list.
,
va_end.
typedef vo1d__FAR *va_1ist;
WCHAR_MAX ,
wchar_t.
WCHAR_MIN ,
wchar_t.
wctype__t ,
.
struct wctype_t{
long tmin; //
long tmax; //
unsigned char *index; //
unsigned i n t *type; //
long cmin; //
long cmax; //
long *code: / /
}:
WEOF <wctype.h>
WEOF ( EOF
).
,
.
5. , 407
win stdin wistream
wfilebuf <fstream.h>
basic^filebuf wchar^t.
typedef basic^filebuf <wchar__t> wfilebuf;
wifstream <fstream.h>
basic^ifstream wchar^t.
typedef basicjfstream<wchar__t> wifstream;
wint__t <wctype.h>
wios <ios.h>
basic__ios wchar^t.
typedef basicJos<wchar_t> wios;
wistream <istream.h>
wistringstream <sstream.h>
basic^istringstream wchar^t.
typedef basic_istringstream<wchar__t> istringstream;
wofstream <fstream.h>
basic^ofstream wchar^t.
typedef basic_ofstream<wchar_t> wofstream;
408 5. ,
wostream <ostream.h>
basic^ostream wchar_t.
typedef basic_ostream<wchar_t> wostream;
wostringstream <sstreani.h>
basic_ostringstream wchar_t.
typedef basic_ostringstream<wchar_t> wostringstream;
wstreambuf <streambuf.h>
basic_streambuf wchar_t.
typedef basic_streambuf<wchar_t> wstreambuf;
,
.
typedef OFF_T wstreamoff;
typedef POS_T wstreampos;
wstring <string.h>
basic_string wchar_t.
typedef basic_string<wchar_t> wstring;
wstringbuf <sstream.h>
basic_stringbuf wchar_t.
typedef basic_stringbuf<wchar_t> wstringbuf;
6
.
, ,
.
<ctype.h> (<cctype>)
isalnum ,
isalpha ,
iscntrl ,
isdigit ,
Isgraph ,
islower ,
1 1 sprint ,
ispunct ,
I isspace ,
1 supper ,
iswalnum ,
iswalpha ,
iswcntrl ,
1swctype
410 6.
iswdigit ,
iswgraph ,
iswlower ,
iswprint ,
iswpunct ,
iswspace ,
iswupper ,
iswxdigit ,
1 isxdigit ,
tolower
1 toupper
tow!owe
towupper
<locale.h> (<clocale>)
localeconv
setlocale
<math.h> (<cmath>)
acos
1 asin
atari
1 atan2
1 ceil
1 cos
cosh
1 exp
fabs
floor
fmod .
frexp
Idexp
log
loglO 10
6. 411
modf
pow
sin
1 sinh
1 sqrt
tan
tanh
<signal.h> (<csignal>)
raise
signal
<stdio.h> (<cstdio>) /
fclose /
feof
ferror
fflush
1 fgetc J
fgetpos
fgets
fgetwc
fgetws |
1 fopen / |
fprintf
fputc
fputs 1
fputwc |
fputws
fread [
freopen / |
fscanf |
1 fseek 1
fsetpos 1
412 6.
1 ftell
fwprintf
fwrite
fwscanf
1 getc
getchar
gets
getwc
getwchar
s: error
printf
putc
putchar
puts
putwc
putwchar
remove
rename
rewind
scanf
setbuf /
setvbuf
sprintf
sscanf
swprintf
swscanf
tmpfile /
tmpnam
ungetc
ungetwc
vfprintf
vfwprintf
6. 413
vprintf
vsprintf |
vswprintf
vwprintf
wprintf
wscanf |
<stdlib.h> (<cstdlib>)
abort 1
abs I
atexit ,
atof
atoi
atol
bsearch
call
d1v
exit
free
getenv
Jabs
Idiv
malloc
mblen
mbstowcs wchart
mbtowc wchart
qsort
rand
realloc
srand
strtod
strtol
strtoul
414 6.
system j
wcstod
wcstol
1 wcstombs w c h a r t
wcstoul
wctomb w c h a r t
<string.h> (<cstring>)
memchr |
|
memcpy |
memmove
memset |
StrCdt |
strchr |
strcmp '1
strcoll |
strcpy |
strcspn 1
strerror |
strlen |
strncat |
strncmp |
strncpy |
strpbrk 1
strrchr |
strspn , I
strstr |
strtok |
strxfrm |
wcscat |
wcschr |
wcscmp |
wcscol1
6. 4 1 5
1 wcscpy
1 wcscspn J
1 wcslen
1 wcsncat
1 wcsncmp
1 wcshcpy
1 wcspbrk
1 wcsrchr
1 wcsspn ,
wcsstr
wcstok
wcstrxfrm
1 wmemcpy
wmemmove
wmemset
<time.h> (<ctime>)
asctime /
clock
ct1me
difftime
gmtime tm timet
localtime tm time_t
mktime
strftime fmt tm
\ time / timet
wcsftime fmt tm
<wchar.h> (<cwchar>)
btowc
fwide
wmemchr
wmemcmp
416 6.
, , ,
5.
abort <stdlib.h>
void abort(void);
abnormal program termination
.
. exit.
, ,
3.
abs <stdl1b.h>
acos <math.h>
double acos(double ):
.
asctime / <t1me.h>
asin <math.h>
atan <math.h>
double atan(double );
.
atan2 <math.h>
atexit , <stcnib.h>
atof <stdlib.h>
atoi <std1ib.h>
atol <std11b.h>
bsearch <stdlib.h>
void bsearch(const void *key. const void *buf. size__t num. size^t size,
int e(*compare)(const void *, const void *) );
but
, , key,
, num -- buf, size
.
()
-1,0 1 , ,
. ,
compare void*,
418 6.
,
( ).
bsearch ,
, NULL.
btowc <wchar.h>
wint_t btowcCint );
WEOF, EOF,
mbrtowc. ,
WEOF.
call <stdlib.h>
ceil <math.h>
clearerr <std1o.h> J
1 clock <time.h>
docket clock{void);
docket
(
tlme.h CLOCKS_,PER__SEC).
cos <math.h>
cosh <math.h>
double coshCdouble ):
, .
,
HUGEVAL, ERANGE.
ctime <time.h>
char *ctime(t1me__t * t ) ;
,
time__t.
difftime <time.h>
div <stdlib.h>
exit <std1ib.h>
<math.h>
fabs <math.h>
fclose / <stdio.h>
feof <stdio.h>
int feof(FILE * f ) ;
EOF , ,
, 0.
terror <std1o.h>
fflush <std1o.h>
int fflushCFILE * f ) ;
.
, EOF.
fgetc <stdio.h>
i n t fgetc(FILE * f ) :
i nt f.
, EOF.
fgetpos <stdio.h>
fgets <std1o.h>
fgetwc <stdio.h>
w1nt_t fgetwcCFILE * f ) ;
wi nt_t f.
, WEOF.
fgetc .
fgetws <stdio.h>
<math.h>
fmod <math.h>
fopen / <stdio.h>
fprintf <stdio.h>
fputc <stdio.h>
ch f. EOF,
.
fputs <stdio.h>
fputwc <stdio.h>
fputws <stdio.h>
free <std1ib.h>
! fread <stdio.h>
freopen / <stdio.h> 1
frexp <math.h>
.
expptr.
fscanf <stclio.h>
int fscanf (FILE *f. const char *fmt [.pari. par2. ...]);
pari, 2 . . ,
fmt, f. ,
.
fseek 1 <std1o.h>
fsetpos <std1o.h>
fte11 <stclio.h>
fwlde <wchar.h>
fwprintf <stdio.h>
f ,
, , fmt.
. fprintf .
fwrite <stdio.h>
fwscanf <std1o.h>
int fwscanf (FILE *f. const wchar_t *fmt. ...);
, fmt, f
, .
WEOF, .
f scanf .
getc <std1o.h>
getchar <stdio.h>
int getchar(void);
i nt .
, EOF.
getenv <stdlib.h>
wint_t getwc(FILE * f ) ;
6. 425
wint^t f.
, WEOF.
getc .
getwchar <stcl1o.h>
wint^t getwchar(void);
(
). getchar .
isalnum , <ctype.h>
isalpha , <ctype.h>
i n t isalpha(int ch);
ch true,
ch , f ! .
1 iscntrl , <ctype.h>
i n t i s c n t r l ( i n t ch):
ch true,
ch ( line feed, del,
, 0x01-IF (
ASCn)), false .
isdlgit , <ctype.h>
int i s d i g i t ( i n t ch):
ch true,
ch , fal se .
isgraph , <ctype.h>
ch true,
ch ( ,
. .) false .
islower , <ctype.h>
isprint , <ctype.h>
ispunct , <ctype.h>
isspace , <ctype.h> 1
isupper , <ctype.h>
1 iswalnum , <ctype.h>
iswalpha , <ctype.h>
iswcntrl , <ctype.h>
i swctype <ctype.h> ,
1 iswdigit , <ctype.h>
iswgraph , <ctype.h>
I iswprint , <ctype.h>
true, ch
(iswgraph + ), false .
iswprint .
iswpunct , <ctype.h>
iswspace , <ctype.h>
iswupper , <ctype.h>
iswxdigit , <ctype.h>
j Isxdigit , <ctype.h>
labs <stdlib.h>
num.
Idexp <math.h>
1div <stdlib.h>
localeconv <locale.h>
struct tm localtime(time__t * t ) :
tm,
/ t. (
gmtime,,e ).
log <math.h>
double log(double );
.
loglO 10 <math.h>
double loglOCdouble );
10.
mall <stdlib.h>
<stcnib.h>
mbstowcs <stdlib.h>
wchar^t
memchr <string.h>
memcmp <string.h>
memcpy <string.h>
memmove <string.h>
I memset <string.h>
mktime <time.h>
modf <math.h>
pow <math.h>
printf <stdio.h>
putc <stdio.h>
putchar <std1o.h> 1
puts <stdio.h>
putwc <stdio.h>
1 putwchar <stdio.h>
qsort <stdlib.h>
raise <signal.h>
C + + .
.
rand <stcll1b.h>
i n t rand(vold);
RAND .
real 1 <stdl1b.h>
remove <std1o.h>
rename <stdio.h>
rewind <std1o.h>
scanf <std1o.h>
pari, 2 . . ,
fmt, ( ).
, .
setbuf / <stclio.h>
setlocale <locale.h>
setvbuf <stdio.h>
signal <signal.h>
sin <math.h>
sinh <math.h>
sprintf <stcl1o.h>
sqrt <math.h>
double sqrt(doub1e );
.
srand <stdlib.h>
int sscanf(const char *buf. const char *format [.pari. par2. ...]):
scanf , ,
, . buf
, , format ,
,
,
.
strcat <str1ng.h>
strchr <string.h>
strcmp <str1ng.h>
strcoll <str1ng.h>
strcpy <str1ng.h>
strcspn <str1ng.h>
strerror <str1ng.h>
strlen <str1ng.h>
strncat <string.h>
strncmp <string.h>
( si s2), ( si s2)
( si s2) .
strncpy <string.h>
strpbrk <string.h>
strrchr <str1ng.h>
strspn , <string.h>
strstr <str1ng.h>
strtod <stcllib.h>
strtok <str1ng.h>
strtol <stdl1b.h>
strtoul <stdl1b.h>
strxfrm <string.h>
swprintf <std1o.h>
swscanf <stdio.h>
,
,
. sscanf .
system <stcll1b.h>
tan <math.h>
tanh <math.h>
double tanh(double );
.
t1me_t t1fTie(t1me_t * t ) ;
/ time^t ( ,
1 1970 ). *t NULL,
.
tmpfile / <std1o.h>
FILE nmpfile(vold);
/
.
tmpnam <std1o.h>
char 4mpnam(char * s ) ;
,
. _ .
_ <stdio. h> 65535. s - ,
, , L^tmpnam . L_tmpnam
<stdio.h>. s , tmpnam
.
440 6.
S , tmpnam ,
L_tmpnam , s.
tolower <ctype.h>
toupper <ctype.h>
i n t toupperdnt ch);
ch .
ch .
towupper <ctype.h>
ungetc <stdio.h>
ungetwc <stdio.h>
vfprintf <stdio.h>
fprintf,
, .
vfwprintf <stdio.h>
vprintf <stcl1o.h>
vsprintf <stdio.h>
vswprintf <stdio.h>
vwprintf <stdio.h>
wcscat <str1ng.h>
wcschr <str1ng.h>
ch s,
, NULL. strchr
.
wcscmp <str1ng.h>
wcscoll <string.h>
wchar_t *wcscoll(wchar_t * s l . wchar_t *s2):
strcmp,
(. setlocale). strcoll
.
wcscpy <str1ng.h>
wcscspn <str1ng.h>
wcslen <str1ng.h>
s1ze_t wcslen(wchar_t * s ) ;
( ).
strlen .
wcsncat <str1ng.h>
s2 si, , si.
strncat .
wcsncmp <str1ng.h>
wcsncpy <string.h>
wcspbrk <string.h>
wcsrchr <str1ng.h>
wcsspn , <str1ng.h>
wcsstr <str1ng.h>
wcstod <std11b.h>
wcstok <str1ng.h>
wcstol <stdl1b.h>
wcstoul <stdl1b.h>
wcstrxfrm <str1ng.h>
wmemcmp <wchar.h>
wmemcpy <str1ng.h>
wmemmove <string.h>
wmemset <str1ng.h>
wpn'ntf <std1o.h>
,
, , ,
format. printf .
wscanf <stclio.h>
S
OJ VO ffl tn t^ ^ CO s 3S i4 s __ CJ H >.-e Xi " t^ S n ^\
LO
CNJ CD 00 CN CO in CD 00 CN CO in CD 00 CTi CM CO ^ in
CN CN CN
CM
CN
CO CO CO CO CO CO CO CO CO CO
CN
in in in in in in
CN CN CN CN CM CN CM CM CN CN CN CN CN CM CM CN CN CN CN CN CM CM CM CM CN CN CN CM
=
;
\
(
h-
:
S
< W OQ Ui t^ W ^ 00 S 3S >^ t=^ s X H >. e X ^ tJ" 3 ^ 3 HQ (T) Q
A
CQ in CD -r-t CN in CD
CD 00 C7^ CN CO 00 CO 00 T1 CN CO
CN CM CM CM CM
CN CN CN CN CN CN CN CN CN CN CN CN CN CN CM CN CN CN CN
^ CN CN CN CN
.
I
00
4 4 V{-1 4 -H- < f^ V 4C Aa 4C 1 1 s
H
^ A 4C 4c: a-
;
m
^ 00 CN CO 4f CD 00 CN CO in CD 00 CN co in CD 00 a^\
CN CN CO CO CO CO CO CO CO CO CO CO ^ in in in in in in in in in in
^
7. ASCII 449
1 |
128 160 192 L 224
129 161 193 X 225
130 162 194 226
131 163 195 [ 227 ^
132 164 196 - 228
133 165 197 + 229 X
134 166 198 230
135 3 167 3 199 h 231
136 168 200 L 232
137 169 201 233
X
138 170 202 234
139 171 203 235 1
U0 172 204 ^ 236
141 173 205 = 237
JL
U2 0 174 206 238
143 175 207 239
144 176 208 X 240
145 1 177 Hi 209 241
146 178 1 210 242
147 ' 179 1 211 243
148 180 ^ 212 244 I
149 181 213 245 1
150 182 ^ 214 246 9
151 183 1 215 247
152 1 184 1 216 248
153 185 i 217 249
154 186 1 218 250
155 187 219 251 4
156 b 188 J 220 252 W
157 189 J 221 253
158 190 J 222 254
255
[ 159 191 1 223
#%& ate, ios, 281
#define, 94 atexit, 417
#elif, 95 atof, 92, 417
#else, 95 atoi, 92, 417
#endif, 95 atol, 92, 417
#if, 95 auto, 30
#ifdef, 95
#ifndef, 95
#include, 93 back, list, 306
#undef, 96 back, queue, 312
back, vector, 301
cplusplus, 96
back_insert_iterator, 332
_ _ D A T E _ , 96
back_inserter, 333
_ F I L E _ , 96
bad, ios, 278
_ L I N E _ , 96
bad_alloc, 230, 378, 379
_ T I M E _ , 96
bad_cast, 233, 379
J O F B F , 399
bad_exception, 379
J O L B F , 399 bad_typeid, 238, 379
J O N B F , 399 badbit, ios, 278
basefield, ios, 270
before, type_info, 239
abort, 223, 228, 416
begin, , 297
abs, 93, 416
binary, ios, 281
accumulate, , 369
binary_function, 335
acos, 93, 416 binary_negate, 337
adjacent_difference, , 371 binary_search, , 358
adjacent_find, , 345 bind 1st, , 337
adjustfield, ios, 270 bind2nd, , 337
advance, 331 binderlst, 337
any, bitset, 325 binder2nd, 337
app, ios, 281 bitset, 323
append, string, 289 bool, 24
apply, valarray, 373 bsearch, 417
asctime, 416 btowc, 418
asin, 93, 416 BUFSIZ, 394
assert, 394
assign, string, 288
assign, vector, 300 C_str, string, 290
at, string, 288 calloc, 418
at, vector, 301 capacity, string, 294
at, , 298 capacity, vector, 301
atan, 93, 416 catch, 224, 301
atan2, 93, 416 ceil, 93, 418
451
N proirity_queue, 313
name, type_info, 239 protected, 200
namespace, 97 ptr_fun, 338
negate, , 335 ptrdiff_t, 331, 402
new_handler, 378, 401 public, 179, 200, 210
next_permutation, , 362 push, proirity_queue, 313
nocreate, ios, 281 push, queue, 312
none, bitset, 325 push, stack, 311
noreplace, ios, 281 push_back, list, 306
not_equal_to, , 336 push_back, vector, 302
notl, , 337 push_back, , 297
not2, , 337 push_front, deque, 306
nth_element, , 362 push_front, list, 306
NULL, 54, 401 push_front, , 297
push_heap, , 368
numeric_limits, 378
put, ostream, 274
putback, istream, 273
oct, ios, 269 putc, 90, 431
oct, , 271 putchar, 90, 432
offsetof, 401 puts, 90, 432
ofstream, 280, 401 putwc, 432
open, fstream, 281 putwchar, 432
open_mode, ios, 281
Q
operator, 190
qsort, 432
ostream, 265, 401
queue, 311
ostream_iterator, 333
ostringstream, 282, 402 R
out, ios, 281 raise, 432
out_of_range, 288, 301, 379 rand, 433
overflow_error, 379 RAND_MAX, 402
random_shuffle, , 352
p range_error, 379
pair, 315 rbegin, , 297
partial_sort, , 363 rdstate, ios, 278
partial_sort_copy, , 363 read, istream, 273
partial_sum, , 371 readsome, 273
partition, , 363 realloc, 433
peek, istream, 273 reference, , 296
perror, 431 register, 30
plus, , 335 reinterpret_cast, 238
pointer_to_binary_function, 338 remove, 433
pointer_to_unary_function, 338 remove, list, 308
pop, proirity_queue, 313 remove, , 353
pop, queue, 312 remove_copy, , 353
pop, stack, 311 remove_copy_if, , 353
pop_back, list, 307 remove_if, list, 308
pop_back, vector, 302 remove_if, , 353
pop_back, , 298 rename, 433
pop_front, deque, 306 rend, , 297
pop_front, list, 306 replace, string, 290
pop_front, , 297 replace, , 354
pop_heap, , 368 replace_copy, , 354
pow, 93, 431 replace_copy_if, , 354
precision, ios, 270 replace_if, , 354
prev_permutation, , 362 reserve, vector, 301
printf, 27, 81, 90, 231, 431 reset, bitset, 324
private, 178, 200 resetiosflags, , 272
455
W X
WCHAR_MAX, 406 xjill, ios, 270
wchar_t, 24 x_flags, 269
wcscat, 441 x_precision, ios, 270
wcschr, 441 X width, ios, 270
wcscmp, 442
wcscoll, 442
wcscpy, 442
wcscspn, 442
wcsftime, 442
wcslen, 442
wcsncat, 442 M
wcsncmp, 443 , 208
wcsncpy, 443
wcspbrk, 443 , 331
wcsrchr, 443 , 295, 310
wcsspn, 443 , 341
wcsstr, 443 , 338
wcstod, 444 , 335
wcstok, 444 STL, 343
wcstol, 444 , 17
wcstombs, 444 , 296, 315
wcstoul, 444
wcstrxfrm, 444
wctomb, 445 , 38
wctype_t, 406 , 200
WEOF, 406 , 122
werr, 407 , 323
wfilebuf, 407 , 69
whar_t, 266 , 16
width, ios, 270 , 89, 265
wifstream, 407
win, 407
, 298
wint_t, 407
, 39
wios, 407
, 206
wistream, 266, 407 , 265
wistringstream, 407 , 29
wlog, 407 , 74
wmemchr, 445 , 329
wmemcmp, 445 , 15, 28, 37
wmemcpy, 445 , 329
wmemmove, 445 , 224
wmemset, 445
wofstream, 266, 407
wostream, 266, 408
, 329
wostringstream, 408 , 298, 304
wout, 407 , 33
wprintf, 445 , 188
write, ostream, 274 , 205
ws, , 271 , 53
wscanf, 446 , 114
wstreambuf, 408 , 60
wstreamoff, 408 , 93
wstring, 408
wstringbuf, 408 , 188
wtrans_t, 406 , 187
458
, 178
, 73 , 205
, 16, 93 , 215
, 19 , 61
, 208
, 321
, 18 , 323
, 265 , 388
, 59 , 27
, 29
, 29
, 176 , 176
, 33 , 201
, 222 , 208
, 227
, 222 , 111
, 296, 328 ,
, 332 , 22
, 333
, 334
, 29
, 29, 97
, 178
, 375
, 29 , 224
, 115, 315 , 331
, 200 , 126
, 19 , 210
, 22 , 180
, 16
, 16 , 31
, 20 , 73
, 329 , 15
break, 50
, 181 continue, 51
, 181 do while, 46
, 218 for, 47
, 180, 182 goto, 49
, 184 if, 40
, 204 namespace, 99
, 182 return, 51
, 185 switch, 43
, 211, 295 using, 100
, 223 using namespace, 100
, 13 while, 45
, 32
. (), 181
, 15 .->, 181
, 115 const_cast, 232
, 179 delete, 54
, 192
dynamic_cast, 232
, 271 new, 54
, 272 , 192
, 271 reinterpret_cast, 238
, 58 sizeof, 34
, 59 static_cast, 237
, 50 typeid, 238
459
() ^ , 233
, 295
, 195 , 265
. (), 68 , 269
->, 68 , 333
--, 33 , 334
/, 34 , 38
, 16, 93
, 180 , 18
, 267 , 16
, 196 , 200
++, 33 , 329
&&,", 36 , 201
!, 34 , 97
%, 34
, 73
, 35
, 329
&, 53
~, 34 , 40
&, |, ^, 35
, 58
, 195
, 206
, 56, 231
, 223
+=-, -=, *=, /=, %=, 36 , 82
, 204
, 191
*, 55 , 337
, 35 , 73
-, 34 , 38
(?:), 36 , 321
, 31 , 315
, 337 - , 175
, 121,311 , 175
, 313 , 16
, 220
, 328 , 86
, 175 , 388
, 221 , 211,298, 306
, 184
, 189 , 379
, 284 , 380
, 83 , 378
, 374
, 77 , 58
, 77
, 78 , 295
, 233
, 28 , 230
, 364 , 265
, 233
, 206 , 186
, 97, 99 , 186
, 176 , 119,310
, 208 , 63
, 208 , 21
, 178 , 13, 14
, 205 , 210
460
, , 270
, 207 , 73
main, 87
void, 26 , 74
, 22 , 73
bool, 23 , 73
char, 23 , 83
double, 23 , 82
float, 23 , 85
int, 23
-, 189
wchar_t, 23
, 74 , 195
, 29 , 334
-
, 211 , 39
, 51 , 206
, 268
, 197
, 198 , 211
, 89 , 85
, 21
, 180
, 280 , 115
, , 269 , 178
CIC++.
.
.
.
. .
.
.
05784 07.09.01.
17.04.03. 70X100'/,^.
. . . 37,41. 4500 . 2783.
0 0 0 .
196105, -, . , . 67.
-
005-93, 2; 953005 - .
. . .
, .
197110, -, ., 15.